Преглед изворни кода

增加登录成功返回值

zhouhao пре 7 година
родитељ
комит
34f49e6764

+ 13 - 2
hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/listener/event/AuthorizationSuccessEvent.java

@@ -20,6 +20,8 @@ package org.hswebframework.web.authorization.listener.event;
 
 import org.hswebframework.web.authorization.Authentication;
 
+import java.util.HashMap;
+import java.util.Map;
 import java.util.Optional;
 import java.util.function.Function;
 
@@ -33,7 +35,9 @@ import java.util.function.Function;
 public class AuthorizationSuccessEvent implements AuthorizationEvent {
     private Authentication authentication;
 
-    private Function<String,Object> parameterGetter;
+    private Function<String, Object> parameterGetter;
+
+    private Map<String, Object> result = new HashMap<>();
 
     public AuthorizationSuccessEvent(Authentication authentication, Function<String, Object> parameterGetter) {
         this.authentication = authentication;
@@ -45,8 +49,15 @@ public class AuthorizationSuccessEvent implements AuthorizationEvent {
     }
 
     @SuppressWarnings("unchecked")
-    public  <T> Optional<T> getParameter(String name) {
+    public <T> Optional<T> getParameter(String name) {
         return Optional.ofNullable((T) parameterGetter.apply(name));
     }
 
+    public Map<String, Object> getResult() {
+        return result;
+    }
+
+    public void setResult(Map<String, Object> result) {
+        this.result = result;
+    }
 }

+ 7 - 5
hsweb-system/hsweb-system-authorization/hsweb-system-authorization-controller/src/main/java/org/hswebframework/web/controller/authorization/AuthorizationController.java

@@ -39,6 +39,7 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.*;
 
 import javax.servlet.http.HttpServletRequest;
+import java.util.Map;
 import java.util.function.Function;
 
 import static org.hswebframework.web.controller.message.ResponseMessage.ok;
@@ -61,7 +62,7 @@ public class AuthorizationController {
 //    private AuthenticationInitializeService authenticationInitializeService;
 
     @Autowired
-    private AuthenticationManager authenticationManager;
+    private AuthenticationManager           authenticationManager;
     @Autowired
     private AuthorizationListenerDispatcher authorizationListenerDispatcher;
 
@@ -82,9 +83,9 @@ public class AuthorizationController {
 
     @PostMapping(value = "/login")
     @ApiOperation("用户名密码登录")
-    public ResponseMessage<String> authorize(@RequestParam @ApiParam("用户名") String username,
-                                             @RequestParam @ApiParam("密码") String password,
-                                             @ApiParam(hidden = true) HttpServletRequest request) {
+    public ResponseMessage<Map<String, Object>> authorize(@RequestParam @ApiParam("用户名") String username,
+                                                          @RequestParam @ApiParam("密码") String password,
+                                                          @ApiParam(hidden = true) HttpServletRequest request) {
 
         AuthorizationFailedEvent.Reason reason = AuthorizationFailedEvent.Reason.OTHER;
         Function<String, Object> parameterGetter = request::getParameter;
@@ -113,11 +114,12 @@ public class AuthorizationController {
             // 验证通过
             Authentication authentication = authenticationManager.getByUserId(entity.getId());
             AuthorizationSuccessEvent event = new AuthorizationSuccessEvent(authentication, parameterGetter);
+            event.getResult().put("userId", entity.getId());
             int size = authorizationListenerDispatcher.doEvent(event);
             if (size == 0) {
                 logger.warn("not found any AuthorizationSuccessEvent,access control maybe disabled!");
             }
-            return ok(entity.getId());
+            return ok(event.getResult());
         } catch (Exception e) {
             AuthorizationFailedEvent failedEvent = new AuthorizationFailedEvent(username, password, parameterGetter, reason);
             failedEvent.setException(e);