關於security的簡單理解和應用

2018年7月30日1.搜索引擎框架百度googleLucene 單機操做,就是一堆jar包中的api的使用,本身干預,如何建立索引庫,刪除索引庫,更新索引庫,高亮,本身調度APISolr   支持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:9300spring.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";     } }
相關文章
相關標籤/搜索