Spring Security
链式编程
狂神springsecurity练习资源地址
https://gitee.com/ENNRIAAA/spring-security-material?_from=gitee_search
简介
百度百科: Spring Security是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架。它提供了一组可以在Spring应用上下文中配置的Bean,充分利用了Spring IoC,DI(控制反转Inversion of Control ,DI:Dependency Injection 依赖注入)和AOP(面向切面编程)功能,为应用系统提供声明式的安全访问控制功能,减少了为企业系统安全控制编写大量重复代码的工作
Spring Security是针对Spring项目的安全框架,也是Spring Boot底层安全模块默认的技术选型,他可以实现强大的Web安全控制,对于安全控制,我们仅需要引入spring-boot-starter-security模块,进行少量的配置,即可实现强大的安全管理!
记住几个类:
- WebSecurityConfigurerAdapter:自定义Security策略
- AuthenticationManagerBuilder:自定义认证策略
- @EnableWebSecurifiy:开启WebSecurity模式
Spring Security的两个主要目标是“认证"和“授权”(访问控制)。
“认证”(Authentication)
“授权”(Authorization)
这个概念是通用的,而不是只在Spring Security中存在。
spring security 的核心功能主要包括:
- 认证 (你是谁)
- 授权 (你能干什么)
- 攻击防护 (防止伪造身份)
参考文档
参考官网: https://spring.io/projects/spring-security,,查看我们自己项目中的版本,找到对应的帮助文档:https://docs.spring.io/spring-security/site/docs/5.2.0.RELEASE/reference/htmlsingle
15.1 Hello Web Security Java Configuration
例子:自定义一个spring security
(根据spring security官方文档进行操作)
授权:重写WebSecurityConfigurerAdapter 中的protected void configure(HttpSecurity http)方法
@EnableWebSecurity
public class Config extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.apply(customDsl())
.flag(true)
.and()
...;
}
认证:重写WebSecurityConfigurerAdapter 中的protected void configure(AuthenticationManagerBuilder auth)方法
- 在内存中认证
//WebSecurityConfigurerAdapter下的源码 关于在内存中认证
* protected void configure(AuthenticationManagerBuilder auth) {
* auth
* // enable in memory based authentication with a user named
* // "user" and "admin"
* .inMemoryAuthentication().withUser("user").password("password").roles("USER").and()
* .withUser("admin").password("password").roles("USER", "ADMIN");
* }
- 在数据库中认证
security登入和注销
//请求授权的规则
//链式编程
@Override
protected void configure(HttpSecurity http) throws Exception {
//首页所有能都能访问,功能页只有有权限的人才可以访问
//请求授权的规则
http.authorizeRequests()
.antMatchers("/").permitAll()
.antMatchers("/level1/**").hasRole("vip1")
.antMatchers("/level2/**").hasRole("vip2")
.antMatchers("/level3/**").hasRole("vip3");
//没有权限,默认到登入页面
// http.formLogin(); //进入一个默认的登入页面
http.formLogin().loginPage("/toLogin");
//防止网站攻击
http.csrf().disable(); //关闭csrf服务,登入失败可能存在的原因
//注销
// http.logout();
http.logout().logoutSuccessUrl("/");//跳转到注销后的地址
// 开启记住我功能 cookie 存了一个value值在cookie中 默认保存两周
http.rememberMe().rememberMeParameter("remember");
}
默认登入跳转 http.formLogin()
http.formLogin()
//源码 默认登入跳转
* The most basic configuration defaults to automatically generating a login page at
* the URL "/login", redirecting to "/login?error" for authentication failure. The
* details of the login page can be found on
//认证
//认证,springboot 2.1.x可以直接使用
//高版本会报----->密码编码错误 PasswordEncoder
//在 spring security5.0+ 新增很多加密方式
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
//在内存中的权限认证
//passwordEncoder(new BCryptPasswordEncoder()) 密码加密
auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
.withUser("admin").password(new BCryptPasswordEncoder().encode("123")).roles("vip1","vip2","vip3")
.and()
.withUser("user").password(new BCryptPasswordEncoder().encode("123")).roles("vip1")
.and()
.withUser("guest").password(new BCryptPasswordEncoder().encode("123")).roles("vip2");
//在jdbc中认证
// auth.jdbcAuthentication().withUser()
}
password加密方法 //passwordEncoder(new BCryptPasswordEncoder())
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-NVBhLq24-1627722937756)(C:\Users\黄小帅\AppData\Roaming\Typora\typora-user-images\image-20210722170454901.png)]
Themeleaf 和security整合
高版本的springboot需要使用thymeleaf-extras-springsecurity5
SpringBoot整合SpringSecurity时sec没有提示解决方法
首先看你的SpringSecurity的版本是4.X还是5.X的版本
4.X
<!--引入security的依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<!--引入thymeleaf和security整合的依赖,实现权限控制-->
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity4</artifactId>
</dependency>
在html页面上导入名称空间
<!--引入thymeleaf-SpringSecurity的名称空间-->
<html xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4">
5.X
<!--引入security的依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<!--引入thymeleaf和security整合的依赖,实现权限控制-->
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity5</artifactId>
</dependency>
在html页面上导入名称空间
<!--引入thymeleaf-SpringSecurity的名称空间-->
<html xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
ifactId>
在html页面上导入名称空间
```java
<!--引入thymeleaf-SpringSecurity的名称空间-->
<html xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/extras/spring-security">