55import com .datamate .common .infrastructure .exception .CommonErrorCode ;
66import com .datamate .gateway .application .UserApplicationService ;
77import com .datamate .gateway .domain .service .UserService ;
8+ import com .datamate .gateway .infrastructure .client .OmsExtensionService ;
9+ import com .datamate .gateway .infrastructure .client .OmsService ;
810import com .datamate .gateway .interfaces .dto .LoginRequest ;
911import com .datamate .gateway .interfaces .dto .LoginResponse ;
1012import com .datamate .gateway .interfaces .dto .RegisterRequest ;
1618import org .springframework .http .HttpStatus ;
1719import org .springframework .http .ResponseEntity ;
1820import org .springframework .http .server .reactive .ServerHttpRequest ;
21+ import org .springframework .util .MultiValueMap ;
1922import org .springframework .validation .annotation .Validated ;
2023import org .springframework .web .bind .annotation .GetMapping ;
2124import org .springframework .web .bind .annotation .PostMapping ;
2225import org .springframework .web .bind .annotation .RequestBody ;
2326import org .springframework .web .bind .annotation .RequestMapping ;
2427import org .springframework .web .bind .annotation .RestController ;
28+ import org .springframework .http .HttpCookie ;
2529
2630/**
2731 * UserController
3640public class UserController {
3741 private final UserApplicationService userApplicationService ;
3842 private final UserService userService ;
43+ private final OmsService omsService ;
44+ private final OmsExtensionService omsExtensionService ;
45+
46+ private static final String AUTH_TOKEN_KEY = "__Host-X-Auth-Token" ;
47+ private static final String CSRF_TOKEN_KEY = "__Host-X-Csrf-Token" ;
48+
49+ /**
50+ * 从 cookies 中获取 token 值
51+ */
52+ private String getToken (MultiValueMap <String , HttpCookie > cookies , String tokenKey ) {
53+ if (cookies .containsKey (tokenKey )) {
54+ return cookies .getFirst (tokenKey ).getValue ();
55+ }
56+ return "" ;
57+ }
58+
59+ /**
60+ * 获取真实 IP 地址
61+ */
62+ private String getRealIp (ServerHttpRequest request ) {
63+ String ip = request .getHeaders ().getFirst ("X-Real-IP" );
64+ if (ip == null || ip .isEmpty () || "unknown" .equalsIgnoreCase (ip )) {
65+ ip = request .getHeaders ().getFirst ("X-Forwarded-For" );
66+ }
67+ if (ip == null || ip .isEmpty () || "unknown" .equalsIgnoreCase (ip )) {
68+ ip = request .getHeaders ().getFirst ("Proxy-Client-IP" );
69+ }
70+ if (ip == null || ip .isEmpty () || "unknown" .equalsIgnoreCase (ip )) {
71+ ip = request .getHeaders ().getFirst ("WL-Proxy-Client-IP" );
72+ }
73+ if (ip == null || ip .isEmpty () || "unknown" .equalsIgnoreCase (ip )) {
74+ ip = request .getRemoteAddress () != null ? request .getRemoteAddress ().getAddress ().getHostAddress () : "" ;
75+ }
76+ if (ip != null && ip .contains ("," )) {
77+ ip = ip .split ("," )[0 ].trim ();
78+ }
79+ return ip != null ? ip : "" ;
80+ }
3981
4082 @ PostMapping ("/login" )
4183 @ IgnoreResponseWrap
@@ -58,7 +100,7 @@ public ResponseEntity<Response<LoginResponse>> register(@Valid @RequestBody Regi
58100 /**
59101 * 获取当前登录用户信息(支持双模式)
60102 * 优先级:
61- * 1. SSO 模式:检查 OMS 请求头 (X-User-Name, X-User-Group-Id)
103+ * 1. SSO 模式:从 cookies 读取 OMS token 并调用 OMS 服务验证
62104 * 2. JWT 模式:检查 Authorization Bearer Token
63105 * 3. 未登录:返回 authenticated=false
64106 *
@@ -67,18 +109,48 @@ public ResponseEntity<Response<LoginResponse>> register(@Valid @RequestBody Regi
67109 */
68110 @ GetMapping ("/me" )
69111 public Response <UserResponse > getCurrentUser (ServerHttpRequest request ) {
70- // 优先检查 SSO 模式(OMS 请求头)
71- String ssoUsername = request .getHeaders ().getFirst ("X-User-Name" );
72- String ssoGroupId = request .getHeaders ().getFirst ("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 ());
112+ log .info ("=== /api/user/me called ===" );
113+
114+ // 优先检查 SSO 模式(从 cookies 读取 OMS token)
115+ MultiValueMap <String , HttpCookie > cookies = request .getCookies ();
116+ String authToken = getToken (cookies , AUTH_TOKEN_KEY );
117+ String csrfToken = getToken (cookies , CSRF_TOKEN_KEY );
118+
119+ log .info ("Cookies present - __Host-X-Auth-Token: {}, __Host-X-Csrf-Token: {}" ,
120+ StringUtils .isNotBlank (authToken ), StringUtils .isNotBlank (csrfToken ));
121+
122+ if (StringUtils .isNotBlank (authToken )) {
123+ try {
124+ // 获取真实 IP
125+ String realIp = getRealIp (request );
126+ log .info ("Calling OMS service with realIp: {}" , realIp );
127+
128+ // 调用 OMS 服务验证
129+ String username = omsService .getUserNameFromOms (authToken , csrfToken , realIp );
130+ if (StringUtils .isNotBlank (username )) {
131+ log .info ("SSO mode: user={}" , username );
132+
133+ // 获取用户组 ID(可能为 null)
134+ String groupId = null ;
135+ try {
136+ groupId = omsExtensionService .getUserGroupId (username );
137+ log .info ("User groupId: {}" , groupId );
138+ } catch (Exception e ) {
139+ log .warn ("Failed to get user group ID: {}" , e .getMessage ());
140+ }
141+
142+ return Response .ok (UserResponse .builder ()
143+ .username (username )
144+ .groupId (groupId )
145+ .authenticated (true )
146+ .authMode ("SSO" )
147+ .build ());
148+ } else {
149+ log .warn ("OMS service returned null username" );
150+ }
151+ } catch (Exception e ) {
152+ log .error ("SSO authentication failed" , e );
153+ }
82154 }
83155
84156 // 检查独立登录模式(JWT Token)
0 commit comments