mirror of https://github.com/elunez/eladmin
[代码完善](v2.5): v2.5 beta TokenFilter Token 验证逻辑优化
对于已放行的接口不去验证 Token Closes #338 2.5 Beta 详情:https://www.ydyno.com/archives/1225.htmlpull/361/head^2
parent
adde56babe
commit
493c02980b
|
@ -15,6 +15,7 @@
|
||||||
*/
|
*/
|
||||||
package me.zhengjie.modules.security.security;
|
package me.zhengjie.modules.security.security;
|
||||||
|
|
||||||
|
import cn.hutool.core.util.StrUtil;
|
||||||
import io.jsonwebtoken.ExpiredJwtException;
|
import io.jsonwebtoken.ExpiredJwtException;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
@ -47,24 +48,22 @@ public class TokenFilter extends GenericFilterBean {
|
||||||
throws IOException, ServletException {
|
throws IOException, ServletException {
|
||||||
HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
|
HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
|
||||||
String token = resolveToken(httpServletRequest);
|
String token = resolveToken(httpServletRequest);
|
||||||
String requestRri = httpServletRequest.getRequestURI();
|
// 对于 Token 为空的不需要去查 Redis
|
||||||
// 验证 token 是否存在
|
if(StrUtil.isNotBlank(token)){
|
||||||
OnlineUserDto onlineUserDto = null;
|
OnlineUserDto onlineUserDto = null;
|
||||||
SecurityProperties properties = SpringContextHolder.getBean(SecurityProperties.class);
|
SecurityProperties properties = SpringContextHolder.getBean(SecurityProperties.class);
|
||||||
try {
|
try {
|
||||||
OnlineUserService onlineUserService = SpringContextHolder.getBean(OnlineUserService.class);
|
OnlineUserService onlineUserService = SpringContextHolder.getBean(OnlineUserService.class);
|
||||||
onlineUserDto = onlineUserService.getOne(properties.getOnlineKey() + token);
|
onlineUserDto = onlineUserService.getOne(properties.getOnlineKey() + token);
|
||||||
} catch (ExpiredJwtException e) {
|
} catch (ExpiredJwtException e) {
|
||||||
log.error(e.getMessage());
|
log.error(e.getMessage());
|
||||||
}
|
}
|
||||||
if (onlineUserDto != null && StringUtils.hasText(token)) {
|
if (onlineUserDto != null && StringUtils.hasText(token)) {
|
||||||
Authentication authentication = tokenProvider.getAuthentication(token);
|
Authentication authentication = tokenProvider.getAuthentication(token);
|
||||||
SecurityContextHolder.getContext().setAuthentication(authentication);
|
SecurityContextHolder.getContext().setAuthentication(authentication);
|
||||||
// Token 续期
|
// Token 续期
|
||||||
tokenProvider.checkRenewal(token);
|
tokenProvider.checkRenewal(token);
|
||||||
log.debug("set Authentication to security context for '{}', uri: {}", authentication.getName(), requestRri);
|
}
|
||||||
} else {
|
|
||||||
log.debug("no valid JWT token found, uri: {}", requestRri);
|
|
||||||
}
|
}
|
||||||
filterChain.doFilter(servletRequest, servletResponse);
|
filterChain.doFilter(servletRequest, servletResponse);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue