增加 user 使用 mobile 认证的逻辑
This commit is contained in:
@@ -10,7 +10,7 @@ import cn.iocoder.mall.system.biz.service.admin.AdminService;
|
||||
import cn.iocoder.mall.system.biz.service.oauth2.OAuth2Service;
|
||||
import cn.iocoder.mall.system.rest.convert.oauth2.AdminsOAuth2Convert;
|
||||
import cn.iocoder.mall.system.rest.request.oauth2.AdminsOAuth2UsernameAuthenticateRequest;
|
||||
import cn.iocoder.mall.system.rest.response.AdminsAuthorizeUsernameLoginResponse;
|
||||
import cn.iocoder.mall.system.rest.response.oauth2.AdminsOAuth2AuthenticateResponse;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -32,10 +32,10 @@ public class AdminsOAuth2Controller {
|
||||
|
||||
@PostMapping("/username_authenticate")
|
||||
@ApiOperation("用户名认证")
|
||||
public CommonResult<AdminsAuthorizeUsernameLoginResponse> usernameAuthenticate(AdminsOAuth2UsernameAuthenticateRequest request) {
|
||||
public CommonResult<AdminsOAuth2AuthenticateResponse> usernameAuthenticate(AdminsOAuth2UsernameAuthenticateRequest request) {
|
||||
// 执行认证
|
||||
OAuth2UsernameAuthenticateDTO usernameAuthenticateDTO = AdminsOAuth2Convert.INSTANCE.convert(request);
|
||||
OAuth2AccessTokenBO accessTokenBO = oauth2Service.authenticate(usernameAuthenticateDTO);
|
||||
OAuth2UsernameAuthenticateDTO authenticateDTO = AdminsOAuth2Convert.INSTANCE.convert(request);
|
||||
OAuth2AccessTokenBO accessTokenBO = oauth2Service.authenticate(authenticateDTO);
|
||||
// 获得 Admin 信息
|
||||
AdminBO adminBO = adminService.get(accessTokenBO.getAccountId());
|
||||
if (adminBO == null) {
|
||||
|
||||
@@ -3,10 +3,15 @@ package cn.iocoder.mall.system.rest.controller.users;
|
||||
import cn.iocoder.common.framework.constant.MallConstants;
|
||||
import cn.iocoder.common.framework.util.HttpUtil;
|
||||
import cn.iocoder.common.framework.vo.CommonResult;
|
||||
import cn.iocoder.mall.system.biz.bo.user.UserAuthenticateBO;
|
||||
import cn.iocoder.mall.system.biz.dto.oatuh2.OAuth2MobileCodeAuthenticateDTO;
|
||||
import cn.iocoder.mall.system.biz.dto.oatuh2.OAuth2MobileCodeSendDTO;
|
||||
import cn.iocoder.mall.system.biz.service.oauth2.OAuth2MobileCodeService;
|
||||
import cn.iocoder.mall.system.biz.service.oauth2.OAuth2Service;
|
||||
import cn.iocoder.mall.system.biz.service.user.UserService;
|
||||
import cn.iocoder.mall.system.rest.convert.oauth2.UsersOAuth2Convert;
|
||||
import cn.iocoder.mall.system.rest.request.oauth2.UsersOAuth2MobileCodeAuthenticateRequest;
|
||||
import cn.iocoder.mall.system.rest.response.user.UsersOAuth2AuthenticateResponse;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiImplicitParam;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
@@ -30,6 +35,20 @@ public class UsersOAuth2Controller {
|
||||
@Autowired
|
||||
private OAuth2MobileCodeService oauth2MobileCodeService;
|
||||
|
||||
@PostMapping("/mobile_code_authenticate")
|
||||
@ApiOperation("手机验证码认证")
|
||||
public CommonResult<UsersOAuth2AuthenticateResponse> mobileCodeAuthenticate(UsersOAuth2MobileCodeAuthenticateRequest request,
|
||||
HttpServletRequest httpRequest) {
|
||||
// 执行认证
|
||||
OAuth2MobileCodeAuthenticateDTO authenticateDTO = UsersOAuth2Convert.INSTANCE.convert(request)
|
||||
.setIp(HttpUtil.getIp(httpRequest));
|
||||
UserAuthenticateBO userAuthenticateBO = userService.authenticate(authenticateDTO);
|
||||
// 转换返回
|
||||
return CommonResult.success(
|
||||
UsersOAuth2Convert.INSTANCE.convert(userAuthenticateBO)
|
||||
);
|
||||
}
|
||||
|
||||
@PostMapping("/send_mobile_code")
|
||||
@ApiOperation("发送手机验证码")
|
||||
@ApiImplicitParam(name = "mobile", value = "手机号", required = true, example = "15601691234")
|
||||
@@ -38,7 +57,7 @@ public class UsersOAuth2Controller {
|
||||
// 执行发送验证码
|
||||
OAuth2MobileCodeSendDTO sendDTO = new OAuth2MobileCodeSendDTO()
|
||||
.setMobile(mobile).setIp(HttpUtil.getIp(request));
|
||||
oauth2MobileCodeService.sendMobileCode(sendDTO);
|
||||
oauth2MobileCodeService.send(sendDTO);
|
||||
// 返回成功
|
||||
return CommonResult.success(true);
|
||||
}
|
||||
|
||||
@@ -4,10 +4,9 @@ import cn.iocoder.mall.system.biz.bo.admin.AdminBO;
|
||||
import cn.iocoder.mall.system.biz.bo.ouath2.OAuth2AccessTokenBO;
|
||||
import cn.iocoder.mall.system.biz.dto.oatuh2.OAuth2UsernameAuthenticateDTO;
|
||||
import cn.iocoder.mall.system.rest.request.oauth2.AdminsOAuth2UsernameAuthenticateRequest;
|
||||
import cn.iocoder.mall.system.rest.response.AdminsAuthorizeUsernameLoginResponse;
|
||||
import cn.iocoder.mall.system.rest.response.oauth2.AdminsOAuth2AuthenticateResponse;
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.Mapping;
|
||||
import org.mapstruct.Mappings;
|
||||
import org.mapstruct.factory.Mappers;
|
||||
|
||||
@Mapper
|
||||
@@ -17,13 +16,10 @@ public interface AdminsOAuth2Convert {
|
||||
|
||||
OAuth2UsernameAuthenticateDTO convert(AdminsOAuth2UsernameAuthenticateRequest request);
|
||||
|
||||
@Mappings(value = {
|
||||
@Mapping(source = "adminBO.id", target = "id"),
|
||||
@Mapping(source = "adminBO.name", target = "name"),
|
||||
@Mapping(source = "accessTokenBO.id", target = "token.accessToken"),
|
||||
@Mapping(source = "accessTokenBO.refreshToken", target = "token.refreshToken"),
|
||||
@Mapping(source = "accessTokenBO.expiresTime", target = "token.expiresTime"),
|
||||
})
|
||||
AdminsAuthorizeUsernameLoginResponse convert(AdminBO adminBO, OAuth2AccessTokenBO accessTokenBO);
|
||||
@Mapping(source = "adminBO", target = "admin")
|
||||
@Mapping(source = "accessTokenBO.id", target = "token.accessToken")
|
||||
@Mapping(source = "accessTokenBO.refreshToken", target = "token.refreshToken")
|
||||
@Mapping(source = "accessTokenBO.expiresTime", target = "token.expiresTime")
|
||||
AdminsOAuth2AuthenticateResponse convert(AdminBO adminBO, OAuth2AccessTokenBO accessTokenBO);
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
package cn.iocoder.mall.system.rest.convert.oauth2;
|
||||
|
||||
import cn.iocoder.mall.system.biz.bo.user.UserAuthenticateBO;
|
||||
import cn.iocoder.mall.system.biz.dto.oatuh2.OAuth2MobileCodeAuthenticateDTO;
|
||||
import cn.iocoder.mall.system.rest.request.oauth2.UsersOAuth2MobileCodeAuthenticateRequest;
|
||||
import cn.iocoder.mall.system.rest.response.user.UsersOAuth2AuthenticateResponse;
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.Mapping;
|
||||
import org.mapstruct.factory.Mappers;
|
||||
|
||||
@Mapper
|
||||
public interface UsersOAuth2Convert {
|
||||
|
||||
UsersOAuth2Convert INSTANCE = Mappers.getMapper(UsersOAuth2Convert.class);
|
||||
|
||||
OAuth2MobileCodeAuthenticateDTO convert(UsersOAuth2MobileCodeAuthenticateRequest request);
|
||||
|
||||
@Mapping(source = "token.id", target = "token.accessToken")
|
||||
UsersOAuth2AuthenticateResponse convert(UserAuthenticateBO userAuthenticateBO);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package cn.iocoder.mall.system.rest.convert.user;
|
||||
|
||||
import org.mapstruct.Mapper;
|
||||
|
||||
@Mapper
|
||||
public interface UsersUserConvert {
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package cn.iocoder.mall.system.rest.request.oauth2;
|
||||
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Data;
|
||||
import lombok.experimental.Accessors;
|
||||
import org.hibernate.validator.constraints.Length;
|
||||
|
||||
import javax.validation.constraints.NotEmpty;
|
||||
import javax.validation.constraints.Pattern;
|
||||
|
||||
@ApiModel("用户 - OAuth2 模块 - 手机验证码认证请求")
|
||||
@Data
|
||||
@Accessors(chain = true)
|
||||
public class UsersOAuth2MobileCodeAuthenticateRequest {
|
||||
|
||||
@ApiModelProperty(value = "手机号", required = true, example = "15601691300")
|
||||
@NotEmpty(message = "手机号不能为空")
|
||||
@Length(min = 11, max = 11, message = "账号长度为 11 位")
|
||||
@Pattern(regexp = "^[0-9]+$", message = "手机号必须都是数字")
|
||||
private String mobile;
|
||||
|
||||
@ApiModelProperty(value = "手机验证码", required = true, example = "1024")
|
||||
@NotEmpty(message = "手机验证码不能为空")
|
||||
@Length(min = 4, max = 6, message = "手机验证码长度为 4-6 位")
|
||||
@Pattern(regexp = "^[0-9]+$", message = "手机验证码必须都是数字")
|
||||
private String code;
|
||||
|
||||
}
|
||||
@@ -1 +0,0 @@
|
||||
package cn.iocoder.mall.system.rest.request;
|
||||
@@ -0,0 +1,49 @@
|
||||
package cn.iocoder.mall.system.rest.response.oauth2;
|
||||
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Data;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
@ApiModel("管理员 - OAuth2 模块 - 认证响应")
|
||||
@Data
|
||||
@Accessors(chain = true)
|
||||
public class AdminsOAuth2AuthenticateResponse {
|
||||
|
||||
@Data
|
||||
public static class Token {
|
||||
|
||||
@ApiModelProperty(value = "access token", required = true, example = "001e8f49b20e47f7b3a2de774497cd50")
|
||||
private String accessToken;
|
||||
|
||||
@ApiModelProperty(value = "refresh token", required = true, example = "001e8f49b20e47f7b3a2de774497cd50")
|
||||
private String refreshToken;
|
||||
|
||||
@ApiModelProperty(value = "过期时间", required = true)
|
||||
private Date expiresTime;
|
||||
|
||||
}
|
||||
|
||||
@Data
|
||||
public static class Admin {
|
||||
|
||||
@ApiModelProperty(value = "管理员编号", required = true, example = "1")
|
||||
private Integer id;
|
||||
|
||||
@ApiModelProperty(value = "真实名字", required = true, example = "小王")
|
||||
private String name;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO 晚点测试下 swagger 的表现
|
||||
*/
|
||||
private Admin admin;
|
||||
/**
|
||||
* TODO 晚点测试下 swagger 的表现
|
||||
*/
|
||||
private Token token;
|
||||
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package cn.iocoder.mall.system.rest.response;
|
||||
package cn.iocoder.mall.system.rest.response.user;
|
||||
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
@@ -7,10 +7,10 @@ import lombok.experimental.Accessors;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
@ApiModel("管理员 - 认证 - 用户名登陆响应")
|
||||
@ApiModel("用户 - OAuth2 模块 - 认证响应")
|
||||
@Data
|
||||
@Accessors(chain = true)
|
||||
public class AdminsAuthorizeUsernameLoginResponse {
|
||||
public class UsersOAuth2AuthenticateResponse {
|
||||
|
||||
@Data
|
||||
public static class Token {
|
||||
@@ -26,11 +26,21 @@ public class AdminsAuthorizeUsernameLoginResponse {
|
||||
|
||||
}
|
||||
|
||||
@ApiModelProperty(value = "管理员编号", required = true, example = "1")
|
||||
private Integer id;
|
||||
@Data
|
||||
public static class User {
|
||||
|
||||
@ApiModelProperty(value = "真实名字", required = true, example = "小王")
|
||||
private String name;
|
||||
@ApiModelProperty(value = "管理员编号", required = true, example = "1")
|
||||
private Integer id;
|
||||
|
||||
@ApiModelProperty(value = "昵称", required = true, example = "小王")
|
||||
private String nickname;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO 晚点测试下 swagger 的表现
|
||||
*/
|
||||
private User user;
|
||||
|
||||
/**
|
||||
* TODO 晚点测试下 swagger 的表现
|
||||
Reference in New Issue
Block a user