1212
1313package com .tinyengine .it .login .config ;
1414
15+ import com .tinyengine .it .common .exception .ExceptionEnum ;
16+ import com .tinyengine .it .common .exception .ServiceException ;
1517import com .tinyengine .it .login .utils .JwtUtil ;
1618import com .tinyengine .it .login .config .context .DefaultLoginUserContext ;
1719import com .tinyengine .it .login .model .UserInfo ;
@@ -35,18 +37,15 @@ public class SSOInterceptor implements HandlerInterceptor {
3537 @ Autowired
3638 private JwtUtil jwtUtil ;
3739
38- private static final String SSO_SERVER = System .getenv ("SSO_SERVER" );
39-
4040 @ Override
4141 public boolean preHandle (HttpServletRequest request ,
4242 HttpServletResponse response , Object handler ) throws Exception {
4343
4444 String authorization = request .getHeader ("Authorization" );
4545 // 如果没有token,重定向到登录页
4646 if (authorization == null || authorization .isEmpty ()) {
47- log .info ("No token, redirecting to: {}" , SSO_SERVER );
48- response .sendRedirect (SSO_SERVER );
49- return false ;
47+ log .info ("No token" );
48+ throw new ServiceException (ExceptionEnum .CM336 .getResultCode (), ExceptionEnum .CM336 .getResultMsg ());
5049 }
5150 String token = jwtUtil .getTokenFromRequest (authorization );
5251 String requestURI = request .getRequestURI ();
@@ -57,8 +56,7 @@ public boolean preHandle(HttpServletRequest request,
5756 // 验证token
5857 if (!jwtUtil .validateToken (token )) {
5958 log .warn ("Token validation failed" );
60- response .sendRedirect (SSO_SERVER );
61- return false ;
59+ throw new ServiceException (ExceptionEnum .CM339 .getResultCode (), ExceptionEnum .CM339 .getResultMsg ());
6260 }
6361
6462 // 从token中获取用户信息
@@ -72,8 +70,7 @@ public boolean preHandle(HttpServletRequest request,
7270 // 检查必需的用户信息
7371 if (username == null || username .isEmpty () || userId == null ) {
7472 log .warn ("User information is incomplete - username: {}, userId: {}" , username , userId );
75- response .sendRedirect (SSO_SERVER );
76- return false ;
73+ throw new ServiceException (ExceptionEnum .CM339 .getResultCode (), ExceptionEnum .CM339 .getResultMsg ());
7774 }
7875
7976 // 存储用户信息到LoginUserContext
@@ -90,9 +87,8 @@ public boolean preHandle(HttpServletRequest request,
9087
9188 } catch (Exception e ) {
9289 log .error ("Token validation exception: {}" , e .getMessage (), e );
93- response .sendRedirect (SSO_SERVER );
9490 DefaultLoginUserContext .clear ();
95- return false ;
91+ throw new ServiceException ( ExceptionEnum . CM339 . getResultCode (), ExceptionEnum . CM339 . getResultMsg ()) ;
9692 }
9793 }
9894
0 commit comments