SpringSecurity(安全)
Spring Security,这是一种基于 Spring AOP 和 Servlet 过滤器的安全框架。它提供全面的安全性解决方案,同时在 Web 请求级和方法调用级处理身份确认和授权。他是基于AspectJ的切面经行配置的。
Spring Security是针对Spring项目的安全框架,也是Spring Boot底层安全模块默认的技术选型,他可以是吸纳强大的Web安全控制,对于安全控制,我们仅需要引入spring-boot-starter-security模块,进行少量的配置,即可实现强大的安全管理!
Spring Security的几个重要的类:
- WebSecurityConfigureAdapter:自定义策略
- AuthenticationManagerBuilder:自定义认证策略
- @EnableWebSecurity:开启WebSecurity模式,@Enablexxxx开启某个功能
WebSecurityConfigureAdapter共有三个configure方法:
- configure(WebSecurity) 通过重载,配置Spring Security的Filter链
- configure(HttpSecurity) 通过重载,配置如何通过拦截器保护请求
- configure(AuthenticationManagerBuilder) 通过重载,配置user-detail服务
Spring Security的主要两个目标是“认证”和“授权”(访问控制)。
特征:
- 对身份验证和授权的全面和可扩展支持
- 防止攻击,如会话固定、点击劫持、跨站点请求伪造等
- 服务 API 集成
- 可选集成与Spring Web MVC
参考官网:https://spring.io/projects/spring-security
Spring Security测试:
需要导入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
写入测试类测试
package com.wj.springsecuiity;
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 {
//链式编程 授权 用户存储认证
protected void configure(HttpSecurity http,AuthenticationManagerBuilder auth)throws Exception{
//首页所有人可以访问,功能也只有对应有权限的人才能访问
http.authorizeRequests()
.antMatchers("/").permitAll()
.antMatchers("/level1/**").hasRole("vip1");
.antMatchers("/level2/**").hasRole("vip2");
.antMatchers("/level3/**").hasRole("vip3");
//基于数据库的用户存储、认证
// auth.jdbcAuthentication().dataSource(dataSource)
// .usersByUsernameQuery("select account,password,true from user where account=?")
// .authoritiesByUsernameQuery("select account,role from user where account=?");
//没有权限默认回到登录页面
http.formLogin().loginPage("toLogin");
//防止网站工具: get, post
http.csrf().disable();//关闭
//注销,开起来注销功能,跳到页面
http.logout().logoutSuccessUrl("/");
}
//认证 spring Boot 2.1.x可以直接使用
//密码编码: passwordEncoder
//在Spring Secutiry 5.0+ 新增了很多的加密方法
protected void configure(AuthenticationManagerBuilder auth)throws Exception{
auth.inMemoryAuthentication()
.withUser("wj").password("123456").roles("vip1","vip2");
.withUser("root").password("123456").roles("vip2","vip3");
}
}
标签库
Security框架可以精确控制页面的一个按钮、链接,它在页面上权限的控制实际上是通过它提供的标签来做到的。
1.引入依赖
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity5</artifactId>
</dependency>
2.在HTML页面引入Spring Security的命名空间
<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity5">
sec:authorize
这个标签用来决定它的内容是否会被执行.
显示一个特定的链接,如果用户允许点击它.
<div sec:authorize="isAuthenticated()">
<a class="item" th:href="@{/toLogin}">
<i class="address card icon"></i>登录
</a>
</div>
sec:authentication
这个标签允许访问当前的Authentication 对象, 保存在安全上下文中。
<div sec:authorize="isAuthenticated()">
<a class="item" th:href="@{/toLogin}">
用户名:<span sec:authentication="name"></span>
密码:<span sec:authentication="principal.getAuthorities()"></span>
</a>
</div>
sec:accesscontrollis
这个标签纸在使用Spring Security ACL 模块时才可以使用。它检测一个用逗号分隔的特
定领域对象的需要权限列表。如果当前用户拥有这些权限的任何一个,标签内容就会被执行。否则,就会被略过。