11package com .github .cadecode .uniboot .framework .svc .controller ;
22
3+ import cn .hutool .core .io .IoUtil ;
4+ import cn .hutool .core .util .CharsetUtil ;
5+ import com .github .cadecode .uniboot .common .core .util .JacksonUtil ;
36import com .github .cadecode .uniboot .common .core .web .response .ApiResult ;
47import com .github .cadecode .uniboot .common .plugin .log .annotation .ApiLogger ;
58import com .github .cadecode .uniboot .common .plugin .log .enums .LogTypeEnum ;
69import com .github .cadecode .uniboot .framework .api .annotation .ApiFormat ;
10+ import com .github .cadecode .uniboot .framework .api .util .SecurityUtil ;
11+ import com .github .cadecode .uniboot .framework .svc .config .FrameSecurityConfig ;
12+ import feign .Response ;
713import io .swagger .annotations .Api ;
814import io .swagger .annotations .ApiOperation ;
915import lombok .RequiredArgsConstructor ;
1420import org .springframework .web .bind .annotation .RequestParam ;
1521import org .springframework .web .bind .annotation .RestController ;
1622
23+ import javax .servlet .http .HttpServletResponse ;
24+ import java .io .IOException ;
25+
1726/**
1827 * 认证API
1928 *
@@ -33,8 +42,14 @@ public class AuthController {
3342 @ ApiLogger (type = LogTypeEnum .AUTH , enableSave = true , description = "用户登录" )
3443 @ ApiOperation ("登录" )
3544 @ PostMapping ("login" )
36- public ApiResult <?> login (@ RequestParam String username , @ RequestParam String password ) {
37- return authClient .login (username , password );
45+ public ApiResult <?> login (HttpServletResponse sResponse , @ RequestParam String username , @ RequestParam String password ) throws IOException {
46+ // 从 rpc 调用中获取 header token 设置到响应
47+ Response rResponse = authClient .login (username , password );
48+ String token = rResponse .headers ().get (SecurityUtil .getHeader ()).stream ().findFirst ().get ();
49+ sResponse .addHeader (SecurityUtil .getHeader (), token );
50+ // 获取 rpc 结果
51+ String body = IoUtil .read (rResponse .body ().asReader (CharsetUtil .CHARSET_UTF_8 ));
52+ return JacksonUtil .toBean (body , ApiResult .class );
3853 }
3954
4055 @ ApiLogger (type = LogTypeEnum .AUTH , enableSave = true , description = "用户注销" )
@@ -52,10 +67,10 @@ public ApiResult<?> logout() {
5267 @ FeignClient (contextId = "AuthClient" , name = "uni-boot-framework" )
5368 public interface AuthClient {
5469
55- @ PostMapping ("login" )
56- ApiResult <?> login (@ RequestParam ("username" ) String username , @ RequestParam ("password" ) String password );
70+ @ PostMapping (FrameSecurityConfig . LOGIN_URL )
71+ Response login (@ RequestParam ("username" ) String username , @ RequestParam ("password" ) String password );
5772
58- @ PostMapping ("logout" )
73+ @ PostMapping (FrameSecurityConfig . LOGOUT_URL )
5974 ApiResult <?> logout ();
6075 }
6176}
0 commit comments