update 完善auth服务 重构用户注册流程 补全接口文档
parent
27087e5d1e
commit
ba635272fe
|
|
@ -25,4 +25,12 @@ public interface RemoteUserService {
|
||||||
* @return 结果
|
* @return 结果
|
||||||
*/
|
*/
|
||||||
Boolean registerUserInfo(SysUser sysUser);
|
Boolean registerUserInfo(SysUser sysUser);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检查用户名是否唯一
|
||||||
|
*
|
||||||
|
* @param username 用户名
|
||||||
|
* @return 结果
|
||||||
|
*/
|
||||||
|
String checkUserNameUnique(String username);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,7 @@
|
||||||
<artifactId>ruoyi-auth</artifactId>
|
<artifactId>ruoyi-auth</artifactId>
|
||||||
|
|
||||||
<description>
|
<description>
|
||||||
ruoyi-auth认证授权中心
|
ruoyi-auth 认证授权中心
|
||||||
</description>
|
</description>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
|
@ -46,6 +46,12 @@
|
||||||
<artifactId>ruoyi-common-security</artifactId>
|
<artifactId>ruoyi-common-security</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<!-- RuoYi Common Swagger -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.ruoyi</groupId>
|
||||||
|
<artifactId>ruoyi-common-swagger</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.ruoyi</groupId>
|
<groupId>com.ruoyi</groupId>
|
||||||
<artifactId>ruoyi-common-web</artifactId>
|
<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.core.enums.DeviceType;
|
||||||
import com.ruoyi.common.satoken.utils.LoginHelper;
|
import com.ruoyi.common.satoken.utils.LoginHelper;
|
||||||
import com.ruoyi.system.api.model.LoginUser;
|
import com.ruoyi.system.api.model.LoginUser;
|
||||||
|
import io.swagger.annotations.Api;
|
||||||
|
import io.swagger.annotations.ApiOperation;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.validation.annotation.Validated;
|
||||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestBody;
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* token 控制
|
* token 控制
|
||||||
*
|
*
|
||||||
* @author ruoyi
|
* @author Lion Li
|
||||||
*/
|
*/
|
||||||
|
@Validated
|
||||||
|
@Api(value = "认证鉴权控制器", tags = {"认证鉴权管理"})
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
@RestController
|
@RestController
|
||||||
public class TokenController {
|
public class TokenController {
|
||||||
|
|
||||||
private final SysLoginService sysLoginService;
|
private final SysLoginService sysLoginService;
|
||||||
|
|
||||||
|
@ApiOperation("登录方法")
|
||||||
@PostMapping("login")
|
@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());
|
LoginUser userInfo = sysLoginService.login(form.getUsername(), form.getPassword());
|
||||||
// 获取登录token
|
// 获取登录token
|
||||||
|
|
@ -42,19 +47,23 @@ public class TokenController {
|
||||||
return R.ok(rspMap);
|
return R.ok(rspMap);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ApiOperation("登出方法")
|
||||||
@DeleteMapping("logout")
|
@DeleteMapping("logout")
|
||||||
public R<?> logout(HttpServletRequest request) {
|
public R<Void> logout() {
|
||||||
try {
|
try {
|
||||||
StpUtil.logout();
|
StpUtil.logout();
|
||||||
|
sysLoginService.logout(LoginHelper.getUsername());
|
||||||
} catch (NotLoginException e) {
|
} catch (NotLoginException e) {
|
||||||
}
|
}
|
||||||
return R.ok();
|
return R.ok();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ApiOperation("用户注册")
|
||||||
@PostMapping("register")
|
@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();
|
return R.ok();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,26 +1,38 @@
|
||||||
package com.ruoyi.auth.form;
|
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.Data;
|
||||||
import lombok.NoArgsConstructor;
|
import lombok.NoArgsConstructor;
|
||||||
import lombok.experimental.Accessors;
|
import org.hibernate.validator.constraints.Length;
|
||||||
|
|
||||||
|
import javax.validation.constraints.NotBlank;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 用户登录对象
|
* 用户登录对象
|
||||||
*
|
*
|
||||||
* @author ruoyi
|
* @author Lion Li
|
||||||
*/
|
*/
|
||||||
@Data
|
@Data
|
||||||
@NoArgsConstructor
|
@NoArgsConstructor
|
||||||
|
@ApiModel("用户登录对象")
|
||||||
public class LoginBody {
|
public class LoginBody {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 用户名
|
* 用户名
|
||||||
*/
|
*/
|
||||||
|
@NotBlank(message = "用户名不能为空")
|
||||||
|
@Length(min = UserConstants.USERNAME_MIN_LENGTH, max = UserConstants.USERNAME_MAX_LENGTH, message = "账户长度必须在2到20个字符之间")
|
||||||
|
@ApiModelProperty(value = "用户名")
|
||||||
private String username;
|
private String username;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 用户密码
|
* 用户密码
|
||||||
*/
|
*/
|
||||||
|
@NotBlank(message = "密码不能为空")
|
||||||
|
@Length(min = UserConstants.PASSWORD_MIN_LENGTH, max = UserConstants.PASSWORD_MAX_LENGTH, message = "密码长度必须在5到20个字符之间")
|
||||||
|
@ApiModelProperty(value = "用户密码")
|
||||||
private String password;
|
private String password;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,21 @@
|
||||||
package com.ruoyi.auth.form;
|
package com.ruoyi.auth.form;
|
||||||
|
|
||||||
/**
|
import io.swagger.annotations.ApiModel;
|
||||||
* 用户注册对象
|
import io.swagger.annotations.ApiModelProperty;
|
||||||
*
|
import lombok.Data;
|
||||||
* @author ruoyi
|
import lombok.EqualsAndHashCode;
|
||||||
*/
|
|
||||||
public class RegisterBody extends LoginBody {
|
/**
|
||||||
|
* 用户注册对象
|
||||||
}
|
*
|
||||||
|
* @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.dev33.satoken.secure.BCrypt;
|
||||||
import cn.hutool.core.util.ObjectUtil;
|
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.CacheConstants;
|
||||||
import com.ruoyi.common.core.constant.Constants;
|
import com.ruoyi.common.core.constant.Constants;
|
||||||
import com.ruoyi.common.core.constant.UserConstants;
|
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.exception.ServiceException;
|
||||||
import com.ruoyi.common.core.utils.ServletUtils;
|
import com.ruoyi.common.core.utils.ServletUtils;
|
||||||
import com.ruoyi.common.core.utils.StringUtils;
|
import com.ruoyi.common.core.utils.StringUtils;
|
||||||
|
|
@ -106,27 +108,25 @@ public class SysLoginService {
|
||||||
/**
|
/**
|
||||||
* 注册
|
* 注册
|
||||||
*/
|
*/
|
||||||
public void register(String username, String password) {
|
public void register(RegisterBody registerBody) {
|
||||||
// 用户名或密码为空 错误
|
String username = registerBody.getUsername();
|
||||||
if (StringUtils.isAnyBlank(username, password)) {
|
String password = registerBody.getPassword();
|
||||||
throw new ServiceException("用户/密码必须填写");
|
// 校验用户类型是否存在
|
||||||
}
|
String userType = UserType.getUserType(registerBody.getUserType()).getUserType();
|
||||||
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个字符之间");
|
|
||||||
}
|
|
||||||
|
|
||||||
|
if (UserConstants.NOT_UNIQUE.equals(remoteUserService.checkUserNameUnique(username))) {
|
||||||
|
throw new ServiceException("保存用户 " + username + " 失败,注册账号已存在");
|
||||||
|
}
|
||||||
// 注册用户信息
|
// 注册用户信息
|
||||||
SysUser sysUser = new SysUser();
|
SysUser sysUser = new SysUser();
|
||||||
sysUser.setUserName(username);
|
sysUser.setUserName(username);
|
||||||
sysUser.setNickName(username);
|
sysUser.setNickName(username);
|
||||||
sysUser.setPassword(BCrypt.hashpw(password));
|
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, "注册成功");
|
recordLogininfor(username, Constants.REGISTER, "注册成功");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,6 @@ package com.ruoyi.system.dubbo;
|
||||||
|
|
||||||
import cn.hutool.core.bean.BeanUtil;
|
import cn.hutool.core.bean.BeanUtil;
|
||||||
import cn.hutool.core.util.ObjectUtil;
|
import cn.hutool.core.util.ObjectUtil;
|
||||||
import com.ruoyi.common.core.constant.Constants;
|
|
||||||
import com.ruoyi.common.core.constant.UserConstants;
|
import com.ruoyi.common.core.constant.UserConstants;
|
||||||
import com.ruoyi.common.core.enums.UserStatus;
|
import com.ruoyi.common.core.enums.UserStatus;
|
||||||
import com.ruoyi.common.core.exception.ServiceException;
|
import com.ruoyi.common.core.exception.ServiceException;
|
||||||
|
|
@ -75,4 +74,9 @@ public class RemoteUserServiceImpl implements RemoteUserService {
|
||||||
}
|
}
|
||||||
return userService.registerUser(sysUser);
|
return userService.registerUser(sysUser);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String checkUserNameUnique(String username) {
|
||||||
|
return userService.checkUserNameUnique(username);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -18,3 +18,6 @@ knife4j:
|
||||||
- name: 资源服务
|
- name: 资源服务
|
||||||
uri: ${knife4j.cloud.gatewayUri}
|
uri: ${knife4j.cloud.gatewayUri}
|
||||||
location: /resource/v2/api-docs
|
location: /resource/v2/api-docs
|
||||||
|
- name: 认证服务
|
||||||
|
uri: ${knife4j.cloud.gatewayUri}
|
||||||
|
location: /auth/v2/api-docs
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue