一、Spring Security
1、什么是 Spring Security?
(1)基本认识
Spring Security 是基于 Spring 框架,用于解决 Web 应用安全性的 一种方案,是一款优秀的权限管理框架。
Web 应用的安全一般关注 用户认证(authentication) 以及 用户授权(authorization) 这两个部分。简单的理解就是 Web 应用 如何确定 你是谁 以及 你能干什么。
【官网地址:】
https://spring.io/projects/spring-security
(2)用户认证(authentication)
用户认证就是 验证某个用户是否为系统中的合法主体,也即该用户是否能登陆系统,通常根据用户名以及密码进行确认。
简单的理解就是 使 Web 应用确定 你是谁。
(3)用户授权(authorization)
用户授权就是 验证某个用户是否有执行某个操作的权限。
简单的理解就是 使 Web 应用确定 你能干什么。
(4)记住几个点
【@EnableWebSecurity】
用于开启 WebSecurity 模式。有时不需要也可以实现相应的功能。
【@EnableGlobalMethodSecurity】
用于开启注解。常见参数为:prePostEnabled、securedEnabled。
【WebSecurityConfigurerAdapter】
用于自定义 Security 策略。
【AuthenticationManagerBuilder】
用于自定义 认证策略。
2、Spring Security 与 Shiro 简单比较一下?
(1)Spring Security
基于 Spring 框架开发,可以与 Spring 无缝整合。
属于重量级的权限控制框架(依赖其他组件、引入各种依赖),提供了全面的权限控制。
(2)Shiro
Apache 的轻量级权限控制框架,不与任何框架捆绑。
使用起来比 Spring Security 简单。
(3)使用
一般来说,使用 Shiro 可以解决大部分项目的问题,且容易操作。
而 SpringBoot 提供了自动化配置方案,通过较少的配置就可以使用 Spring Security。
所以常见组合通常为: SSM + Shiro 或者 SpringBoot / SpringCloud + Spring Security。
3、Spring Security 初体验(SpringBoot + Spring Security)
(1)步骤
【步骤:】
Step1:新建一个 SpringBoot 项目。
Step2:引入 Web 依赖、Spring Security 依赖。
Step3:新建一个 controller 进行测试。
注:
此处仅导入依赖,未进行任何配置,所以显示的都是默认效果。
【效果:】
当 Spring Security 依赖存在时,访问 controller 时会默认跳转到登陆页面。
默认用户名为:user
密码在控制台上可以看到(随机生成)。
(2)新建一个 SpringBoot 项目,并添加 Web、Spring Security 等依赖。
【依赖:】
org.springframework.boot
spring-boot-starter-security
org.springframework.boot
spring-boot-starter-web
(3)新建一个 controller,并简单测试一下 Spring Security。
【controller:】
package com.lyh.demo.springsecurity.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RequestMapping("test")
@RestController
public class TestController {
@GetMapping("/hello")
public String hello() {
return "hello spring security";
}
}
如下图所示,未添加 SpringSecurity 依赖时,访问 controller 没有限制。
而 添加上依赖后,访问 controller 会首先跳转到登录页面,成功登录后才允许访问。
4、Spring Security 再次体验(SSM + Spring Security)
(1)步骤
使用 SSM 时,需要进行一些繁琐的配置,没有 SpringBoot 用起来舒服,
此处简单配置一下,后面介绍仍然以 SpringBoot 为主。
【步骤:】
Step1:创建一个 maven 工程 或者 web 工程(能使用 SpringMVC 即可),可参考:https://www.cnblogs.com/l-y-h/p/12030104.html
Step2:配置 SpringSecurity,并测试。
(2)新建 maven 工程,导入相关依赖
此处使用 tomcat 8 版本启动项目,tomcat 7 启动后在登录时可能会报错。
【依赖】
org.springframework.security
spring-security-core
5.3.4.RELEASE
org.springframework.security
spring-security-web
5.3.4.RELEASE
org.springframework.security
spring-security-config
5.3.4.RELEASE
org.springframework
spring-webmvc
5.2.8.RELEASE
junit
junit
4.11
test
【注意:(tomcat7 版本可能会报如下的错误,更换 tomcat 8 以上版本即可)】
java.lang.NoSuchMethodError: javax.servlet.http.HttpServletRequest.changeSessionId()Ljava/lang/String;
(3)配置基本的 web 环境(Spring 以及 SpringMVC)
【web.xml】
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
contextConfigLocation
classpath:applicationContext.xml
class>org.springframework.web.context.ContextLoaderListenerclass>
springmvcDispatcherServlet
class>org.springframework.web.servlet.DispatcherServletclass>
contextConfigLocation
classpath:dispatcher-servlet.xml
1
springmvcDispatcherServlet
/
characterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
forceRequestEncoding
false
forceResponseEncoding
true
characterEncodingFilter
/*
HiddenHttpMethodFilter
org.springframework.web.filter.HiddenHttpMethodFilter
HiddenHttpMethodFilter
/*
【applicationContext.xml】
【dispatcher-servlet.xml】
(4)配置 SpringSecurity,并新建一个 controller 进行测试
【web.xml 中配置核心过滤器链 springSecurityFilterChain】
springSecurityFilterChain
class>org.springframework.web.filter.DelegatingFilterProxyclass>
springSecurityFilterChain
/*
【新建一个 spring-security.xml 用于进行 Spring Security 相关配置】
【在 web.xml 中导入 spring-security.xml 文件(与导入 applicationContext.xml 类似,也可以在 applicationContext.xml 中通过 标签引入 spring-security.xml)】
contextConfigLocation
classpath:applicationContext.xml
classpath:spring-security.xml
【完整 web.xml 如下:】
contextConfigLocation
classpath:applicationContext.xml
classpath:spring-security.xml
org.springframework.web.context.ContextLoaderListener
springmvcDispatcherServlet
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:dispatcher-servlet.xml
1
springmvcDispatcherServlet
/
characterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
forceRequestEncoding
false
forceResponseEncoding
true
characterEncodingFilter
/*
HiddenHttpMethodFilter
org.springframework.web.filter.HiddenHttpMethodFilter
HiddenHttpMethodFilter
/*
springSecurityFilterChain
org.springframework.web.filter.DelegatingFilterProxy
springSecurityFilterChain
/*
【新建一个 TestController.java 进行测试】
package com.lyh.demo.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RequestMapping("test")
@RestController
public class TestController {
@GetMapping("/hello")
public String hello() {
return "hello spring security";
}
}
如下图所示,未配置 springSecurityFilterChain 时,等同于普通的系统登录。
配置 springSecurityFilterChain 后,在 spring-security.xml 中可以看到,
配置了如下内容:
拦截所有请求,并只允许拥有 ROLE_USER 这个角色的用户才可以登录。
设置了三个用户,tom 为 ROLE_USER 角色,且密码未加密,所以可以正常登陆。
jarry 为 ROLE_ADMIN 角色,没有权限,所以不能正常登陆。
jack 为 ROLE_USER 角色,但密码被加密,所以不能正常登陆。
5、Spring Security 过滤器链
(1)本质
Spring Security 基于 Servlet 过滤器实现的。
默认由 15 个过滤器组成过滤器链(可以通过配置添加、移除过滤器),通过过滤器拦截请求并进行相关操作。
(2)简单了解几个过滤器
【org.springframework.security.web.context.SecurityContextPersistenceFilter】
此过滤器主要是在 SecurityContextRepository 中 保存或者更新 SecurityContext,并交给后续的过滤器操作。
而 SecurityContext 中保存了当前用户认证、权限等信息。
【org.springframework.security.web.csrf.CsrfFilter】
此过滤器用于防止 CSRF 攻击。Spring Security 4.0 开始,默认开启 CSRF 防护,针对 PUT、POST、DELETE 等请求进行防护。
注:
CSRF 指的是 Cross Site Request Forgery,即 跨站请求伪造。
简单理解为:攻击者冒用用户身份去执行操作。
举例:
用户打开浏览器并成功登陆某个网站 A,
此时用户 未登出网站 A,且在同一浏览器中新增一个 Tab 页并访问 网站 B,
而浏览器接收到网站 B 返回的恶意代码后,在用户不知情的情况下携带 cookie 等用户信息向 A 网站发送请求。
网站 A 处理该请求,从而导致网站 B 的恶意代码被执行。
简单理解就是:用户登录一个网站 A,并打开了另一个网站 B,B 网站携带恶意代码 且使用用户身份去访问 网站 A。
XSS 指的是 Cross Site Scripting,即 跨站脚本。
简单理解:攻击者将恶意代码嵌入网站,当用户访问网站时导致 恶意代码被执行。
【org.springframework.security.web.authentication.logout.LogoutFilter】
匹配 URL(默认为 /logout),用于实现用户退出并清除认证信息。
【org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter】
匹配 URL(默认为 /login),用于实现用户登录认证操作(必须为 POST 请求)。
【org.springframework.security.web.authentication.ui.DefaultLoginPageGeneratingFilter】
若没有指定登录认证界面,此过滤器会提供一个默认的界面。
【org.springframework.security.web.authentication.ui.DefaultLogoutPageGeneratingFilter】
若没有指定登出界面,此过滤器会提供一个默认的界面。
【org.springframework.security.web.authentication.AnonymousAuthenticationFilter】
创建一个匿名身份,用于系统的访问。(兼容游客登录模式)
【org.springframework.security.web.access.ExceptionTranslationFilter】
位于整个 springSecurityFilterChain 过滤链后方,用于处理链路中的异常(跳转到指定页面或者返回错误信息)。
【org.springframework.security.web.access.intercept.FilterSecurityInterceptor】
获取资源访问的授权信息,根据 SecurityContext 中存储的用户信息来决定操作是否有权限。
(3)这些过滤器是如何加载进来的?
通过前面 SSM + Spring Security 可以看到,在 web.xml 中配置了名为 springSecurityFilterChain 的过滤器,可以 Debug 看下 DelegatingFilterProxy 加载的流程。
【基本流程:】
Step1:
通过 DelegatingFilterProxy 过滤器的 doFilter() 获取到 FilterChainProxy 过滤器并执行。
Step2:
通过 FilterChainProxy 过滤器的 doFilter() 调用 doFilterInternal() 加载到 过滤器链。
Step3:
doFilterInternal() 内部通过 SecurityFilterChain 接口获取到 过滤器链。
Step4:
SecurityFilterChain 接口实现类为 DefaultSecurityFilterChain。
二、SpringBoot + SpringSecurity 相关操作
1、三种认证方式(设置用户名、密码)
不进行任何 SpringSecurity 配置时,系统默认提供用户名以及密码,但是这种情况肯定不适用于工作场景。那么如何进行 认证呢?
(1)方式一:
通过配置文件 application.properties 或者 application.yml 中直接定义。
不太适用于实际工作场景。
【在 application.properties 中直接定义 用户名、密码】
spring.security.user.name=tom
spring.security.user.password=123456
(2)方式二:
通过配置类的形式。(需要继承 WebSecurityConfigurerAdapter 抽象类)
不太适用于实际工作场景。
【通过配置类的形式:】
package com.lyh.demo.springsecurity.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
/**
* 配置 Spring Security
*/
@Configuration
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
auth.inMemoryAuthentication()
.withUser("jack")
// .password("{noop}" + "123456") // 未配置 PasswordEncoder 时,可以在 密码前拼接上 {noop},防止出错
.password(bCryptPasswordEncoder.encode("123456"))
.roles("admin");
}
/**
* 配置加密类,若不配置,则 bCryptPasswordEncoder.encode() 进行加密时会出错。
* java.lang.IllegalArgumentException: There is no PasswordEncoder mapped for the id "null"
*
* 若不想配置,可以在 设置 password 时,在密码前添加上 {noop}
* @return 加密类
*/
@Bean
PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}
(3)方式三
通过配置类 以及 自定义实现类(实现 UserDetailsService 接口)实现。
适用于工作场景(从数据库中查询出用户信息并认证)。
【步骤一:在配置类中 指定使用 UserDetailsService 接口,并注入其 实现类】
package com.lyh.demo.springsecurity.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
/**
* 配置 Spring Security
*/
@Configuration
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private UserDetailsService userDetailsService;
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
}
@Bean
PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}
【步骤二:编写自定义实现类(实现 UserDetailsService 接口)】
package com.lyh.demo.springsecurity.service;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import java.util.List;
@Service("userDetailsService")
public class MyUserDetailsService implements UserDetailsService {
@Override
public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
// 设置用户权限,若有多个权限可以使用 逗号分隔
List auths = AuthorityUtils.commaSeparatedStringToAuthorityList("admin");
return new User("jarry", new BCryptPasswordEncoder().encode("123456"), auths);
}
}
2、SpringSecurity 中登录认证过程中 的密码加密(BCryptPasswordEncoder)
(1)为什么要了解密码加密?
Spring Security 5.0 以上版本 对于密码处理需要特别注意一下,前面也介绍了,Spring Security 认证时会对密码进行加密,采用 {encodingId}password 的形式设置加密方式。
如果不想密码加密,可以在配置密码时在 密码前拼接上 {noop}, 即 ({noop}password)。
而实际场景中,数据库存储的密码都是非明文存储(即存储的都是加密后的密码),所以有必要了解一下 SpringSecurity 加密相关内容。
【相关的 encodingId 与其 对应的 实体类 如下:】
public static PasswordEncoder createDelegatingPasswordEncoder() {
String encodingId = "bcrypt";
Map encoders = new HashMap();
encoders.put(encodingId, new BCryptPasswordEncoder());
encoders.put("ldap", new LdapShaPasswordEncoder());
encoders.put("MD4", new Md4PasswordEncoder());
encoders.put("MD5", new MessageDigestPasswordEncoder("MD5"));
encoders.put("noop", NoOpPasswordEncoder.getInstance());
encoders.put("pbkdf2", new Pbkdf2PasswordEncoder());
encoders.put("scrypt", new SCryptPasswordEncoder());
encoders.put("SHA-1", new MessageDigestPasswordEncoder("SHA-1"));
encoders.put("SHA-256", new MessageDigestPasswordEncoder("SHA-256"));
encoders.put("sha256", new StandardPasswordEncoder());
encoders.put("argon2", new Argon2PasswordEncoder());
return new DelegatingPasswordEncoder(encodingId, encoders);
}
(2)PasswordEncoder
SpringSecurity 默认需要在容器中存在 PasswordEncoder 实例对象,用于进行密码加密。所以配置 SpringSecurity 时,需要在容器中配置一个 PasswordEncoder Bean 对象(一般使用 BCryptPasswordEncoder 实例对象)。
【在配置类中通过 @Bean 配置一个 PasswordEncoder 的 Bean 对象:】
@Bean
PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
【PasswordEncoder 常用方法:】
String encode(CharSequence rawPassword); // 用于密码加密
boolean matches(CharSequence rawPassword, String encodedPassword); // 用于密码解密,rawPassword 表示待匹配的密码,encodedPassword 表示加密后的密码。
(3)BCryptPasswordEncoder
是最常用的一种密码解析器,其通过 哈希算法 并加上 随机盐(salt)的方式进行密码加密。
密码解密时,根据加密后的数据 A 得到盐值(salt),将待比较数据根据盐值进行一次加密得到 B,如果 B 与 A 是相同的结果,则说明密码是正确的。
密码加密、解密的关键点在于 盐值的计算。
密码加密相关代码如下所示:
【 encode() 加密:】 加密代码如下所示,首先调用 BCrypt.gensalt() 方法计算出 盐值(salt), 然后调用 BCrypt.hashpw() 方法,根据 盐值(salt)进行密码(password)加密。 而在 BCrypt 中的 hashpw() 中,会通过 salt.substring() 截取并得到真实的盐值(real_salt), 通过 B.crypt_raw() 求得一个哈希数组(hashed),通过 encode_base64() 进行加密。 public String encode(CharSequence rawPassword) { if (rawPassword == null) { throw new IllegalArgumentException("rawPassword cannot be null"); } String salt; if (random != null) { salt = BCrypt.gensalt(version.getVersion(), strength, random); } else { salt = BCrypt.gensalt(version.getVersion(), strength); } return BCrypt.hashpw(rawPassword.toString(), salt); } 【BCrypt】 public static String hashpw(String password, String salt) { byte passwordb[]; passwordb = password.getBytes(StandardCharsets.UTF_8); return hashpw(passwordb, salt); } public static String hashpw(byte passwordb[], String salt) { BCrypt B; String real_salt; byte saltb[], hashed[]; char minor = (char) 0; int rounds, off; StringBuilder rs = new StringBuilder(); if (salt == null) { throw new IllegalArgumentException("salt cannot be null"); } int saltLength = salt.length(); if (saltLength < 28) { throw new IllegalArgumentException("Invalid salt"); } if (salt.charAt(0) != '$' || salt.charAt(1) != '2') throw new IllegalArgumentException ("Invalid salt version"); if (salt.charAt(2) == '$') off = 3; else { minor = salt.charAt(2); if ((minor != 'a' && minor != 'x' && minor != 'y' && minor != 'b') || salt.charAt(3) != '$') throw new IllegalArgumentException ("Invalid salt revision"); off = 4; } // Extract number of rounds if (salt.charAt(off + 2) > '$') throw new IllegalArgumentException ("Missing salt rounds"); if (off == 4 && saltLength < 29) { throw new IllegalArgumentException("Invalid salt"); } rounds = Integer.parseInt(salt.substring(off, off + 2)); real_salt = salt.substring(off + 3, off + 25); saltb = decode_base64(real_salt, BCRYPT_SALT_LEN); if (minor >= 'a') // add null terminator passwordb = Arrays.copyOf(passwordb, passwordb.length + 1); B = new BCrypt(); hashed = B.crypt_raw(passwordb, saltb, rounds, minor == 'x', minor == 'a' ? 0x10000 : 0); rs.append("$2"); if (minor >= 'a') rs.append(minor); rs.append("$"); if (rounds < 10) rs.append("0"); rs.append(rounds); rs.append("$"); encode_base64(saltb, saltb.length, rs); encode_base64(hashed, bf_crypt_ciphertext.length * 4 - 1, rs); return rs.toString(); }
密码解密相关代码如下所示:
【matches() 解密:】
解密代码如下所示,首先确保 encodedPassword 是加密后的代码。
然后调用 BCrypt.checkpw() 进行密码匹配。
而 BCrypt 的 checkpw() 中,可以看到其会将待比较的密码 重新进行一次 hashpw() 密码加密。
而此时传入的盐值是 加密的代码,在 hashpw() 方法中会截取出相应的 盐值(real_salt)并用于加密。
加密完成后,再去比较新加密的密码 与 原来加密的密码 是否相同即可。
所以如果待比较的密码 与 加密的密码是相同的,也即相当于 根据相同的 盐值 再加密了一次,加密结果是相同的。
public boolean matches(CharSequence rawPassword, String encodedPassword) {
if (rawPassword == null) {
throw new IllegalArgumentException("rawPassword cannot be null");
}
if (encodedPassword == null || encodedPassword.length() == 0) {
logger.warn("Empty encoded password");
return false;
}
if (!BCRYPT_PATTERN.matcher(encodedPassword).matches()) {
logger.warn("Encoded password does not look like BCrypt");
return false;
}
return BCrypt.checkpw(rawPassword.toString(), encodedPassword);
}
【BCrypt】
public static boolean checkpw(String plaintext, String hashed) {
return equalsNoEarlyReturn(hashed, hashpw(plaintext, hashed));
}
static boolean equalsNoEarlyReturn(String a, String b) {
return MessageDigest.isEqual(a.getBytes(StandardCharsets.UTF_8), b.getBytes(StandardCharsets.UTF_8));
}
3、从数据库中查询用户信息并认证(MyBatis-Plus + MySQL 8 )
(1)建表(SQL)
MyBatis-Plus 使用可以参考:https://www.cnblogs.com/l-y-h/p/12859477.html
配置 SpringSecurity 时需要配置 使用密码加密,
若不使用加密,则需在设置密码时在密码前拼接上 {noop}。
若使用加密,则使用 BCryptPasswordEncoder 的 encode() 方法对其进行加密。
若数据库存储的已经是 BCryptPasswordEncoder 加密后的数据,不用再次加密。
此处为了方便理解,存储密码时均使用 明文存储。
【建表 SQL :】
DROP DATABASE IF EXISTS testSpringSecurity;
CREATE DATABASE testSpringSecurity;
USE testSpringSecurity;
DROP TABLE IF EXISTS users;
CREATE TABLE users
(
id BIGINT(20) PRIMARY KEY AUTO_INCREMENT COMMENT '主键ID',
name VARCHAR(30) NOT NULL COMMENT '姓名',
password VARCHAR(64) NOT NULL COMMENT '密码',
role VARCHAR(20) NOT NULL COMMENT '角色'
);
INSERT INTO users (name, password, role) VALUES
('tom', '123456', 'user'),
('jarry', '123456', 'admin'),
('jack', '123456', 'ROLE_USER');
(2)引入 MyBatis-Plus 与 MySQL 相关依赖
【依赖:】
com.baomidou
mybatis-plus-boot-starter
3.3.1.tmp
mysql
mysql-connector-java
8.0.18
org.projectlombok
lombok
1.18.10
(3)配置 MyBatis-Plus 以及 MySQL 数据源信息
【数据源信息】
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: 123456
url: jdbc:mysql://localhost:3306/testSpringSecurity?useUnicode=true&characterEncoding=utf8
(4)编写 数据表对应的 实体类,以及相应的 mapper 或者 service(用于操作数据库)
【实体类:】
package com.lyh.demo.springsecurity.entity;
import lombok.Data;
@Data
public class Users {
private Long id;
private String name;
private String password;
private String role;
}
【Mapper:】
package com.lyh.demo.springsecurity.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.lyh.demo.springsecurity.entity.Users;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Service;
@Mapper
@Service
public interface UsersMapper extends BaseMapper {
}
(5)结合 SpringSecurity 进行安全验证。
通过前面分析,添加上 SpirngSecurity 配置类后,会执行 loadUserByUsername() 方法将需要认证的用户信息加载到当前认证系统中,所以在此添加 查询数据库的逻辑即可。
首先根据用户名 在数据库中 查询出相应的 用户、密码 并封装到 实体类中,并将此时的用户、密码、角色等加入到 当前认证系统中。然后再根据 输入的用户名、密码 进行验证。
【修改 MyUserDetailsService 中 loadUserByUsername() 代码:(改为从数据库中获取用户)】
package com.lyh.demo.springsecurity.service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lyh.demo.springsecurity.entity.Users;
import com.lyh.demo.springsecurity.mapper.UsersMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import java.util.List;
@Service("userDetailsService")
public class MyUserDetailsService implements UserDetailsService {
@Autowired
private UsersMapper usersMapper;
@Override
public UserDetails loadUserByUsername(String name) throws UsernameNotFoundException {
// 定义查询条件,根据用户名 从数据库查询 对应的 用户、密码、角色
QueryWrapper queryWrapper = new QueryWrapper<>();
queryWrapper.eq("name", name);
Users user = usersMapper.selectOne(queryWrapper);
// 用户不存在时,直接抛异常
if (user == null) {
throw new UsernameNotFoundException("用户不存在");
}
// 用户存在时,把 用户、密码、角色 加入到当前认证系统中
List auths = AuthorityUtils.commaSeparatedStringToAuthorityList(user.getRole());
// 将数据库中的密码进行 加密
return new User(user.getName(), new BCryptPasswordEncoder().encode(user.getPassword()), auths);
// return new User(user.getName(), user.getPassword(), auths); // 若数据库密码已经加密过,直接使用即可
}
}
4、自定义页面(不使用默认页面)以及 页面跳转、页面访问权限控制
(1)自定义页面
在前面与 数据库 交互的基础上,添加如下代码。
【登录页面:(login.html)】
Login
Please sign in
for="username">Username
for="password" class="sr-only">Password
Sign in
【403 页面:】
403
403
【在 TestController 中添加一个 处理错误的逻辑:】
package com.lyh.demo.springsecurity.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RequestMapping("test")
@RestController
public class TestController {
@GetMapping("/hello")
public String hello() {
return "hello spring security";
}
@PostMapping("/error")
public String error() {
return "login error";
}
}
(2)编写配置类,配置页面跳转规则
在配置类中,重写 configure() 方法,并通过 formLogin() 方法设置相关页面。
【配置类中重写 configure() 方法:】
package com.lyh.demo.springsecurity.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
/**
* 配置 Spring Security
*/
@Configuration
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private UserDetailsService userDetailsService;
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
}
@Override
protected void configure(HttpSecurity http) throws Exception {
/**
* csrf() 表示开启 csrf 防护。
* disable() 表示关闭 csrf 防护。
*/
http.csrf().disable();
/**
* formLogin() 用于自定义表单登录。
* loginPage() 用于自定义登录页面。
* defaultSuccessUrl() 登录成功后 跳转的路径。
* loginProcessingUrl() 表单提交的 action 地址(默认为 /login,修改后,对应的表单 action 也要修改),由系统提供 UsernamePasswordAuthenticationFilter 过滤器拦截并处理。
* usernameParameter() 用于自定义表单提交的用户参数名,默认为 username,修改后,对应的表单参数也要修改。
* passwordParameter() 用于自定义表单提交的用户密码名,默认为 password,修改后,对应的表单参数也要修改。
* failureForwardUrl() 用于自定义表单提交失败后 重定向地址,可用于前后端分离中,指向某个 controller,注意使用 POST 处理。
*/
http.formLogin()
.loginPage("/login.html")
.loginProcessingUrl("/login")
.defaultSuccessUrl("/test/hello")
//.usernameParameter("name")
//.passwordParameter("pwd")
.failureForwardUrl("/test/error")
;
/**
* authorizeRequests() 用于 开启认证,基于 HttpServletRequest 对 url 进行身份控制并授权访问。
* antMatchers() 用于匹配 url。
* permitAll() 用于允许任何人访问该 url。
* hasAuthority() 用于指定 具有某种权限的 人才能访问 url。
* hasAnyAuthority() 用于指定 多个权限 进行访问,多个权限间使用逗号分隔。
*
* hasRole() 写法与 hasAuthority() 类似,但是其会在 角色前 拼接上 ROLE_,使用时需要注意。
* hasAnyRole() 写法与 hasAnyAuthority() 类似,同样会在 角色前 拼接上 ROLE_。
*
* 使用时 hasAuthority()、hasAnyAuthority() 或者 hasAnyRole()、hasAnyAuthority() 任选一对即可,同时使用四种可能会出现问题。
*/
http.authorizeRequests()
.antMatchers("/test/hello").hasAuthority("user")
//.antMatchers("/test/hello").hasAnyRole("USER,GOD")
//.antMatchers("/test/hello").hasRole("GOD")
.antMatchers("/test/hello").hasAnyAuthority("user,admin")
.antMatchers("/login", "/test/error").permitAll();
/**
* 自定义 403 页面
*/
http.exceptionHandling().accessDeniedPage("/403.html");
}
@Bean
PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}
如下图所示,tom 的角色为 user、jarry 的角色为 admin,jack 的角色为 ROLE_USER。
只允许 user、admin 角色能够访问 /test/hello,即 tom、jarry 可以成功访问系统,而 jack 访问时会跳转到 403 页面,若 用户名 或者 密码输入错误时,将会跳转到 /test/error 画面。
5、了解几个注解
为了简化开发,可以使用注解进行相关操作(操作不太灵活,慎用)。
(1)@Secured
添加在 方法上,并可以指定用户角色,作用是只允许指定的用户角色去访问 该方法。
【使用步骤一:】
在配置类上,通过 @EnableGlobalMethodSecurity(securedEnabled = true) 开启注解。
【使用步骤二:】
在方法上添加注解 @Secured,并指定 角色,角色前缀要为 ROLE_。
@GetMapping("/testSecured")
@Secured({"ROLE_USER"})
public String testSecured() {
return "success";
}
注:
由于 角色需要使用 ROLE_ 为前缀,所以数据库存储的 角色需要以 ROLE_ 为前缀 或者 设置权限时手动加上 ROLE_。
(2)@PreAuthorize
添加在 方法上,并可以指定用户角色,作用是只允许指定的用户角色去访问 该方法。
在进入方法之前 会进行 校验,校验通过后才能执行方法。
【使用步骤一:】
在配置类上,通过 @EnableGlobalMethodSecurity(prePostEnabled = true) 开启注解。
【使用步骤二:】
在方法上添加 @PreAuthorize 注解,并指定角色,角色的指定可以使用 Spring 表达式。
@GetMapping("/testSecured")
@PreAuthorize("hasAnyAuthority('user', 'ROLE_USER')")
public String testSecured() {
return "success";
}
(3)@PostAuthorize
添加在 方法上,并可以指定用户角色,作用是只允许指定的用户角色去访问 该方法。
在进入方法之后 会进行 校验。不管有没有权限,都会执行方法,适合带有返回值的校验。
【使用步骤一:】
在配置类上,通过 @EnableGlobalMethodSecurity(prePostEnabled = true) 开启注解。
【使用步骤二:】
在方法上添加 @PostAuthorize 注解,并指定角色,角色的指定可以使用 Spring 表达式。
@GetMapping("/testSecured")
@PostAuthorize("hasAuthority('user')")
public String testSecured() {
System.out.println("不管有没有权限,我都会执行");
return "success";
}
6、用户注销操作
(1)自定义一个登录成功页面,并添加一个 退出链接。
【登录成功页面 success.html】
success
Success
注销
(2)编写配置类,修改页面退出规则。
此处为了跳转到 success.html 页面,还需要 通过 http.formLogin().defaultSuccessUrl() 去指定页面。
【添加退出规则:】
http.formLogin()
.loginPage("/login.html")
.loginProcessingUrl("/login")
.defaultSuccessUrl("/success.html")
//.usernameParameter("name")
//.passwordParameter("pwd")
.failureForwardUrl("/test/error")
;
/**
* logout() 用于自定义退出逻辑。
* logoutUrl() 用于拦截退出请求,默认为 /logout。
* logoutSuccessUrl() 用于自定义退出成功后,跳转的页面。
*/
http.logout()
.logoutUrl("/logout")
.logoutSuccessUrl("/login.html")
;
如下图所示,tom、jarry 可以访问 /test/hello,jack 不可以访问,所以当使用 tom、jarry 登录时可以成功登陆,jack 会显示 403,一旦点击注销后,需要再次进行登录才能继续访问 /test/hello。
7、记住我
(1)工作流程
记住我 功能上指的是 用户通过浏览器登录一次网站后,关闭浏览器并再次访问网站时,可以不用再次登录而直接进行相关操作。
【工作流程:】
第一次通过浏览器登录系统时:
首先 用户名、密码 会被 UsernamePasswordAuthenticationFilter 过滤器拦截,并进行认证。
认证通过后,会调用 RememberMeServices 生成 token,并将 token 写入数据库 以及 浏览器 cookie 中。
第二次通过浏览器登录系统时:
直接携带 cookie 访问,会被 RememberMeAuthenticationFilter 过滤器拦截,根据 cookie 读取出 token 信息。
从数据库中查找出 对应的 token 并比较,若相同,则可以登录系统,否则跳转到登录页面。
工作流程见下图(图片来源于网络):
(2)基本实现:
由于 token 需要存储在 数据库中,所以需要配置数据源信息,并操作,而 SpringSecurity 中已经提供了相关操作类,只需在配置类中配置即可。
【配置如下:(注入 DataSource,并配置 PersistentTokenRepository 交给 Spring 管理)】
@Autowired
private DataSource dataSource;
@Bean
public PersistentTokenRepository persistentTokenRepository() {
JdbcTokenRepositoryImpl jdbcTokenRepository = new JdbcTokenRepositoryImpl();
// 设置数据源
jdbcTokenRepository.setDataSource(dataSource);
// 自动建表
// jdbcTokenRepository.setCreateTableOnStartup(true);
return jdbcTokenRepository;
}
【完整配置类:(通过 http.rememberMe() 配置)】
package com.lyh.demo.springsecurity.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.rememberme.JdbcTokenRepositoryImpl;
import org.springframework.security.web.authentication.rememberme.PersistentTokenRepository;
import javax.sql.DataSource;
/**
* 配置 Spring Security
*/
@Configuration
@EnableWebSecurity
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private UserDetailsService userDetailsService;
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
}
@Autowired
private DataSource dataSource;
/**
* 默认使用 PersistentTokenRepository 的子类 InMemoryTokenRepositoryImpl 将 token 放在内存中,
* 可以使用子类 JdbcTokenRepositoryImpl 将 token 持久化到 数据库中。
* 注:
*
* jdbcTokenRepository.setCreateTableOnStartup(true); 等同于下面 SQL,
* 若不手动创建,可以使用代码自动创建,但是执行一次后需要将其注释掉。
*
* create table persistent_logins (
* username varchar(64) not null,
* series varchar(64) primary key,
* token varchar(64) not null,
* last_used timestamp not null
* )
*/
@Bean
public PersistentTokenRepository persistentTokenRepository() {
JdbcTokenRepositoryImpl jdbcTokenRepository = new JdbcTokenRepositoryImpl();
// 设置数据源
jdbcTokenRepository.setDataSource(dataSource);
// 自动建表
// jdbcTokenRepository.setCreateTableOnStartup(true);
return jdbcTokenRepository;
}
@Override
protected void configure(HttpSecurity http) throws Exception {
/**
* rememberMe() 用于实现记住我功能。
* tokenRepository() 设置数据访问层。
* userDetailsService() 设置 userDetailsService。
* tokenValiditySeconds() 设置过期时间。
* rememberMeParameter() 自定义参数名,默认为 remember-me
*/
http.rememberMe()
.tokenRepository(persistentTokenRepository())
.userDetailsService(userDetailsService)
//.rememberMeParameter("remember")
.tokenValiditySeconds(24 * 60 * 60);
/**
* csrf() 表示开启 csrf 防护。
* disable() 表示关闭 csrf 防护。
*/
http.csrf().disable();
/**
* formLogin() 用于自定义表单登录。
* loginPage() 用于自定义登录页面。
* defaultSuccessUrl() 登录成功后 跳转的路径。
* loginProcessingUrl() 表单提交的 action 地址(默认为 /login,修改后,对应的表单 action 也要修改),由系统提供 UsernamePasswordAuthenticationFilter 过滤器拦截并处理。
* usernameParameter() 用于自定义表单提交的用户参数名,默认为 username,修改后,对应的表单参数也要修改。
* passwordParameter() 用于自定义表单提交的用户密码名,默认为 password,修改后,对应的表单参数也要修改。
* failureForwardUrl() 用于自定义表单提交失败后 重定向地址,可用于前后端分离中,指向某个 controller。
*/
http.formLogin()
.loginPage("/login.html")
.loginProcessingUrl("/login")
.defaultSuccessUrl("/success.html")
//.usernameParameter("name")
//.passwordParameter("pwd")
.failureForwardUrl("/test/error")
;
/**
* logout() 用于自定义退出逻辑。
* logoutUrl() 用于拦截退出请求,默认为 /logout。
* logoutSuccessUrl() 用于自定义退出成功后,跳转的页面。
*/
http.logout()
.logoutUrl("/logout")
.logoutSuccessUrl("/login.html")
;
/**
* authorizeRequests() 用于 开启认证,基于 HttpServletRequest 对 url 进行身份控制并授权访问。
* antMatchers() 用于匹配 url。
* permitAll() 用于允许任何人访问该 url。
* hasAuthority() 用于指定 具有某种权限的 人才能访问 url。
* hasAnyAuthority() 用于指定 多个权限 进行访问,多个权限间使用逗号分隔。
*
* hasRole() 写法与 hasAuthority() 类似,但是其会在 角色前 拼接上 ROLE_,使用时需要注意。
* hasAnyRole() 写法与 hasAnyAuthority() 类似,同样会在 角色前 拼接上 ROLE_。
*
* 使用时 hasAuthority()、hasAnyAuthority() 或者 hasAnyRole()、hasAnyAuthority() 任选一对即可,同时使用四种可能会出现问题。
*/
http.authorizeRequests()
.antMatchers("/test/hello").hasAuthority("user")
//.antMatchers("/test/hello").hasAnyRole("USER,GOD")
//.antMatchers("/test/hello").hasRole("GOD")
.antMatchers("/test/hello").hasAnyAuthority("user,admin")
.antMatchers("/login", "/test/error").permitAll();
/**
* 自定义 403 页面
*/
http.exceptionHandling().accessDeniedPage("/403.html");
}
@Bean
PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}
如下图所示,点击 记住我,并登陆后,会在浏览器 cookie 以及 数据库中 各存放一份 token,关闭浏览器并再次登录时,无需重新登录,会自动检测 cookie 中的 token 值是否正确,若相同则可以正常登陆。注销时,浏览器 token 以及 数据库的 token 会一起注销。
(3)源码分析:
Step1:
第一次通过浏览器登录系统时,首先会被 UsernamePasswordAuthenticationFilter 过滤器拦截,认证通过后,会在 AbstractAuthenticationProcessingFilter 抽象类的 successfulAuthentication() 方法中 进行 token 的处理。
【UsernamePasswordAuthenticationFilter 继承 AbstractAuthenticationProcessingFilter:】
public class UsernamePasswordAuthenticationFilter extends AbstractAuthenticationProcessingFilter {}
【AbstractAuthenticationProcessingFilter 的 doFilter() 中 调用了 successfulAuthentication() 方法:】
public abstract class AbstractAuthenticationProcessingFilter extends GenericFilterBean
implements ApplicationEventPublisherAware, MessageSourceAware {
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
throws IOException, ServletException {
...
successfulAuthentication(request, response, chain, authResult);
}
}
Step2:
AbstractAuthenticationProcessingFilter 中定义了 RememberMeServices 接口,在 successfulAuthentication() 方法中 会调用 RememberMeServices 接口的 loginSuccess() 方法。
【调用 RememberMeServices 的 loginSuccess() 方法:】
private RememberMeServices rememberMeServices = new NullRememberMeServices();
protected void successfulAuthentication(HttpServletRequest request,
HttpServletResponse response, FilterChain chain, Authentication authResult)
throws IOException, ServletException {
...
rememberMeServices.loginSuccess(request, response, authResult);
}
Step3:
RememberMeServices 接口的 loginSuccess() 方法 由子类 AbstractRememberMeServices 实现,loginSuccess() 会先检测是否存在 记住我 的功能,默认参数名为 remember-me,若表单中不存在 或者 为 false 时,会直接返回。为 true 时,会执行 onLoginSuccess() 方法。
【调用 AbstractRememberMeServices 的 loginSuccess() 方法:】
public final void loginSuccess(HttpServletRequest request,
HttpServletResponse response, Authentication successfulAuthentication) {
if (!rememberMeRequested(request, parameter)) {
logger.debug("Remember-me login not requested.");
return;
}
onLoginSuccess(request, response, successfulAuthentication);
}
Step4:
onLoginSuccess() 方法由 AbstractRememberMeServices 的子类 PersistentTokenBasedRememberMeServices 去实现。向数据库中 添加 token 以及 向 cookie 中添加 token。
【调用 PersistentTokenBasedRememberMeServices 的 onLoginSuccess() 方法:】
protected void onLoginSuccess(HttpServletRequest request,
HttpServletResponse response, Authentication successfulAuthentication) {
String username = successfulAuthentication.getName();
logger.debug("Creating new persistent login for user " + username);
PersistentRememberMeToken persistentToken = new PersistentRememberMeToken(
username, generateSeriesData(), generateTokenData(), new Date());
try {
tokenRepository.createNewToken(persistentToken);
addCookie(persistentToken, request, response);
}
catch (Exception e) {
logger.error("Failed to save persistent token ", e);
}
}
Step5:
关闭浏览器,再次登录时,由 RememberMeAuthenticationFilter 过滤器拦截请求,在其 doFilter() 方法中 调用 RememberMeServices 接口的 autoLogin() 方法进行处理。
【RememberMeAuthenticationFilter 的 】
public class RememberMeAuthenticationFilter extends GenericFilterBean implements
ApplicationEventPublisherAware {
private RememberMeServices rememberMeServices;
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
throws IOException, ServletException {
...
Authentication rememberMeAuth = rememberMeServices.autoLogin(request,response);
...
}
}
Step6:
RememberMeServices 接口的 autoLogin() 方法由 AbstractRememberMeServices 子类实现,其根据 cookie 值解析出相应的 token。并根据 token 从数据库中查询用户,并验证用户是否合法。
public final Authentication autoLogin(HttpServletRequest request,
HttpServletResponse response) {
String rememberMeCookie = extractRememberMeCookie(request);
...
String[] cookieTokens = decodeCookie(rememberMeCookie);
user = processAutoLoginCookie(cookieTokens, request, response);
userDetailsChecker.check(user);
}
Step7:
调用 processAutoLoginCookie() 方法根据 token 从数据库中查询出用户。
protected UserDetails processAutoLoginCookie(String[] cookieTokens,
HttpServletRequest request, HttpServletResponse response) {
final String presentedSeries = cookieTokens[0];
final String presentedToken = cookieTokens[1];
PersistentRememberMeToken token = tokenRepository.getTokenForSeries(presentedSeries);
if (!presentedToken.equals(token.getTokenValue())) {
}
...
return getUserDetailsService().loadUserByUsername(token.getUsername());
}
Original: https://www.cnblogs.com/l-y-h/p/14010034.html
Author: 累成一条狗
Title: 学习一下 Spring Security
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/598424/
转载文章受原作者版权保护。转载请注明原作者出处!