Springsecurity 简介
Springsecurity 是针对Spring 项目的安全框架,也是Springboot 底层安全模块默认的技术选型,他可以实现强大的WEB 安全控制,对于安全控制,我们仅需要引入 Spring-starter-security 模块
主要的几个类
@WebSecurityConfigurerAdapter :自定义security 策略
@AuthenticationManagerBuider :自定义认证策略
@EnableWebSecurity 开启WebSecurity 模式,@Enable XXX 开启某个功能
Springsecurity 的两个主要目标是“认证” 和 “授权”(访问控制)
这个概念是互通的不是只在Springsecurity 中存在。
准备工作,导入依赖
<!--引入thymeleaf依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- security依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
几个简单的页面 目录结构
页面的效果
写自己的config 类 Springboot 帮我们集成了很多,只需要写一个类,继承WebSecurityConfigurerAdapter 加入注解 @EnableWebSecurity
package com.jj.config;
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.WebSecurityConfiguration;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@EnableWebSecurity
public class Securityconfig extends WebSecurityConfigurerAdapter {
// 重写
@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();
}
}
效果,当我们点击
会退回到登录页面,因为没有权限
相当于 Spring 的aop 面向切面。
设置登录的账号密码权限
// 重写登录的账号密码
// 可以是数据库的,我没有连接数据库,就用的内存的
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication()
// 设置fjj 可以看 vip2 ,vip3
.withUser("fengjiaojaio").password("123456").roles("vip2","vip3")
// 设置憨憨 只能看vip1
.and() .withUser("憨憨").password("285").roles("vip1");
}
版本问题应该会报错,会说密码不安全。可能也是为了大家的密码着想吧,啊哈哈哈哈哈
java.lang.IllegalArgumentException: There is no PasswordEncoder mapped for the id "null"
at org.springframework.security.crypto.password.DelegatingPasswordEncoder$UnmappedIdPasswordEncoder.matches(DelegatingPasswordEncoder.java:254) ~[spring-security-core-5.4.2.jar:5.4.2]
at org.springframework.security.crypto.password.DelegatingPasswordEncoder.matches(DelegatingPasswordEncoder.java:202) ~[spring-security-core-5.4.2.jar:5.4.2]
** 解决方式一:如果不想设置 密码加密编码的话,可以把版本降低到2.1.X 的 **
** 解决方式二:加上密码的加密 **
// 重写登录的账号密码
// 可以是数据库的,我没有连接数据库,就用的内存的
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
// 设置fjj 可以看 vip2 ,vip3
.withUser("fengjiaojaio").password(new BCryptPasswordEncoder().encode("123456")).roles("vip2","vip3")
// 设置憨憨 只能看vip1
.and() .withUser("憨憨").password(new BCryptPasswordEncoder().encode("285")).roles("vip1");
}
效果
会说没有权限!!
如果连接数据库的话,官方文档给的 ,注入数据源即可!!
登录后会有cookie 所以需要开启消除登录的 ,加上这一句话
在首页写上注销的按钮
这里这个mapper 是Spring security 写好的
点进去
下载他的源代码,可以看到注释
效果
有友好的提示
如果不想跳到登录界面,也可以跳到指定的url
**
功能!!