Initial commit

This commit is contained in:
2023-06-23 19:00:37 +08:00
committed by Yo Vinchen
parent 39a3866312
commit bf21c15274
19 changed files with 492 additions and 83 deletions

View File

@@ -89,7 +89,7 @@ public class JwtHelper {
* @param args
*/
public static void main(String[] args) {
String token = JwtHelper.createToken(1L, "admin");
String token = JwtHelper.createToken(4L, "lisi");
System.out.println(token);
String username = JwtHelper.getUsername(token);
Long userId = JwtHelper.getUserId(token);

View File

@@ -0,0 +1,39 @@
package com.atguigu.security.custom;
/**
* ClassName: LoginUserInfoHelper
* Package: com.atguigu.security.custom
* 获取当前用户信息帮助类
*
* @author yovinchen
* @Create 2023/6/23 09:24
*/
public class LoginUserInfoHelper {
private static ThreadLocal<Long> userId = new ThreadLocal<Long>();
private static ThreadLocal<String> username = new ThreadLocal<String>();
public static Long getUserId() {
return userId.get();
}
public static void setUserId(Long _userId) {
userId.set(_userId);
}
public static void removeUserId() {
userId.remove();
}
public static String getUsername() {
return username.get();
}
public static void setUsername(String _username) {
username.set(_username);
}
public static void removeUsername() {
username.remove();
}
}

View File

@@ -5,6 +5,7 @@ import com.atguigu.common.jwt.JwtHelper;
import com.atguigu.common.result.Result;
import com.atguigu.common.result.ResultCodeEnum;
import com.atguigu.common.utils.ResponseUtil;
import com.atguigu.security.custom.LoginUserInfoHelper;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
@@ -60,12 +61,15 @@ public class TokenAuthenticationFilter extends OncePerRequestFilter {
String token = request.getHeader("token");
logger.info("token:" + token);
if (!StringUtils.isEmpty(token)) {
String useruame = JwtHelper.getUsername(token);
logger.info("useruame:" + useruame);
String username = JwtHelper.getUsername(token);
logger.info("username:" + username);
//认证成功
if (!StringUtils.isEmpty(useruame)) {
if (!StringUtils.isEmpty(username)) {
//当前用户信息放到 Thresdlocal 里面
LoginUserInfoHelper.setUserId(JwtHelper.getUserId(token));
LoginUserInfoHelper.setUsername(username);
//通过username从reids中获取权限数据
String authString = (String) redisTemplate.opsForValue().get(useruame);
String authString = (String) redisTemplate.opsForValue().get(username);
//将redis中获取的字符串权限数据转换为 ArrayList<SimpleGrantedAuthority>
if (!StringUtils.isEmpty(authString)) {
List<Map> mapList = JSON.parseArray(authString, Map.class);
@@ -74,9 +78,9 @@ public class TokenAuthenticationFilter extends OncePerRequestFilter {
for (Map map : mapList) {
authList.add(new SimpleGrantedAuthority((String) map.get("authority")));
}
return new UsernamePasswordAuthenticationToken(useruame, null, authList);
return new UsernamePasswordAuthenticationToken(username, null, authList);
} else {
return new UsernamePasswordAuthenticationToken(useruame, null, new ArrayList<>());
return new UsernamePasswordAuthenticationToken(username, null, new ArrayList<>());
}
}
}