springSecruity
Spring Security 是 Spring 社区的一个顶级项目,也是 Spring Boot 官方推荐使用的安全框架。除了常规的认证(Authentication)和授权(Authorization)之外,Spring Security还提供了诸如ACLs,LDAP,JAAS,CAS等高级特性以满足复杂场景下的安全需求。另外,就目前而言,Spring Security和Shiro也是当前广大应用使用比较广泛的两个安全框架。
Spring Security 应用级别的安全主要包含两个主要部分,即登录认证(Authentication)和访问授权(Authorization),首先用户登录的时候传入登录信息,登录验证器完成登录认证并将登录认证好的信息存储到请求上下文,然后再进行其他操作,如在进行接口访问、方法调用时,权限认证器从上下文中获取登录认证信息,然后根据认证信息获取权限信息,通过权限信息和特定的授权策略决定是否授权。
实现案例
controller
@Controller
public class RouterController {
@RequestMapping({"/index", "/"})
public String index() {
return "index";
}
@RequestMapping("/toLogin")
public String toLogin() {
return "views/login";
}
@RequestMapping("/level1/{id}")
public String level1(@PathVariable("id") int id) {
return "views/level1/" + id;
}
@RequestMapping("/level2/{id}")
public String level2(@PathVariable("id") int id) {
return "views/level2/" + id;
}
@RequestMapping("/level3/{id}")
public String level3(@PathVariable("id") int id) {
return "views/level3/" + id;
}
}
- 配置项目模板:
- 勾上依赖安全(security)、web、模板引擎(templates)
- 导入页面项目在templates中:
- 添加config文件夹并在其中配置security信息:
代码:
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
//链式编程
@Override
protected void configure(HttpSecurity http) throws Exception {
//首页所有人可以访问,功能页有权限的人才能访问
//请求规则的授权
http.authorizeRequests()
.antMatchers("/").permitAll()
.antMatchers("/level1/**").hasRole("vip1") //含有vip1权限能够进入
.antMatchers("/level2/**").hasRole("vip2")
.antMatchers("/level3/**").hasRole("vip3");
http.formLogin(); //没权限就进入登录页面 /Login
}
//密码编码调整 passwordEncoder
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder()) //使用
.withUser("zhongqing").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1","vip2") //添加用户并赋予权限
.and() //加and可实现下一个用户添加
.withUser("root").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1","vip2","vip3")
.and()
.withUser("zq").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1");
}
}
注销及权限控制
- themleaf和security整合:
<!--security thymeleaf整合包-->
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity5</artifactId>
<version>3.0.4.RELEASE</version>
</dependency>
<!--thymeleaf-->
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring5</artifactId>
</dependency>
<!--security-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
- 引入thymeleaf连接和security整合链接:
<html lang="en" xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
- 登录信息显示用户名、用户包含权限,未登录信息显示退出:
<!--登录注销-->
<div class="right menu">
<!--未登录-->
<div sec:authorize="!isAuthenticated()"> <!--没有登录显示-->
<a class="item" th:href="@{/toLogin}">
<i class="address card icon"></i> 登录
</a>
</div>
<div sec:authorize="isAuthenticated()"> <!-- 登录显示如下数据-->
<a class="item" >
用户名:<span sec:authentication="name"></span> <!--获取登录用户名-->
角色:<span sec:authentication="principal.authorities"></span> <!--获取用户所含权限-->
</a>
</div>
<div sec:authorize="isAuthenticated()">
<a class="item" th:href="@{/logout}">
<i class="user times icon"></i> 退出
</a>
</div>
- 首页显示权限含有内容,没有就不显示:
<div class="column" sec:authorize="hasRole('vip1')"> <!--有vip1权限显示-->
- 配置内容,登录跳转,注销跳转:
http.formLogin().loginPage("/usr/login"); //没权限就进入登录页面 /Login
http.csrf().disable(); //防止网站工具. :get不安全,进行拦截 post
http.logout().logoutSuccessUrl("/"); //注销成功后返回首页路径
记住我首页定制
- 前端代码:
<input type="checkbox" name="rememberme">记住我
- 首页验证及跳转页面、rememberme功能:
//定制登录页,跳转登录页,提交登录页,也可以自定义返回登录账号名称
http.formLogin().loginPage("/toLogin").loginProcessingUrl("/login"); //没权限就进入登录页面 /Login
http.rememberMe().rememberMeParameter("rememberme"); //记住我,保证下次到主页可以自动为自己的账号页面,cookie保存值
http.csrf().disable(); //防止网站工具. :get不安全,进行拦截 post
http.logout().logoutSuccessUrl("/"); //注销成功后返回首页路径