开始重构 system 模块的代码,先修改认证逻辑
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
package cn.iocoder.mall.system.rest.controller.admin;
|
||||
|
||||
import cn.iocoder.common.framework.constant.MallConstants;
|
||||
import cn.iocoder.common.framework.util.ServiceExceptionUtil;
|
||||
import cn.iocoder.common.framework.vo.CommonResult;
|
||||
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.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 io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import static cn.iocoder.mall.system.biz.constant.SystemErrorCodeEnum.*;
|
||||
|
||||
@RestController
|
||||
@RequestMapping(MallConstants.ROOT_PATH_ADMIN + "/oauth2")
|
||||
@Api(tags = "管理员 - OAuth2 API")
|
||||
public class AdminsOAuth2Controller {
|
||||
|
||||
@Autowired
|
||||
private OAuth2Service oauth2Service;
|
||||
@Autowired
|
||||
private AdminService adminService;
|
||||
|
||||
@PostMapping("/username_authenticate")
|
||||
@ApiOperation("用户名认证")
|
||||
public CommonResult<AdminsAuthorizeUsernameLoginResponse> usernameAuthenticate(AdminsOAuth2UsernameAuthenticateRequest request) {
|
||||
// 执行认证
|
||||
OAuth2UsernameAuthenticateDTO usernameAuthenticateDTO = AdminsOAuth2Convert.INSTANCE.convert(request);
|
||||
OAuth2AccessTokenBO accessTokenBO = oauth2Service.authenticate(usernameAuthenticateDTO);
|
||||
// 获得 Admin 信息
|
||||
AdminBO adminBO = adminService.get(accessTokenBO.getAccountId());
|
||||
if (adminBO == null) {
|
||||
throw ServiceExceptionUtil.exception(ADMIN_NOT_FOUND);
|
||||
}
|
||||
// 转换返回
|
||||
return CommonResult.success(
|
||||
AdminsOAuth2Convert.INSTANCE.convert(adminBO, accessTokenBO)
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package cn.iocoder.mall.system.rest.convert.admin;
|
||||
|
||||
import cn.iocoder.mall.system.biz.bo.account.AccountUsernameAuthorizeBO;
|
||||
import cn.iocoder.mall.system.rest.request.oauth2.AdminsOAuth2UsernameAuthenticateRequest;
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.factory.Mappers;
|
||||
|
||||
@Mapper
|
||||
public interface AdminsAdminConvert {
|
||||
|
||||
AdminsAdminConvert INSTANCE = Mappers.getMapper(AdminsAdminConvert.class);
|
||||
|
||||
AccountUsernameAuthorizeBO convert(AdminsOAuth2UsernameAuthenticateRequest request);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package cn.iocoder.mall.system.rest.convert.oauth2;
|
||||
|
||||
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 org.mapstruct.Mapper;
|
||||
import org.mapstruct.Mapping;
|
||||
import org.mapstruct.Mappings;
|
||||
import org.mapstruct.factory.Mappers;
|
||||
|
||||
@Mapper
|
||||
public interface AdminsOAuth2Convert {
|
||||
|
||||
AdminsOAuth2Convert INSTANCE = Mappers.getMapper(AdminsOAuth2Convert.class);
|
||||
|
||||
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);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
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 AdminsOAuth2UsernameAuthenticateRequest {
|
||||
|
||||
@ApiModelProperty(value = "用户名", required = true, example = "yudaoyuanma")
|
||||
@NotEmpty(message = "登陆账号不能为空")
|
||||
@Length(min = 5, max = 16, message = "账号长度为 5-16 位")
|
||||
@Pattern(regexp = "^[A-Za-z0-9]+$", message = "账号格式为数字以及字母")
|
||||
private String username;
|
||||
|
||||
@ApiModelProperty(value = "用户名", required = true, example = "buzhidao")
|
||||
@NotEmpty(message = "密码不能为空")
|
||||
@Length(min = 4, max = 16, message = "密码长度为 4-16 位")
|
||||
private String password;
|
||||
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
package cn.iocoder.mall.system.rest.request;
|
||||
@@ -0,0 +1,40 @@
|
||||
package cn.iocoder.mall.system.rest.response;
|
||||
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Data;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
@ApiModel("管理员 - 认证 - 用户名登陆响应")
|
||||
@Data
|
||||
@Accessors(chain = true)
|
||||
public class AdminsAuthorizeUsernameLoginResponse {
|
||||
|
||||
@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;
|
||||
|
||||
}
|
||||
|
||||
@ApiModelProperty(value = "管理员编号", required = true, example = "1")
|
||||
private Integer id;
|
||||
|
||||
@ApiModelProperty(value = "真实名字", required = true, example = "小王")
|
||||
private String name;
|
||||
|
||||
/**
|
||||
* TODO 晚点测试下 swagger 的表现
|
||||
*/
|
||||
private Token token;
|
||||
|
||||
}
|
||||
5
system/system-rest/src/main/resources/rest.yaml
Normal file
5
system/system-rest/src/main/resources/rest.yaml
Normal file
@@ -0,0 +1,5 @@
|
||||
# 服务器的配置项
|
||||
server:
|
||||
port: 18083
|
||||
servlet:
|
||||
context-path: /system-api/
|
||||
Reference in New Issue
Block a user