Browse Source

单点登录

王先生 2 years ago
parent
commit
adddbbe90c

+ 27 - 0
eladmin-system/src/main/java/me/zhengjie/modules/security/rest/AuthorizationController.java

@@ -106,6 +106,33 @@ public class AuthorizationController {
         return ResponseEntity.ok(authInfo);
     }
 
+    @ApiOperation("登录授权")
+    @AnonymousPostMapping(value = "/login2")
+    public ResponseEntity<Object> login2(@Validated @RequestBody AuthUserDto authUser, HttpServletRequest request) throws Exception {
+        System.out.println("authUser.getPassword():"+authUser.getPassword());
+        // 密码解密
+        String password = RsaUtils.decryptByPrivateKey(RsaProperties.privateKey, authUser.getPassword());
+        UsernamePasswordAuthenticationToken authenticationToken =
+                new UsernamePasswordAuthenticationToken(authUser.getUsername(), password);
+        Authentication authentication = authenticationManagerBuilder.getObject().authenticate(authenticationToken);
+        SecurityContextHolder.getContext().setAuthentication(authentication);
+        // 生成令牌
+        String token = tokenProvider.createToken(authentication);
+        final JwtUserDto jwtUserDto = (JwtUserDto) authentication.getPrincipal();
+        // 保存在线信息
+        onlineUserService.save(jwtUserDto, token, request);
+        // 返回 token 与 用户信息
+        Map<String, Object> authInfo = new HashMap<String, Object>(2) {{
+            put("token", properties.getTokenStartWith() + token);
+            put("user", jwtUserDto);
+        }};
+        if (loginProperties.isSingleLogin()) {
+            //踢掉之前已经登录的token
+            onlineUserService.checkLoginOnUser(authUser.getUsername(), token);
+        }
+        return ResponseEntity.ok(authInfo);
+    }
+
     @ApiOperation("获取用户信息")
     @GetMapping(value = "/info")
     public ResponseEntity<Object> getUserInfo() {