Skip to content

Commit 8e7e2be

Browse files
committed
refactor/重构鉴权逻辑
1 parent fffa864 commit 8e7e2be

File tree

10 files changed

+54
-24
lines changed

10 files changed

+54
-24
lines changed

pmhub-auth/src/main/java/com/laigeoffer/pmhub/auth/controller/LoginController.java

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.laigeoffer.pmhub.auth.controller;
22

33
import com.laigeoffer.pmhub.auth.service.SysLoginService;
4-
import com.laigeoffer.pmhub.base.core.constant.Constants;
54
import com.laigeoffer.pmhub.base.core.core.domain.AjaxResult;
65
import com.laigeoffer.pmhub.base.core.core.domain.R;
76
import com.laigeoffer.pmhub.base.core.core.domain.model.LoginBody;
@@ -38,14 +37,12 @@ public class LoginController {
3837
private SysLoginService sysLoginService;
3938

4039
@PostMapping("login")
41-
public AjaxResult login(@RequestBody LoginBody form) {
40+
public R<?> login(@RequestBody LoginBody form) {
4241
AjaxResult ajax = success();
4342
// 用户登录
4443
LoginUser userInfo = sysLoginService.login(form.getUsername(), form.getPassword());
45-
String token = tokenService.createToken(userInfo);
46-
ajax.put(Constants.TOKEN, token);
4744
// 获取登录token
48-
return ajax;
45+
return R.ok(tokenService.createToken(userInfo));
4946
}
5047

5148
@DeleteMapping("logout")

pmhub-base/pmhub-base-core/src/main/java/com/laigeoffer/pmhub/base/core/constant/CacheConstants.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public class CacheConstants {
99
/**
1010
* 登录用户 redis key
1111
*/
12-
public static final String LOGIN_TOKEN_KEY = "login_tokens:";
12+
public static final String LOGIN_TOKEN_KEY = "user_key:";
1313

1414
/**
1515
* 验证码 redis key

pmhub-base/pmhub-base-core/src/main/java/com/laigeoffer/pmhub/base/core/constant/SecurityConstants.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
/**
44
* 权限相关通用常量
55
*
6-
* @author ruoyi
6+
* @author canghe
77
*/
88
public class SecurityConstants
99
{

pmhub-base/pmhub-base-core/src/main/java/com/laigeoffer/pmhub/base/core/constant/TokenConstants.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@ public class TokenConstants
2020
/**
2121
* 令牌秘钥
2222
*/
23-
public final static String SECRET = "abcdefghijklmnopqrstuvwxyz";
23+
public final static String SECRET = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz";
2424

2525
}

pmhub-base/pmhub-base-core/src/main/java/com/laigeoffer/pmhub/base/core/core/domain/model/LoginUser.java

+9
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ public class LoginUser implements Serializable {
1919
*/
2020
private Long userId;
2121

22+
/**
23+
* 用户名
24+
*/
25+
private String username;
26+
2227
/**
2328
* 部门ID
2429
*/
@@ -95,6 +100,10 @@ public Long getUserId() {
95100
return userId;
96101
}
97102

103+
public void setUsername(String username) {
104+
this.username = username;
105+
}
106+
98107
public void setUserId(Long userId) {
99108
this.userId = userId;
100109
}

pmhub-base/pmhub-base-core/src/main/java/com/laigeoffer/pmhub/base/core/utils/ServletUtils.java

+10
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,16 @@ public static Map<String, String> getHeaders(HttpServletRequest request)
220220
return map;
221221
}
222222

223+
public static String getHeader(HttpServletRequest request, String name)
224+
{
225+
String value = request.getHeader(name);
226+
if (StringUtils.isEmpty(value))
227+
{
228+
return StringUtils.EMPTY;
229+
}
230+
return urlDecode(value);
231+
}
232+
223233
/**
224234
* 设置webflux模型响应
225235
*

pmhub-base/pmhub-base-security/src/main/java/com/laigeoffer/pmhub/base/security/interceptor/HeaderInterceptor.java

+5-4
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
import com.laigeoffer.pmhub.base.core.constant.SecurityConstants;
44
import com.laigeoffer.pmhub.base.core.context.SecurityContextHolder;
55
import com.laigeoffer.pmhub.base.core.core.domain.model.LoginUser;
6-
import com.laigeoffer.pmhub.base.security.utils.SecurityUtils;
6+
import com.laigeoffer.pmhub.base.core.utils.ServletUtils;
77
import com.laigeoffer.pmhub.base.core.utils.StringUtils;
88
import com.laigeoffer.pmhub.base.security.auth.AuthUtil;
9+
import com.laigeoffer.pmhub.base.security.utils.SecurityUtils;
910
import org.springframework.web.method.HandlerMethod;
1011
import org.springframework.web.servlet.AsyncHandlerInterceptor;
1112

@@ -28,9 +29,9 @@ public boolean preHandle(HttpServletRequest request, HttpServletResponse respons
2829
return true;
2930
}
3031

31-
// SecurityContextHolder.setUserId(ServletUtils.getHeader(request, SecurityConstants.DETAILS_USER_ID));
32-
// SecurityContextHolder.setUserName(ServletUtils.getHeader(request, SecurityConstants.DETAILS_USERNAME));
33-
// SecurityContextHolder.setUserKey(ServletUtils.getHeader(request, SecurityConstants.USER_KEY));
32+
SecurityContextHolder.setUserId(ServletUtils.getHeader(request, SecurityConstants.DETAILS_USER_ID));
33+
SecurityContextHolder.setUserName(ServletUtils.getHeader(request, SecurityConstants.DETAILS_USERNAME));
34+
SecurityContextHolder.setUserKey(ServletUtils.getHeader(request, SecurityConstants.USER_KEY));
3435

3536
String token = SecurityUtils.getToken();
3637
if (StringUtils.isNotEmpty(token))

pmhub-base/pmhub-base-security/src/main/java/com/laigeoffer/pmhub/base/security/service/TokenService.java

+24-10
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.laigeoffer.pmhub.base.core.config.redis.RedisService;
44
import com.laigeoffer.pmhub.base.core.constant.CacheConstants;
55
import com.laigeoffer.pmhub.base.core.constant.Constants;
6+
import com.laigeoffer.pmhub.base.core.constant.SecurityConstants;
67
import com.laigeoffer.pmhub.base.core.core.domain.model.LoginUser;
78
import com.laigeoffer.pmhub.base.core.utils.JwtUtils;
89
import com.laigeoffer.pmhub.base.core.utils.ServletUtils;
@@ -41,6 +42,9 @@ public class TokenService {
4142
protected static final long MILLIS_SECOND = 1000;
4243
protected static final long MILLIS_MINUTE = 60 * MILLIS_SECOND;
4344
private static final Long MILLIS_MINUTE_TEN = 20 * 60 * 1000L;
45+
46+
private final static String ACCESS_TOKEN = CacheConstants.LOGIN_TOKEN_KEY;
47+
4448
// 令牌自定义标识
4549
@Value("${token.header}")
4650
private String header;
@@ -133,19 +137,29 @@ public void delLoginUser(String token) {
133137

134138
/**
135139
* 创建令牌
136-
*
137-
* @param loginUser 用户信息
138-
* @return 令牌
139140
*/
140-
public String createToken(LoginUser loginUser) {
141+
public Map<String, Object> createToken(LoginUser loginUser)
142+
{
141143
String token = IdUtils.fastUUID();
144+
Long userId = loginUser.getUser().getUserId();
145+
String userName = loginUser.getUser().getUserName();
142146
loginUser.setToken(token);
143-
setUserAgent(loginUser);
147+
loginUser.setUserId(userId);
148+
loginUser.setUsername(userName);
149+
loginUser.setIpaddr(IpUtils.getIpAddr());
144150
refreshToken(loginUser);
145151

146-
Map<String, Object> claims = new HashMap<>();
147-
claims.put(Constants.LOGIN_USER_KEY, token);
148-
return createToken(claims);
152+
// Jwt存储信息
153+
Map<String, Object> claimsMap = new HashMap<String, Object>();
154+
claimsMap.put(SecurityConstants.USER_KEY, token);
155+
claimsMap.put(SecurityConstants.DETAILS_USER_ID, userId);
156+
claimsMap.put(SecurityConstants.DETAILS_USERNAME, userName);
157+
158+
// 接口返回信息
159+
Map<String, Object> rspMap = new HashMap<String, Object>();
160+
rspMap.put("access_token", JwtUtils.createToken(claimsMap));
161+
rspMap.put("expires_in", expireTime);
162+
return rspMap;
149163
}
150164

151165

@@ -308,7 +322,7 @@ private String getToken(HttpServletRequest request) {
308322
return token;
309323
}
310324

311-
private String getTokenKey(String uuid) {
312-
return CacheConstants.LOGIN_TOKEN_KEY + uuid;
325+
private String getTokenKey(String token) {
326+
return ACCESS_TOKEN + token;
313327
}
314328
}

pmhub-gateway/src/main/java/com/laigeoffer/pmhub/gateway/filter/AuthFilter.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
7171
addHeader(mutate, SecurityConstants.USER_KEY, userkey);
7272
addHeader(mutate, SecurityConstants.DETAILS_USER_ID, userid);
7373
addHeader(mutate, SecurityConstants.DETAILS_USERNAME, username);
74-
// 内部请求来源参数清除
74+
// 内部请求来源参数清除(防止网关携带内部请求标识,造成系统安全风险)
7575
removeHeader(mutate, SecurityConstants.FROM_SOURCE);
7676
return chain.filter(exchange.mutate().request(mutate.build()).build());
7777
}

pmhub-modules/pmhub-system/src/main/java/com/laigeoffer/pmhub/system/controller/SysUserController.java

-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@ public void importTemplate(HttpServletResponse response) {
103103
/**
104104
* 根据用户编号获取详细信息
105105
*/
106-
@InnerAuth
107106
@RequiresPermissions("system:user:query")
108107
@GetMapping(value = {"/", "/{userId}"})
109108
public AjaxResult getInfo(@PathVariable(value = "userId", required = false) Long userId) {

0 commit comments

Comments
 (0)