Browse Source

注册时自动登陆

mingfu 1 month ago
parent
commit
a90d4df9db

+ 10 - 2
ie-admin/src/main/java/com/ruoyi/web/controller/system/SysRegisterController.java

@@ -1,7 +1,9 @@
 package com.ruoyi.web.controller.system;
 
 import com.ruoyi.common.annotation.Anonymous;
+import com.ruoyi.common.constant.Constants;
 import com.ruoyi.common.utils.SecurityUtils;
+import com.ruoyi.web.service.SysLoginService;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -28,7 +30,7 @@ public class SysRegisterController extends BaseController
     private SysRegisterService registerService;
 
     @Autowired
-    private ISysConfigService configService;
+    private SysLoginService sysLoginService;
 
     @PostMapping("/registry")
     @ApiOperation("注册完善")
@@ -36,7 +38,13 @@ public class SysRegisterController extends BaseController
     public AjaxResult registry(@RequestBody RegisterBody user)
     {
         String msg = registerService.register(user, null);
-        return StringUtils.isEmpty(msg) ? success() : error(msg);
+        if(StringUtils.isEmpty(msg)) {
+            // 生成token
+            AjaxResult ajax = AjaxResult.success();
+            ajax.put(Constants.TOKEN, sysLoginService.loginInner(user.getUsername(), user.getPassword()));
+            return ajax;
+        }
+        return error(msg);
     }
 
     @PostMapping("/improve")

+ 35 - 0
ie-admin/src/main/java/com/ruoyi/web/service/SysLoginService.java

@@ -133,6 +133,41 @@ public class SysLoginService
         return ajax;
     }
 
+    /**
+     * 内部自动登陆,不记录日志
+     * @param username
+     * @param password
+     * @return
+     */
+    public String loginInner(String username, String password) {
+        // 用户验证
+        Authentication authentication = null;
+        try
+        {
+            UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(username, password);
+            AuthenticationContextHolder.setContext(authenticationToken);
+            // 该方法会去调用UserDetailsServiceImpl.loadUserByUsername
+            authentication = authenticationManager.authenticate(authenticationToken);
+        }
+        catch (Exception e)
+        {
+            if (e instanceof BadCredentialsException)
+            {
+                throw new UserPasswordNotMatchException();
+            }
+            else
+            {
+                throw new ServiceException(e.getMessage());
+            }
+        }
+        finally
+        {
+            AuthenticationContextHolder.clearContext();
+        }
+        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
+        return tokenService.createToken(loginUser);
+    }
+
     /**
      * 校验验证码
      * 

+ 2 - 0
ie-admin/src/main/java/com/ruoyi/web/service/SysRegisterService.java

@@ -71,6 +71,8 @@ public class SysRegisterService
             } else {
                 username = mobile;
                 password = "123456";
+                registerBody.setUsername(username); // TODO 带回去重登陆
+                registerBody.setPassword(password); // TODO 带回去重登陆
             }
             upUser.setPassword2(SecurityUtils.encryptPassword2(password));
             upUser.setPassword(SecurityUtils.encryptPassword(password));