1. 程式人生 > >關於security的簡單理解和應用

關於security的簡單理解和應用

pid server 集群 css exc for 關於 統一 rip

2018年7月30日
1.搜索引擎框架
百度
google

Lucene 單機操作,就是一堆jar包中的api的使用,自己幹預,如何創建索引庫,刪除索引庫,更新索引庫,高亮,自己調度API
Solr 支持web應用研發,它封裝好了對索引庫的操作,直接做高級API編程。
ElasticSearch 默認支持集群的,調度,統一協調,任務派發,ZooKeeper (KeepAlived 簡單)


Lucene簡介
Lucene是apache軟件基金會一個開放源代碼的全文檢索引擎工具包,是一個全文檢索引擎的架構,提供了完整的查詢引擎和索引引擎,
部分文本分析引擎。
Lucene的目的是為軟件開發人員提供一個簡單易用的工具包,以方便在目標系統中實現全文檢索的功能,或者是以此為基礎建立起完整的
全文檢索引擎。
Lucene最初是由Doug Cutting 所撰寫的,是一位資深全文索引/檢索專家,曾經是V-Twin搜索引擎的主要開發者,後來在Excite擔任高級
系統架構設計師,目前從事於一些INTERNET底層架構的研究。他貢獻出Lucene的目標是為各種中小型應用程式加入全文檢索功能。

OSChina使用Lucene實現全文搜索。


索引:
全文索引
SQL Server全文索引
Mysql全文索引

全文搜索是一種將文件中所有文本與搜索項匹配的文字資料檢索方法:
建文本庫---》建立索引---》執行搜索---》過濾結果


ElasticSearch簡介
ElasticSearch是一個基於Lucene實時分布式搜索和分析引擎。設計用於雲計算中,能夠達到實時搜索,穩定,可靠,快速,
安裝使用方便。基於RestFul接口。
ElasticSearch就是為可用和可擴展而生的。可以通過購置性能更強的服務器來完成,稱為垂直擴展或者向上擴展,或者增加
更多的服務器來完成,稱為水平擴展或者向外擴展。

ES核心概念:
近實時
集群:一個或者多個節點的集合,保存應用的全部數據,並提供基於節點集成式的索引和搜索功能。
節點
分片:每個索引分成多個分片
小Tip: 默認ES每個索引分配5個分片,一個副本(5個分片),共計10個分片。

SpringBoot 整合 ElasticSearch:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>

<dependency>
<groupId>com.sun.jna</groupId>
<artifactId>jna</artifactId>
<version>3.0.9</version>
</dependency>

配置文件配置:
spring.data.elasticsearch.cluster-nodes=123.56.20.15:9300
spring.data.elasticsearch.properties.transport.tcp.connect_timeout=1200s


------------------------------------------
ElasticsearchRepository(能力是最強的)---->ElasticsearchCrudRepository--->PagingAndSortingRepository--->CrudRepository



---------------------------------------------------------------------------------
SpringSequrity認證:
步驟一:依賴

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>

<!--thymeleaf和springsecurity整合的依賴-->
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity4</artifactId>
</dependency>

<!--thymeleaf 新的模板引擎,比jsp要出色-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

步驟二:
application.properties中需要的配置
##模板引擎的配置
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.cache = false
spring.thymeleaf.mode=HTML5

步驟三:
準備UI頁面
1.1 login.html:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>登錄</title>
<script type="text/javascript" th:src="@{/js/jquery-3.3.1.js}"></script>
<script type="text/javascript">

</script>
</head>
<body>
<div>
<form th:action="@{/login}" method="post">
<h2>請登錄</h2>
用戶名:<input name="username" type="text"/><br/>
密碼:<input name="password" type="password"/><br/>
<input type="submit" value="登錄"/><br/>
<div th:if="${loginError}"></div>
<div th:text="${errorMsg}"></div>
</form>
</div>
</body>
</html>

1.2 index.html

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4">

<head>
<meta charset="UTF-8">
<title>博客系統</title>
<script type="text/javascript" th:src="@{/js/jquery-3.3.1.js}"></script>
<script type="text/javascript">

</script>
</head>
<body>
<div>
<!--authorize:認證,授權-->
<div sec:authorize="isAuthenticated()">
<p>登錄的用戶名為:<span sec:authentication="name"></span></p>
<p>登錄的角色為:<span sec:authentication="principal.authorities"></span></p>
</div>
<!--匿名的。未經過認證的-->
<div sec:authorize="isAnonymous()">
<p>未登錄</p>
</div>
</div>
</body>
</html>

步驟四:核心配置
在util層創建一個類SecurityConfig,繼承了 WebSecurityConfigurerAdapter
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/css/**","/js/**","/fonts/**","/index").permitAll() //都可以訪問
.antMatchers("/users/**").hasRole("ADMIN") //需要相應的角色才能訪問
.and()
.formLogin() //基於form表單登錄驗證
.loginPage("/login") //自定義登錄信息
.failureUrl("/login-error");
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception{
auth
.inMemoryAuthentication() //認證信息存儲在內存中
.passwordEncoder(new MyPasswordEncoder()) //在此處應用自定義PasswordEncoder
.withUser("happy").password("6375196").roles("ADMIN");
}
}

步驟五:自定義密碼編輯器: MyPasswordEncoder
import org.springframework.security.crypto.password.PasswordEncoder;

public class MyPasswordEncoder implements PasswordEncoder {

@Override
public String encode(CharSequence arg0) {
return arg0.toString();
}

@Override
public boolean matches(CharSequence arg0, String arg1) {
return arg1.equals(arg0.toString());
}
}

步驟六:controller中給路徑做界面映射和尋址
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class MainController {
@GetMapping("/index")
public String index(){
return "index";
}

@GetMapping("/login")
public String login(){
return "login";
}

@GetMapping("/login-error")
public String loginError(Model model){
model.addAttribute("loginError",true);
model.addAttribute("errorMsg","登錄失敗,用戶名或密碼錯誤");
return "login";
}
}

關於security的簡單理解和應用