1.Spring Security是针对Spring项目的安全框架,也是Spring Boot底层安全模块默认的技术选型。他能够实现强大的web安全控制。对于安全控制,咱们仅需引入spring-boot-starter-security模块,进行少许的配置,便可实现强大的安全管理。几个类:
**WebSecurityConfigurerAdapter:自定义Security策略
AuthenticationManagerBuilder:自定义认证策略
@EnableWebSecurity:开启WebSecurity模式**java
2.应用程序的两个主要区域是“认证”和“受权”(或者访问控制)。这两个主要区域是Spring Security 的两个目标。web
3.“认证”(Authentication),是创建一个他声明执行流程的主体的过程(一个“主体”通常是指用户,设备或一些能够在你的应用程序中执行动做的其余系统)。spring
4.“受权”(Authorization)指肯定一个主体是否容许在你的应用程序执行一个动做的过程。为了抵达须要受权的店,主体的身份已经有认证过程创建。浏览器
5.这个概念是通用的而不仅在Spring Security中。安全
HttpSecurity配置登录、注销功能cookie
须要引入thymeleaf-extras-springsecurity4
sec:authentication=“name”得到当前用户的用户名
sec:authorize=“hasRole(‘ADMIN’)”当前用户必须拥有ADMIN权限时才会显示标签内容session
表单添加remember-me的checkbox
配置启用remember-me功能框架
HttpSecurity启用csrf功能,会为表单添加_csrf的值,提交携带来预防CSRF;
执行流程ide
一、引入SpringSecurity; 二、编写SpringSecurity的配置类; @EnableWebSecurity extends WebSecurityConfigurerAdapter 三、控制请求的访问权限: configure(HttpSecurity http) { http.authorizeRequests().antMatchers("/").permitAll() .antMatchers("/level1/**").hasRole("VIP1") } 四、定义认证规则: configure(AuthenticationManagerBuilder auth){ auth.inMemoryAuthentication().withUser("zhangsan").password("123456").roles("VIP1","VIP2") } 五、开启自动配置的登录功能: configure(HttpSecurity http){ http.formLogin(); } 六、注销:http.logout(); 七、记住我:Remeberme();
1.spring-boot
<dependency> <groupId>org.thymeleaf.extras</groupId> <artifactId>thymeleaf-extras-springsecurity4</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>
2.
package com.atguigu.security.config; 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 MySecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { //super.configure(http); //定制请求的受权规则 http.authorizeRequests().antMatchers("/").permitAll() .antMatchers("/level1/**").hasRole("VIP1") .antMatchers("/level2/**").hasRole("VIP2") .antMatchers("/level3/**").hasRole("VIP3"); //开启自动配置的登录功能,效果,若是没有登录,没有权限就会来到登录页面 http.formLogin().usernameParameter("user").passwordParameter("pwd") .loginPage("/userlogin"); //一、/login来到登录页 //二、重定向到/login?error表示登录失败 //三、更多详细规定 //四、默认post形式的 /login表明处理登录 //五、一但定制loginPage;那么 loginPage的post请求就是登录 //开启自动配置的注销功能。 http.logout().logoutSuccessUrl("/");//注销成功之后来到首页 //一、访问 /logout 表示用户注销,清空session //二、注销成功会返回 /login?logout 页面; //开启记住我功能 http.rememberMe().rememberMeParameter("remeber"); //登录成功之后,将cookie发给浏览器保存,之后访问页面带上这个cookie,只要经过检查就能够免登陆 //点击注销会删除cookie } //定义认证规则 @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { //super.configure(auth); auth.inMemoryAuthentication() .withUser("zhangsan").password("123456").roles("VIP1","VIP2") .and() .withUser("lisi").password("123456").roles("VIP2","VIP3") .and() .withUser("wangwu").password("123456").roles("VIP1","VIP3"); } }