一、安全
Spring Security是针对Spring项目的安全框架,也是Spring Boot底层安全模块默认的技术选型。他可以实现强大的web安全控制。对于安全控制,我们仅需引入spring-boot-starter-security模块,进行少量的配置,即可实现强大的安全管理。几个类:
WebSecurityConfigurerAdapter:自定义Security策略
AuthenticationManagerBuilder:自定义认证策略
@EnableWebSecurity:开启WebSecurity模式
•应用程序的两个主要区域是“认证”和“授权”(或者访问控制)。这两个主要区域是· Spring Security 的两个目标。
•“认证”(Authentication),是建立一个他声明的主体的过程(一个“主体”一般是指 用户,设备或一些可以在你的应用程序中执行动作的其他系统)。
•“授权”(Authorization)指确定一个主体是否允许在你的应用程序执行一个动作的过 程。为了抵达需要授权的店,主体的身份已经有认证过程建立。
•这个概念是通用的而不只在Spring Security中。
/*** 1、引入 Spring Security 模块的启动器* 2、编写 Spring Security 的配置类* @EnableWebSecurity* public class MySecurityConfig extends WebSecurityConfigurerAdapter* 3、控制请求的访问权限**/@SpringBootApplicationpublic class Springboot05SecurityApplication {public static void main(String[] args) {SpringApplication.run(Springboot05SecurityApplication.class, args);}}@EnableWebSecuritypublic class MySecurityConfig extends WebSecurityConfigurerAdapter {/*** 定义授权规则* @param http* @throws Exception*/@Overrideprotected void configure(HttpSecurity http) throws Exception {// 定制请求的授权规则http.authorizeRequests().antMatchers("/").permitAll().antMatchers("/level1/**").hasRole("vip1").antMatchers("/level2/**").hasRole("vip2").antMatchers("/level3/**").hasRole("vip3");// 开启自动配置的登录功能,如果没有权限,就会来到登录页面http.formLogin();// 1、login请求来到登录页面// 2、登陆失败重定向 /login?error// 3、更多详细规则}/*** 定义认证规则* @param auth* @throws Exception*/@Overrideprotected void configure(AuthenticationManagerBuilder auth) throws Exception {auth.inMemoryAuthentication().passwordEncoder(passwordEncoder()).withUser("admin").password(new BCryptPasswordEncoder().encode("123")).roles("vip1","vip2").and().withUser("lisi").password(new BCryptPasswordEncoder().encode("123")).roles("vip2","vip3").and().withUser("wangwu").password(new BCryptPasswordEncoder().encode("123")).roles("vip1","vip3");}private PasswordEncoder passwordEncoder() {return new BCryptPasswordEncoder();}}
登录登出
// 开启自动配置的注销功能http.logout().logoutSuccessUrl("/");//注销成功以后来到首页// 1、访问 /logout 表示用户注销,清空session// 2、注销成功返回 /login?logout 页面
权限控制显示
<html xmlns:th="http://www.thymeleaf.org"xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4">
<div sec:authorize="isAuthenticated()"><h2><span sec:authentication="name"></span>,你的角色有:<span sec:authentication="principal.authorities"></span></h2><form th:action="@{/logout}" method="post"><input type="submit" th:value="注销"/></form></div><div sec:authorize="hasRole('vip1')"><h3>普通武功秘籍</h3><ul><li><a th:href="@{/level1/1}">罗汉拳</a></li><li><a th:href="@{/level1/2}">武当长拳</a></li><li><a th:href="@{/level1/3}">全真剑法</a></li></ul></div><div sec:authorize="hasRole('vip2')"><h3>高级武功秘籍</h3><ul><li><a th:href="@{/level2/1}">太极拳</a></li><li><a th:href="@{/level2/2}">七伤拳</a></li><li><a th:href="@{/level2/3}">梯云纵</a></li></ul></div><div sec:authorize="hasRole('vip3')"><h3>绝世武功秘籍</h3><ul><li><a th:href="@{/level3/1}">葵花宝典</a></li><li><a th:href="@{/level3/2}">龟派气功</a></li><li><a th:href="@{/level3/3}">独孤九剑</a></li></ul></div>
记住我、定制登录页面
// 开启记住我功能http.rememberMe();// 登录成功以后,将cookie发送给浏览器保存,以后登录带上这个cookie,只要通过检查既可以免登录,// 点击注销,删除cookie
