Merge pull request #36 from hiparker/development

fix: 修复登录过程中验证Jwt异常后 无法正常拦截问题
pull/38/head
Anthony Parker 1 year ago committed by GitHub
commit 1d29d0ea4f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -15,6 +15,7 @@
*/
package org.opsli.core.security.filter;
import cn.hutool.json.JSONUtil;
import lombok.AllArgsConstructor;
import org.opsli.core.base.dto.LoginUserDto;
import org.opsli.core.security.service.UidUserDetailDetailServiceImpl;
@ -22,6 +23,8 @@ import org.opsli.core.utils.UserTokenUtil;
import org.opsli.plugins.security.authentication.AfterAuthenticationToken;
import org.opsli.plugins.security.exception.AuthException;
import org.opsli.plugins.security.exception.errorcode.AuthErrorCodeEnum;
import org.opsli.plugins.security.utils.WebUtils;
import org.opsli.plugins.security.vo.AuthResultWrapper;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.util.StringUtils;
@ -56,6 +59,7 @@ public class JwtAuthenticationTokenFilter extends OncePerRequestFilter {
return;
}
try {
// 验证Token
UserTokenUtil.verify(token);
@ -72,10 +76,24 @@ public class JwtAuthenticationTokenFilter extends OncePerRequestFilter {
AfterAuthenticationToken authenticationToken =
new AfterAuthenticationToken(userDetails, null, userDetails.getAuthorities());
SecurityContextHolder.getContext().setAuthentication(authenticationToken);
}catch (AuthException ae){
// 权校验Token异常
Integer code = ae.getCode();
String errorMessage = ae.getErrorMessage();
AuthResultWrapper<?> customResultWrapper =
AuthResultWrapper.getCustomResultWrapper(code, errorMessage);
WebUtils.renderString(request, response, JSONUtil.toJsonStr(customResultWrapper));
return;
}catch (Exception e){
// 其他异常
AuthResultWrapper<?> customResultWrapper = AuthResultWrapper.getErrorResultWrapper();
WebUtils.renderString(request, response, JSONUtil.toJsonStr(customResultWrapper));
return;
}
//放行
filterChain.doFilter(request, response);
}
}

Loading…
Cancel
Save