|
1 | 1 | /** |
2 | 2 | * Copyright (c) 2023 - present TinyEngine Authors. |
3 | 3 | * Copyright (c) 2023 - present Huawei Cloud Computing Technologies Co., Ltd. |
4 | | - * |
| 4 | + * <p> |
5 | 5 | * Use of this source code is governed by an MIT-style license. |
6 | | - * |
| 6 | + * <p> |
7 | 7 | * THE OPEN SOURCE SOFTWARE IN THIS PRODUCT IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL, |
8 | 8 | * BUT WITHOUT ANY WARRANTY, WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR |
9 | 9 | * A PARTICULAR PURPOSE. SEE THE APPLICABLE LICENSES FOR MORE DETAILS. |
10 | | - * |
11 | 10 | */ |
12 | 11 |
|
13 | 12 | package com.tinyengine.it.login.config; |
|
17 | 16 | import com.tinyengine.it.login.utils.JwtUtil; |
18 | 17 | import com.tinyengine.it.login.config.context.DefaultLoginUserContext; |
19 | 18 | import com.tinyengine.it.login.model.UserInfo; |
| 19 | +import com.tinyengine.it.mapper.AuthUsersUnitsRolesMapper; |
20 | 20 | import com.tinyengine.it.model.entity.Tenant; |
21 | 21 | import jakarta.servlet.http.HttpServletRequest; |
22 | 22 | import jakarta.servlet.http.HttpServletResponse; |
|
34 | 34 | @Component |
35 | 35 | public class SSOInterceptor implements HandlerInterceptor { |
36 | 36 |
|
37 | | - @Autowired |
38 | | - private JwtUtil jwtUtil; |
39 | | - |
40 | | - @Override |
41 | | - public boolean preHandle(HttpServletRequest request, |
42 | | - HttpServletResponse response, Object handler) throws Exception { |
43 | | - |
44 | | - String authorization = request.getHeader("Authorization"); |
45 | | - // 如果没有token,重定向到登录页 |
46 | | - if (authorization == null || authorization.isEmpty()) { |
47 | | - log.info("No token"); |
48 | | - throw new ServiceException(ExceptionEnum.CM336.getResultCode(), ExceptionEnum.CM336.getResultMsg()); |
49 | | - } |
50 | | - String token = jwtUtil.getTokenFromRequest(authorization); |
51 | | - String requestURI = request.getRequestURI(); |
52 | | - |
53 | | - log.info("Intercepting: {}, Token: {}", requestURI, token != null ? "present" : "null"); |
54 | | - |
55 | | - try { |
56 | | - // 验证token |
57 | | - if (!jwtUtil.validateToken(token)) { |
58 | | - log.warn("Token validation failed"); |
59 | | - throw new ServiceException(ExceptionEnum.CM339.getResultCode(), ExceptionEnum.CM339.getResultMsg()); |
60 | | - } |
61 | | - |
62 | | - // 从token中获取用户信息 |
63 | | - String username = jwtUtil.getUsernameFromToken(token); |
64 | | - String userId = jwtUtil.getUserIdFromToken(token); |
65 | | - List<Tenant> tenants = jwtUtil.getTenantIdFromToken(token); |
66 | | - String roles = jwtUtil.getRolesFromToken(token); |
67 | | - Integer platformId = jwtUtil.getPlatformIdFromToken(token); |
68 | | - |
69 | | - |
70 | | - // 检查必需的用户信息 |
71 | | - if (username == null || username.isEmpty() || userId == null) { |
72 | | - log.warn("User information is incomplete - username: {}, userId: {}", username, userId); |
73 | | - throw new ServiceException(ExceptionEnum.CM339.getResultCode(), ExceptionEnum.CM339.getResultMsg()); |
74 | | - } |
75 | | - |
76 | | - // 存储用户信息到LoginUserContext |
77 | | - UserInfo userInfo = new UserInfo(userId, username, tenants); |
78 | | - |
79 | | - userInfo.setPlatformId(platformId != null ? platformId : 0); |
80 | | - userInfo.setRoles(roles != null ? roles : "USER"); |
81 | | - userInfo.setToken(token); |
82 | | - |
83 | | - DefaultLoginUserContext.setCurrentUser(userInfo); |
84 | | - |
85 | | - log.info("Token validated and user context set for user: {}", username); |
86 | | - return true; |
87 | | - |
88 | | - } catch (Exception e) { |
89 | | - log.error("Token validation exception: {}", e.getMessage(), e); |
90 | | - DefaultLoginUserContext.clear(); |
91 | | - throw new ServiceException(ExceptionEnum.CM339.getResultCode(), ExceptionEnum.CM339.getResultMsg()); |
92 | | - } |
93 | | - } |
94 | | - |
95 | | - @Override |
96 | | - public void afterCompletion(HttpServletRequest request, |
97 | | - HttpServletResponse response, Object handler, Exception ex) { |
98 | | - // 请求完成后清理用户上下文 |
99 | | - DefaultLoginUserContext.clear(); |
100 | | - log.debug("Cleared user context for request completion"); |
101 | | - } |
| 37 | + @Autowired |
| 38 | + private JwtUtil jwtUtil; |
| 39 | + @Autowired |
| 40 | + AuthUsersUnitsRolesMapper authUsersUnitsRolesMapper; |
| 41 | + |
| 42 | + @Override |
| 43 | + public boolean preHandle(HttpServletRequest request, |
| 44 | + HttpServletResponse response, Object handler) throws Exception { |
| 45 | + |
| 46 | + String authorization = request.getHeader("Authorization"); |
| 47 | + String org = request.getHeader("X-Lowcode-Org"); |
| 48 | + // 如果没有token,重定向到登录页 |
| 49 | + if (authorization == null || authorization.isEmpty()) { |
| 50 | + log.info("No token"); |
| 51 | + throw new ServiceException(ExceptionEnum.CM336.getResultCode(), ExceptionEnum.CM336.getResultMsg()); |
| 52 | + } |
| 53 | + String token = jwtUtil.getTokenFromRequest(authorization); |
| 54 | + String requestURI = request.getRequestURI(); |
| 55 | + |
| 56 | + log.info("Intercepting: {}, Token: {}", requestURI, token != null ? "present" : "null"); |
| 57 | + |
| 58 | + try { |
| 59 | + // 验证token |
| 60 | + if (!jwtUtil.validateToken(token)) { |
| 61 | + log.warn("Token validation failed"); |
| 62 | + throw new ServiceException(ExceptionEnum.CM339.getResultCode(), ExceptionEnum.CM339.getResultMsg()); |
| 63 | + } |
| 64 | + |
| 65 | + // 从token中获取用户信息 |
| 66 | + String username = jwtUtil.getUsernameFromToken(token); |
| 67 | + String userId = jwtUtil.getUserIdFromToken(token); |
| 68 | + String roles = jwtUtil.getRolesFromToken(token); |
| 69 | + Integer platformId = jwtUtil.getPlatformIdFromToken(token); |
| 70 | + |
| 71 | + |
| 72 | + // 检查必需的用户信息 |
| 73 | + if (username == null || username.isEmpty() || userId == null) { |
| 74 | + log.warn("User information is incomplete - username: {}, userId: {}", username, userId); |
| 75 | + throw new ServiceException(ExceptionEnum.CM339.getResultCode(), ExceptionEnum.CM339.getResultMsg()); |
| 76 | + } |
| 77 | + int userIdInt; |
| 78 | + try { |
| 79 | + userIdInt = Integer.parseInt(userId); |
| 80 | + } catch (NumberFormatException e) { |
| 81 | + log.error("Invalid userId format: {}", userId); |
| 82 | + throw new ServiceException(ExceptionEnum.CM342.getResultCode(), ExceptionEnum.CM342.getResultMsg()); |
| 83 | + } |
| 84 | + List<Tenant> tenants = authUsersUnitsRolesMapper.queryAllTenantByUserId(userIdInt); |
| 85 | + if (tenants == null) { |
| 86 | + log.warn("No tenants found for userId: {}", userId); |
| 87 | + throw new ServiceException(ExceptionEnum.CM340.getResultCode(), ExceptionEnum.CM340.getResultMsg()); |
| 88 | + } |
| 89 | + |
| 90 | + if (!"null".equals(org) && org != null) { |
| 91 | + boolean findOrg = false; |
| 92 | + for (Tenant tenant : tenants) { |
| 93 | + tenant.setIsInUse(tenant.getId().equals(org)); |
| 94 | + if (tenant.getIsInUse()) { |
| 95 | + findOrg = true; |
| 96 | + } |
| 97 | + } |
| 98 | + if (!findOrg) { |
| 99 | + log.warn("X-Lowcode-Org not found in user's tenants - X-Lowcode-Org: {}", org); |
| 100 | + throw new ServiceException(ExceptionEnum.CM341.getResultCode(), ExceptionEnum.CM341.getResultMsg()); |
| 101 | + } |
| 102 | + } |
| 103 | + // 存储用户信息到LoginUserContext |
| 104 | + UserInfo userInfo = new UserInfo(userId, username, tenants); |
| 105 | + |
| 106 | + userInfo.setPlatformId(platformId != null ? platformId : 0); |
| 107 | + userInfo.setRoles(roles != null ? roles : "USER"); |
| 108 | + userInfo.setToken(token); |
| 109 | + |
| 110 | + DefaultLoginUserContext.setCurrentUser(userInfo); |
| 111 | + |
| 112 | + log.info("Token validated and user context set for user: {}", username); |
| 113 | + return true; |
| 114 | + |
| 115 | + } catch (Exception e) { |
| 116 | + log.error("Token validation exception: {}", e.getMessage(), e); |
| 117 | + DefaultLoginUserContext.clear(); |
| 118 | + if(e instanceof ServiceException) { |
| 119 | + throw e; |
| 120 | + } |
| 121 | + throw new ServiceException(ExceptionEnum.CM339.getResultCode(), ExceptionEnum.CM339.getResultMsg()); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + @Override |
| 126 | + public void afterCompletion(HttpServletRequest request, |
| 127 | + HttpServletResponse response, Object handler, Exception ex) { |
| 128 | + // 请求完成后清理用户上下文 |
| 129 | + DefaultLoginUserContext.clear(); |
| 130 | + |
| 131 | + log.debug("Cleared user context for request completion"); |
| 132 | + } |
102 | 133 | } |
0 commit comments