SprngSecurity实战实例,JWT Token登录认证&权限控制
这个在暑假就写过,上学以后重新再去写一遍发现困难比较多,于是准备写一篇记录一下。
tips: 源码在文章末尾,sql文件在源码resource文件夹下的sql文件夹里
需求
想用springsecurity实现JWT Token登录认证,实现动态菜单生成,权限控制,登出(用户注销)。
认证
准备工作
maven依赖
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.9.1</version>
</dependency>
<!--redis依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>添加Redis相关配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.TypeFactory;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.SerializationException;
import com.alibaba.fastjson.parser.ParserConfig;
import org.springframework.util.Assert;
import java.nio.charset.Charset;
/**
* Redis使用FastJson序列化
*
* @author sg
*/
public class FastJsonRedisSerializer<T> implements RedisSerializer<T>
{
public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
private Class<T> clazz;
static
{
ParserConfig.getGlobalInstance().setAutoTypeSupport(true);
}
public FastJsonRedisSerializer(Class<T> clazz)
{
super();
this.clazz = clazz;
}
public byte[] serialize(T t) throws SerializationException
{
if (t == null)
{
return new byte[0];
}
return JSON.toJSONString(t, SerializerFeature.WriteClassName).getBytes(DEFAULT_CHARSET);
}
public T deserialize(byte[] bytes) throws SerializationException
{
if (bytes == null || bytes.length <= 0)
{
return null;
}
String str = new String(bytes, DEFAULT_CHARSET);
return JSON.parseObject(str, clazz);
}
protected JavaType getJavaType(Class<?> clazz)
{
return TypeFactory.defaultInstance().constructType(clazz);
}
}响应类
1 | public class RespBean { |
工具类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;
import java.util.Date;
import java.util.UUID;
/**
* JWT工具类
*/
public class JwtUtil {
//有效期为
public static final Long JWT_TTL = 60 * 60 *1000L;// 60 * 60 *1000 一个小时
//设置秘钥明文
public static final String JWT_KEY = "lcdzzz";
public static String getUUID(){
String token = UUID.randomUUID().toString().replaceAll("-", "");
return token;
}
/**
* 生成jtw
* @param subject token中要存放的数据(json格式)
* @return
*/
public static String createJWT(String subject) {
JwtBuilder builder = getJwtBuilder(subject, null, getUUID());// 设置过期时间
return builder.compact();
}
/**
* 生成jtw
* @param subject token中要存放的数据(json格式)
* @param ttlMillis token超时时间
* @return
*/
public static String createJWT(String subject, Long ttlMillis) {
JwtBuilder builder = getJwtBuilder(subject, ttlMillis, getUUID());// 设置过期时间
return builder.compact();
}
private static JwtBuilder getJwtBuilder(String subject, Long ttlMillis, String uuid) {
SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.HS256;
SecretKey secretKey = generalKey();
long nowMillis = System.currentTimeMillis();
Date now = new Date(nowMillis);
if(ttlMillis==null){
ttlMillis=JwtUtil.JWT_TTL;
}
long expMillis = nowMillis + ttlMillis;
Date expDate = new Date(expMillis);
return Jwts.builder()
.setId(uuid) //唯一的ID
.setSubject(subject) // 主题 可以是JSON数据
.setIssuer("lcdzzz") // 签发者
.setIssuedAt(now) // 签发时间
.signWith(signatureAlgorithm, secretKey) //使用HS256对称加密算法签名, 第二个参数为秘钥
.setExpiration(expDate);
}
/**
* 创建token
* @param id
* @param subject
* @param ttlMillis
* @return
*/
public static String createJWT(String id, String subject, Long ttlMillis) {
JwtBuilder builder = getJwtBuilder(subject, ttlMillis, id);// 设置过期时间
return builder.compact();
}
public static void main(String[] args) throws Exception {
// String jwt = createJWT("2123");
Claims claims = parseJWT("eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiIyOTY2ZGE3NGYyZGM0ZDAxOGU1OWYwNjBkYmZkMjZhMSIsInN1YiI6IjIiLCJpc3MiOiJzZyIsImlhdCI6MTYzOTk2MjU1MCwiZXhwIjoxNjM5OTY2MTUwfQ.NluqZnyJ0gHz-2wBIari2r3XpPp06UMn4JS2sWHILs0");
String subject = claims.getSubject();
System.out.println(subject);
// System.out.println(claims);
}
/**
* 生成加密后的秘钥 secretKey
* @return
*/
public static SecretKey generalKey() {
byte[] encodedKey = Base64.getDecoder().decode(JwtUtil.JWT_KEY);
SecretKey key = new SecretKeySpec(encodedKey, 0, encodedKey.length, "AES");
return key;
}
/**
* 解析
*
* @param jwt
* @return
* @throws Exception
*/
public static Claims parseJWT(String jwt) throws Exception {
SecretKey secretKey = generalKey();
return Jwts.parser()
.setSigningKey(secretKey)
.parseClaimsJws(jwt)
.getBody();
}
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundSetOperations;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;
import java.util.*;
import java.util.concurrent.TimeUnit;
public class RedisCache
{
private RedisTemplate redisTemplate;
/**
* 缓存基本的对象,Integer、String、实体类等
*
* @param key 缓存的键值
* @param value 缓存的值
*/
public <T> void setCacheObject(final String key, final T value)
{
redisTemplate.opsForValue().set(key, value);
}
/**
* 缓存基本的对象,Integer、String、实体类等
*
* @param key 缓存的键值
* @param value 缓存的值
* @param timeout 时间
* @param timeUnit 时间颗粒度
*/
public <T> void setCacheObject(final String key, final T value, final Integer timeout, final TimeUnit timeUnit)
{
redisTemplate.opsForValue().set(key, value, timeout, timeUnit);
}
/**
* 设置有效时间
*
* @param key Redis键
* @param timeout 超时时间
* @return true=设置成功;false=设置失败
*/
public boolean expire(final String key, final long timeout)
{
return expire(key, timeout, TimeUnit.SECONDS);
}
/**
* 设置有效时间
*
* @param key Redis键
* @param timeout 超时时间
* @param unit 时间单位
* @return true=设置成功;false=设置失败
*/
public boolean expire(final String key, final long timeout, final TimeUnit unit)
{
return redisTemplate.expire(key, timeout, unit);
}
/**
* 获得缓存的基本对象。
*
* @param key 缓存键值
* @return 缓存键值对应的数据
*/
public <T> T getCacheObject(final String key)
{
ValueOperations<String, T> operation = redisTemplate.opsForValue();
return operation.get(key);
}
/**
* 删除单个对象
*
* @param key
*/
public boolean deleteObject(final String key)
{
return redisTemplate.delete(key);
}
/**
* 删除集合对象
*
* @param collection 多个对象
* @return
*/
public long deleteObject(final Collection collection)
{
return redisTemplate.delete(collection);
}
/**
* 缓存List数据
*
* @param key 缓存的键值
* @param dataList 待缓存的List数据
* @return 缓存的对象
*/
public <T> long setCacheList(final String key, final List<T> dataList)
{
Long count = redisTemplate.opsForList().rightPushAll(key, dataList);
return count == null ? 0 : count;
}
/**
* 获得缓存的list对象
*
* @param key 缓存的键值
* @return 缓存键值对应的数据
*/
public <T> List<T> getCacheList(final String key)
{
return redisTemplate.opsForList().range(key, 0, -1);
}
/**
* 缓存Set
*
* @param key 缓存键值
* @param dataSet 缓存的数据
* @return 缓存数据的对象
*/
public <T> BoundSetOperations<String, T> setCacheSet(final String key, final Set<T> dataSet)
{
BoundSetOperations<String, T> setOperation = redisTemplate.boundSetOps(key);
Iterator<T> it = dataSet.iterator();
while (it.hasNext())
{
setOperation.add(it.next());
}
return setOperation;
}
/**
* 获得缓存的set
*
* @param key
* @return
*/
public <T> Set<T> getCacheSet(final String key)
{
return redisTemplate.opsForSet().members(key);
}
/**
* 缓存Map
*
* @param key
* @param dataMap
*/
public <T> void setCacheMap(final String key, final Map<String, T> dataMap)
{
if (dataMap != null) {
redisTemplate.opsForHash().putAll(key, dataMap);
}
}
/**
* 获得缓存的Map
*
* @param key
* @return
*/
public <T> Map<String, T> getCacheMap(final String key)
{
return redisTemplate.opsForHash().entries(key);
}
/**
* 往Hash中存入数据
*
* @param key Redis键
* @param hKey Hash键
* @param value 值
*/
public <T> void setCacheMapValue(final String key, final String hKey, final T value)
{
redisTemplate.opsForHash().put(key, hKey, value);
}
/**
* 获取Hash中的数据
*
* @param key Redis键
* @param hKey Hash键
* @return Hash中的对象
*/
public <T> T getCacheMapValue(final String key, final String hKey)
{
HashOperations<String, String, T> opsForHash = redisTemplate.opsForHash();
return opsForHash.get(key, hKey);
}
/**
* 删除Hash中的数据
*
* @param key
* @param hkey
*/
public void delCacheMapValue(final String key, final String hkey)
{
HashOperations hashOperations = redisTemplate.opsForHash();
hashOperations.delete(key, hkey);
}
/**
* 获取多个Hash中的数据
*
* @param key Redis键
* @param hKeys Hash键集合
* @return Hash对象集合
*/
public <T> List<T> getMultiCacheMapValue(final String key, final Collection<Object> hKeys)
{
return redisTemplate.opsForHash().multiGet(key, hKeys);
}
/**
* 获得缓存的基本对象列表
*
* @param pattern 字符串前缀
* @return 对象列表
*/
public Collection<String> keys(final String pattern)
{
return redisTemplate.keys(pattern);
}
}实体类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
public class User implements UserDetails, Serializable {
private static final long serialVersionUID = 1L;
private Long id;
private String username;
private String password;
private String name;
private Integer sex;
/**
* 医保卡号
*/
private String medicalInsuranceCardNumber;
/**
* 身份证号
*/
private String idNumber;
private String phone;
private String salt;
private LocalDateTime gmtCreate;
private LocalDateTime gmtModified;
private Integer isDeleted;
}
实现
配置工作
1 | DROP TABLE IF EXISTS `user`; |
数据库配置信息
1 | spring: |
定义mapper接口
1 |
|
实体类
关于实体类,mapper层自动生成可以看https://lcdzzz.github.io/2022/06/22/mybatisplus/ 的”代码生成器部分“
实体类需要实现UserDetails接口,并实现里面的方法
1 |
|
mapper扫描
1 |
|
添加junit依赖
1 | <dependency> |
测试MP是否能正常使用
1 |
|
controller层
1 |
|
service类(核心代码)
1 |
|
执行到Authentication authenticate = authenticationManager.authenticate(authenticationToken);
这行时,需要通过UserDetailsService
这个接口的实现类去完成用户认证。所以需要一个类去实现UserDetailsService
,并实现loadUserByUsername
方法。
ps:这里内含最开始没有用springsecurity实现注册的痕迹,可以随便看看,用了MD5加密的方法,不过用了springsecurity以后,就用它自带的BCryptPasswordEncoder
了。关于怎么配置,在后面的SecurityConfig
配置中会有说明。
user.setRoles(userMapper.getHrRolesById(user.getId()));
这行代码可以先不要,这是接下来授权部分的代码。
1 |
|
验证完,LoginServiceImpl的login方法就可以继续往下走了
关于密码加密存储
实际项目中我们不会把密码明文存储在数据库中。
我们一般使用SpringSecurity为我们提供的BCryptPasswordEncoder。
只需要使用把BCryptPasswordEncoder对象注入Spring容器中,SpringSecurity就会使用该PasswordEncoder来进行密码校验。
可以定义一个SpringSecurity的配置类,SpringSecurity要求这个配置类要继承WebSecurityConfigurerAdapter。
同时这也是非常重要的springsecurity配置类
1 |
|
springsecurity配置类
在这之前我们自定义登陆接口,所以需要让SpringSecurity对这个接口放行,让用户访问这个接口的时候不用登录也能访问。
在接口中我们通过AuthenticationManager的authenticate方法来进行用户认证,所以也需要在SecurityConfig中配置把AuthenticationManager注入容器。(上面是把BCryptPasswordEncoder注入进容器)
认证成功的话要生成一个jwt,放入响应中返回。并且为了让用户下回请求时能通过jwt识别出具体的是哪个用户,我们需要把用户信息存入redis,可以把用户id作为key。(代码可见上面LoginServiceImpl类的login方法)
ps:这边复制完会在 http.addFilterBefore(jwtAuthenticationTokenFilter, UsernamePasswordAuthenticationFilter.class);
上报错,别急,继续往下看
1 |
|
认证过滤器
在上面springsecurity配置类中,我们把token校验过滤器也添加到了过滤器链中 http.addFilterBefore(jwtAuthenticationTokenFilter, UsernamePasswordAuthenticationFilter.class);
说明:
- 我们需要自定义一个过滤器,这个过滤器会去获取请求头中的token,对token进行解析取出其中的userid。
- 使用userid去redis中获取对应的LoginUser对象。
- 然后封装Authentication对象存入SecurityContextHolder
1 |
|
退出登录
我们只需要定义一个退出登录的接口,然后获取SecurityContextHolder中的认证信息,删除redis中对应的数据即可。
1 |
|
1 |
|
到这一步,登录和登出代码已经完成,用户登录时输入username(账号),password(密码),就会返回用户信息和token。这里因为功能已经,所以有roles和authorities等信息,可以暂时忽略(接下来会讲)。
运行结果:


授权
授权基本流程
在SpringSecurity中,会使用默认的FilterSecurityInterceptor来进行权限校验。在FilterSecurityInterceptor中会从SecurityContextHolder获取其中的Authentication,然后获取其中的权限信息。当前用户是否拥有访问当前资源所需的权限。
所以我们在项目中只需要把当前登录用户的权限信息也存入Authentication。
然后设置我们的资源所需要的权限即可。
授权实现
封装权限信息
之前在在写UserDetailsServiceImpl的时候说过user.setRoles(userMapper.getHrRolesById(user.getId()));
这行代码。
它的意思是在查询出用户后还要获取对应的权限信息,封装到UserDetails中返回。
讲到这里有必要说下这边数据库的设计,详细的可以去了解下RBAC(Role Based Access Control,基于角色的访问控制)权限模型,这里就不阐述了。
整个代码的仓库地址会放在文章末尾,里面会附带sql这个项目的整个数据库,数据都是假的,别管

sql语句:
1 | <select id="getHrRolesById" resultType="com.qingshan.smartregsystem.pojo.Role"> |
我们之前定义了UserDetails的实现类User,想要让其能封装权限信息就要对其进行修改。
实体类:
1 |
|
User修改完后我们就可以在UserDetailsServiceImpl中去把权限信息封装到User中了。

所必要的配置类
CustomFilterInvocationSecurityMetadataSource
和CustomUrlDecisionManager
,这也是实现权限控制(根据权限对接口进行拦截)的核心。
CustomFilterInvocationSecurityMetadataSource
它作用是 根据用户传来的请求地址 分析出请求需要的角色
这里需要把登录的接口、获取动态菜单的接口、注册用户的接口,设置后“特殊”的权限,这样在后面就可以对这些需要“特殊”权限的接口放行。
1 |
|
CustomUrlDecisionManager
它的作用是判断当前用户是否具备CustomFilterInvocationSecurityMetadataSource分析出来的角色
在CustomFilterInvocationSecurityMetadataSource的时候,我们已经把登录、菜单、注册这三个接口附上了“需要特殊权限”的这个信息,而在这个类中,我们可以设置:假设用户需要的权限是 那三个特殊的权限,也就是:ROLE_WANT_TO_LOGIN
、ROLE_WANT_TO_MENU
、ROLE_WANT_TO_REG
,就放行。
如果不是那三个特殊的权限,就会继续往下运行。
于是:

贴上代码↓
1 |
|
SecurityConfig收尾工作
然后,我们需要在configure(HttpSecurity http)方法中,通过withObjectPostProcessor将刚刚创建CustomUrlDecisionManager和CustomFilterInvocationSecurityMetadataSource注入进来
1 |
|
自定义失败处理
我们还希望在认证失败或者是授权失败的情况下也能和我们的接口一样返回相同结构的json,这样可以让前端能对响应进行统一的处理。要实现这个功能我们需要知道SpringSecurity的异常处理机制。
在SpringSecurity中,如果我们在认证或者授权的过程中出现了异常会被ExceptionTranslationFilter捕获到。在ExceptionTranslationFilter中会去判断是认证失败还是授权失败出现的异常。
对ExceptionTranslationFilter原理感兴趣的可以看这篇博客的第三部分https://blog.csdn.net/weixin_52834606/article/details/126729690
如果是认证过程中出现的异常会被封装成AuthenticationException然后调用AuthenticationEntryPoint对象的方法去进行异常处理。
如果是授权过程中出现的异常会被封装成AccessDeniedException然后调用AccessDeniedHandler对象的方法去进行异常处理。
所以如果我们需要自定义异常处理,我们只需要自定义AuthenticationEntryPoint和AccessDeniedHandler然后配置给SpringSecurity即可。
自定义实现类
1
2
3
4
5
6
7
8
9
10
11
12
public class AccessDeniedHandlerImpl implements AccessDeniedHandler {
public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
RespBean result = new RespBean(HttpStatus.FORBIDDEN.value(),"您的权限不足");
String json = JSON.toJSONString(result);
//处理异常
WebUtils.renderString(response,json);
}
}1
2
3
4
5
6
7
8
9
10
11
public class AuthenticationEntryPointImpl implements AuthenticationEntryPoint {
public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
RespBean result = new RespBean(HttpStatus.UNAUTHORIZED.value(),"用户认证失败请查询登录");
String json = JSON.toJSONString(result);
//处理异常
WebUtils.renderString(response,json);
}
}配置给SpringSecurity
先注入对应的处理器
1
2
3
4
5
private AuthenticationEntryPoint authenticationEntryPoint;
private AccessDeniedHandler accessDeniedHandler;然后可以使用HttpSecurity对象的方法去配置。
ps: 注释掉的是之前没有把两个实现类分离出来的写法,可以参考参考,当然更优雅的写法还是注释外的那种。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24.and().exceptionHandling()
/*.authenticationEntryPoint((request, response, authenticationException) -> {
Map<String, Object> rs = new HashMap<>();
rs.put("code", 401);//注销以后再用直接的token去访问,就是这个异常
rs.put("msg", "尚未认证!");
ObjectMapper objectMapper = new ObjectMapper();
String json = objectMapper.writeValueAsString(rs);
response.setStatus(200);
response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);
response.getWriter().println(json);
})
.accessDeniedHandler((request, response, accessDeniedException) -> {
Map<String, Object> rs = new HashMap<>();
rs.put("code", 403);
rs.put("msg", "没有权限!");
ObjectMapper objectMapper = new ObjectMapper();
String json = objectMapper.writeValueAsString(rs);
response.setStatus(200);
response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);
response.getWriter().println(json);
})*/
.authenticationEntryPoint(authenticationEntryPoint).
accessDeniedHandler(accessDeniedHandler)
结尾
写本博客时,参考了
最后还要感谢 http://www.javaboy.org/ 松哥的各种文章,没有他的指导,我没法在暑假写出动态权限的功能
还有关于springsecurity的其他细节,可以看↓