mirror of
https://gitee.com/dromara/RuoYi-Cloud-Plus.git
synced 2025-09-03 11:05:58 +00:00
update 完善auth服务 重构用户注册流程 补全接口文档
This commit is contained in:
@@ -11,7 +11,7 @@
|
||||
<artifactId>ruoyi-auth</artifactId>
|
||||
|
||||
<description>
|
||||
ruoyi-auth认证授权中心
|
||||
ruoyi-auth 认证授权中心
|
||||
</description>
|
||||
|
||||
<dependencies>
|
||||
@@ -46,6 +46,12 @@
|
||||
<artifactId>ruoyi-common-security</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- RuoYi Common Swagger -->
|
||||
<dependency>
|
||||
<groupId>com.ruoyi</groupId>
|
||||
<artifactId>ruoyi-common-swagger</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.ruoyi</groupId>
|
||||
<artifactId>ruoyi-common-web</artifactId>
|
||||
|
@@ -9,29 +9,34 @@ import com.ruoyi.common.core.domain.R;
|
||||
import com.ruoyi.common.core.enums.DeviceType;
|
||||
import com.ruoyi.common.satoken.utils.LoginHelper;
|
||||
import com.ruoyi.system.api.model.LoginUser;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* token 控制
|
||||
*
|
||||
* @author ruoyi
|
||||
* @author Lion Li
|
||||
*/
|
||||
@Validated
|
||||
@Api(value = "认证鉴权控制器", tags = {"认证鉴权管理"})
|
||||
@RequiredArgsConstructor
|
||||
@RestController
|
||||
public class TokenController {
|
||||
|
||||
private final SysLoginService sysLoginService;
|
||||
|
||||
@ApiOperation("登录方法")
|
||||
@PostMapping("login")
|
||||
public R<?> login(@RequestBody LoginBody form) {
|
||||
public R<Map<String, Object>> login(@Validated @RequestBody LoginBody form) {
|
||||
// 用户登录
|
||||
LoginUser userInfo = sysLoginService.login(form.getUsername(), form.getPassword());
|
||||
// 获取登录token
|
||||
@@ -42,19 +47,23 @@ public class TokenController {
|
||||
return R.ok(rspMap);
|
||||
}
|
||||
|
||||
@ApiOperation("登出方法")
|
||||
@DeleteMapping("logout")
|
||||
public R<?> logout(HttpServletRequest request) {
|
||||
public R<Void> logout() {
|
||||
try {
|
||||
StpUtil.logout();
|
||||
sysLoginService.logout(LoginHelper.getUsername());
|
||||
} catch (NotLoginException e) {
|
||||
}
|
||||
return R.ok();
|
||||
}
|
||||
|
||||
@ApiOperation("用户注册")
|
||||
@PostMapping("register")
|
||||
public R<?> register(@RequestBody RegisterBody registerBody) {
|
||||
public R<Void> register(@RequestBody RegisterBody registerBody) {
|
||||
// 用户注册
|
||||
sysLoginService.register(registerBody.getUsername(), registerBody.getPassword());
|
||||
sysLoginService.register(registerBody);
|
||||
return R.ok();
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -1,26 +1,38 @@
|
||||
package com.ruoyi.auth.form;
|
||||
|
||||
import com.ruoyi.common.core.constant.UserConstants;
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.experimental.Accessors;
|
||||
import org.hibernate.validator.constraints.Length;
|
||||
|
||||
import javax.validation.constraints.NotBlank;
|
||||
|
||||
/**
|
||||
* 用户登录对象
|
||||
*
|
||||
* @author ruoyi
|
||||
* @author Lion Li
|
||||
*/
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@ApiModel("用户登录对象")
|
||||
public class LoginBody {
|
||||
|
||||
/**
|
||||
* 用户名
|
||||
*/
|
||||
@NotBlank(message = "用户名不能为空")
|
||||
@Length(min = UserConstants.USERNAME_MIN_LENGTH, max = UserConstants.USERNAME_MAX_LENGTH, message = "账户长度必须在2到20个字符之间")
|
||||
@ApiModelProperty(value = "用户名")
|
||||
private String username;
|
||||
|
||||
/**
|
||||
* 用户密码
|
||||
*/
|
||||
@NotBlank(message = "密码不能为空")
|
||||
@Length(min = UserConstants.PASSWORD_MIN_LENGTH, max = UserConstants.PASSWORD_MAX_LENGTH, message = "密码长度必须在5到20个字符之间")
|
||||
@ApiModelProperty(value = "用户密码")
|
||||
private String password;
|
||||
|
||||
}
|
||||
|
@@ -1,10 +1,21 @@
|
||||
package com.ruoyi.auth.form;
|
||||
|
||||
/**
|
||||
* 用户注册对象
|
||||
*
|
||||
* @author ruoyi
|
||||
*/
|
||||
public class RegisterBody extends LoginBody {
|
||||
|
||||
}
|
||||
package com.ruoyi.auth.form;
|
||||
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
|
||||
/**
|
||||
* 用户注册对象
|
||||
*
|
||||
* @author Lion Li
|
||||
*/
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@ApiModel("用户注册对象")
|
||||
public class RegisterBody extends LoginBody {
|
||||
|
||||
@ApiModelProperty(value = "用户类型")
|
||||
private String userType;
|
||||
|
||||
}
|
||||
|
@@ -2,9 +2,11 @@ package com.ruoyi.auth.service;
|
||||
|
||||
import cn.dev33.satoken.secure.BCrypt;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import com.ruoyi.auth.form.RegisterBody;
|
||||
import com.ruoyi.common.core.constant.CacheConstants;
|
||||
import com.ruoyi.common.core.constant.Constants;
|
||||
import com.ruoyi.common.core.constant.UserConstants;
|
||||
import com.ruoyi.common.core.enums.UserType;
|
||||
import com.ruoyi.common.core.exception.ServiceException;
|
||||
import com.ruoyi.common.core.utils.ServletUtils;
|
||||
import com.ruoyi.common.core.utils.StringUtils;
|
||||
@@ -106,27 +108,25 @@ public class SysLoginService {
|
||||
/**
|
||||
* 注册
|
||||
*/
|
||||
public void register(String username, String password) {
|
||||
// 用户名或密码为空 错误
|
||||
if (StringUtils.isAnyBlank(username, password)) {
|
||||
throw new ServiceException("用户/密码必须填写");
|
||||
}
|
||||
if (username.length() < UserConstants.USERNAME_MIN_LENGTH
|
||||
|| username.length() > UserConstants.USERNAME_MAX_LENGTH) {
|
||||
throw new ServiceException("账户长度必须在2到20个字符之间");
|
||||
}
|
||||
if (password.length() < UserConstants.PASSWORD_MIN_LENGTH
|
||||
|| password.length() > UserConstants.PASSWORD_MAX_LENGTH) {
|
||||
throw new ServiceException("密码长度必须在5到20个字符之间");
|
||||
}
|
||||
public void register(RegisterBody registerBody) {
|
||||
String username = registerBody.getUsername();
|
||||
String password = registerBody.getPassword();
|
||||
// 校验用户类型是否存在
|
||||
String userType = UserType.getUserType(registerBody.getUserType()).getUserType();
|
||||
|
||||
if (UserConstants.NOT_UNIQUE.equals(remoteUserService.checkUserNameUnique(username))) {
|
||||
throw new ServiceException("保存用户 " + username + " 失败,注册账号已存在");
|
||||
}
|
||||
// 注册用户信息
|
||||
SysUser sysUser = new SysUser();
|
||||
sysUser.setUserName(username);
|
||||
sysUser.setNickName(username);
|
||||
sysUser.setPassword(BCrypt.hashpw(password));
|
||||
remoteUserService.registerUserInfo(sysUser);
|
||||
|
||||
sysUser.setUserType(userType);
|
||||
boolean regFlag = remoteUserService.registerUserInfo(sysUser);
|
||||
if (!regFlag) {
|
||||
throw new ServiceException("注册失败,请联系系统管理人员");
|
||||
}
|
||||
recordLogininfor(username, Constants.REGISTER, "注册成功");
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user