关于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";     } }
相关文章
相关标签/搜索