1717import com .tinyengine .it .login .utils .JwtUtil ;
1818import com .tinyengine .it .login .config .context .DefaultLoginUserContext ;
1919import com .tinyengine .it .login .model .UserInfo ;
20+ import com .tinyengine .it .mapper .AuthUsersUnitsRolesMapper ;
2021import com .tinyengine .it .model .entity .Tenant ;
21- import com .tinyengine .it .model .entity .User ;
22- import com .tinyengine .it .service .app .UserService ;
2322import jakarta .servlet .http .HttpServletRequest ;
2423import jakarta .servlet .http .HttpServletResponse ;
2524import lombok .extern .slf4j .Slf4j ;
@@ -39,13 +38,13 @@ public class SSOInterceptor implements HandlerInterceptor {
3938 @ Autowired
4039 private JwtUtil jwtUtil ;
4140 @ Autowired
42- private UserService userService ;
43-
41+ AuthUsersUnitsRolesMapper authUsersUnitsRolesMapper ;
4442 @ Override
4543 public boolean preHandle (HttpServletRequest request ,
4644 HttpServletResponse response , Object handler ) throws Exception {
4745
4846 String authorization = request .getHeader ("Authorization" );
47+ String org = request .getHeader ("X-Lowcode-Org" );
4948 // 如果没有token,重定向到登录页
5049 if (authorization == null || authorization .isEmpty ()) {
5150 log .info ("No token" );
@@ -66,7 +65,6 @@ public boolean preHandle(HttpServletRequest request,
6665 // 从token中获取用户信息
6766 String username = jwtUtil .getUsernameFromToken (token );
6867 String userId = jwtUtil .getUserIdFromToken (token );
69- List <Tenant > tenants = jwtUtil .getTenantIdFromToken (token );
7068 String roles = jwtUtil .getRolesFromToken (token );
7169 Integer platformId = jwtUtil .getPlatformIdFromToken (token );
7270
@@ -76,16 +74,30 @@ public boolean preHandle(HttpServletRequest request,
7674 log .warn ("User information is incomplete - username: {}, userId: {}" , username , userId );
7775 throw new ServiceException (ExceptionEnum .CM339 .getResultCode (), ExceptionEnum .CM339 .getResultMsg ());
7876 }
79- User user = userService .queryUserById (userId );// 确认用户存在
80- if (user == null ) {
81- log .warn ("User not found for userId: {}" , userId );
82- throw new ServiceException (ExceptionEnum .CM338 .getResultCode (), ExceptionEnum .CM339 .getResultMsg ());
83- }
84- Integer useTenantId = user .getUseTenantId ();
85- for (Tenant tenant : tenants ) {
86- tenant .setIsInUse (tenant .getId ().equals (useTenantId .toString ()));
77+ List <Tenant > tenants = authUsersUnitsRolesMapper .queryAllTenantByUserId (Integer .valueOf (userId ));
78+ if (!requestURI .equals ("/platform-center/api/user/me" )){
79+
80+ if (requestURI .contains ("user/tenant" )){
81+ String queryString = request .getQueryString ();
82+ org =queryString .split ("=" )[1 ];
83+ }
84+ if (tenants != null &&!org .equals ("null" )){
85+ boolean findOrg = false ;
86+ for (Tenant tenant : tenants ) {
87+ tenant .setIsInUse (tenant .getId ().equals (org ));
88+ if (tenant .getIsInUse ()){
89+ findOrg = true ;
90+ }
91+ }
92+ if (!findOrg ){
93+ log .warn ("X-Lowcode-Org not found in user's tenants - X-Lowcode-Org: {}" , org );
94+ throw new ServiceException (ExceptionEnum .CM341 .getResultCode (), ExceptionEnum .CM341 .getResultMsg ());
95+ }
96+ }
97+
8798 }
88- // 存储用户信息到LoginUserContext
99+
100+ // 存储用户信息到LoginUserContext
89101 UserInfo userInfo = new UserInfo (userId , username , tenants );
90102
91103 userInfo .setPlatformId (platformId != null ? platformId : 0 );
@@ -100,7 +112,7 @@ public boolean preHandle(HttpServletRequest request,
100112 } catch (Exception e ) {
101113 log .error ("Token validation exception: {}" , e .getMessage (), e );
102114 DefaultLoginUserContext .clear ();
103- throw new ServiceException (ExceptionEnum .CM339 .getResultCode (), ExceptionEnum . CM339 . getResultMsg ());
115+ throw new ServiceException (ExceptionEnum .CM339 .getResultCode (), e . getMessage ());
104116 }
105117 }
106118
@@ -109,6 +121,7 @@ public void afterCompletion(HttpServletRequest request,
109121 HttpServletResponse response , Object handler , Exception ex ) {
110122 // 请求完成后清理用户上下文
111123 DefaultLoginUserContext .clear ();
124+
112125 log .debug ("Cleared user context for request completion" );
113126 }
114127}
0 commit comments