44import com .datamate .common .infrastructure .common .Response ;
55import com .datamate .common .infrastructure .exception .CommonErrorCode ;
66import com .datamate .gateway .application .UserApplicationService ;
7+ import com .datamate .gateway .domain .service .UserService ;
78import com .datamate .gateway .interfaces .dto .LoginRequest ;
89import com .datamate .gateway .interfaces .dto .LoginResponse ;
910import com .datamate .gateway .interfaces .dto .RegisterRequest ;
11+ import com .datamate .gateway .interfaces .dto .UserResponse ;
12+ import jakarta .servlet .http .HttpServletRequest ;
1013import jakarta .validation .Valid ;
1114import lombok .RequiredArgsConstructor ;
1215import lombok .extern .slf4j .Slf4j ;
16+ import org .apache .commons .lang3 .StringUtils ;
1317import org .springframework .http .HttpStatus ;
1418import org .springframework .http .ResponseEntity ;
1519import org .springframework .validation .annotation .Validated ;
20+ import org .springframework .web .bind .annotation .GetMapping ;
1621import org .springframework .web .bind .annotation .PostMapping ;
1722import org .springframework .web .bind .annotation .RequestBody ;
1823import org .springframework .web .bind .annotation .RequestMapping ;
3035@ RequiredArgsConstructor
3136public class UserController {
3237 private final UserApplicationService userApplicationService ;
38+ private final UserService userService ;
3339
3440 @ PostMapping ("/login" )
3541 @ IgnoreResponseWrap
@@ -48,4 +54,54 @@ public ResponseEntity<Response<LoginResponse>> register(@Valid @RequestBody Regi
4854 .orElseGet (() -> ResponseEntity .status (HttpStatus .BAD_REQUEST )
4955 .body (Response .error (CommonErrorCode .SIGNUP_ERROR )));
5056 }
57+
58+ /**
59+ * 获取当前登录用户信息(支持双模式)
60+ * 优先级:
61+ * 1. SSO 模式:检查 OMS 请求头 (X-User-Name, X-User-Group-Id)
62+ * 2. JWT 模式:检查 Authorization Bearer Token
63+ * 3. 未登录:返回 authenticated=false
64+ *
65+ * @param request HTTP 请求
66+ * @return 用户信息(包含认证模式)
67+ */
68+ @ GetMapping ("/me" )
69+ public Response <UserResponse > getCurrentUser (HttpServletRequest request ) {
70+ // 优先检查 SSO 模式(OMS 请求头)
71+ String ssoUsername = request .getHeader ("X-User-Name" );
72+ String ssoGroupId = request .getHeader ("X-User-Group-Id" );
73+
74+ if (StringUtils .isNotBlank (ssoUsername )) {
75+ log .info ("SSO mode: user={}, groupId={}" , ssoUsername , ssoGroupId );
76+ return Response .ok (UserResponse .builder ()
77+ .username (ssoUsername )
78+ .groupId (ssoGroupId )
79+ .authenticated (true )
80+ .authMode ("SSO" )
81+ .build ());
82+ }
83+
84+ // 检查独立登录模式(JWT Token)
85+ String authHeader = request .getHeader ("Authorization" );
86+ if (authHeader != null && authHeader .startsWith ("Bearer " )) {
87+ String token = authHeader .substring (7 );
88+ String username = userService .validateToken (token );
89+
90+ if (StringUtils .isNotBlank (username )) {
91+ log .info ("JWT mode: user={}" , username );
92+ return Response .ok (UserResponse .builder ()
93+ .username (username )
94+ .authenticated (true )
95+ .authMode ("JWT" )
96+ .build ());
97+ }
98+ }
99+
100+ // 未登录
101+ log .debug ("User not authenticated" );
102+ return Response .ok (UserResponse .builder ()
103+ .authenticated (false )
104+ .authMode ("NONE" )
105+ .build ());
106+ }
51107}
0 commit comments