Commit ebee4ddb7c424d88b24ce4279b02447a8ba83987

Authored by YunaiV
1 parent 5e864850

制定 OAuth2 协议的表结构与 API 设计

Showing 12 changed files with 203 additions and 14 deletions
yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/api/auth/OAuth2Api.java 0 → 100644
  1 +package cn.iocoder.yudao.module.system.api.auth;
  2 +
  3 +/**
  4 + * OAuth2.0 API 接口
  5 + *
  6 + * @author 芋道源码
  7 + */
  8 +public interface OAuth2Api {
  9 +
  10 +
  11 +
  12 +}
... ...
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/auth/AuthController.java
... ... @@ -40,7 +40,7 @@ import static java.util.Collections.singleton;
40 40  
41 41 @Api(tags = "管理后台 - 认证")
42 42 @RestController
43   -@RequestMapping("/system/auth") // 暂时不跟 /auth 结尾
  43 +@RequestMapping("/system/auth")
44 44 @Validated
45 45 @Slf4j
46 46 public class AuthController {
... ...
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/auth/OAuth2Controller.java 0 → 100644
  1 +package cn.iocoder.yudao.module.system.controller.admin.auth;
  2 +
  3 +import io.swagger.annotations.Api;
  4 +import lombok.extern.slf4j.Slf4j;
  5 +import org.springframework.validation.annotation.Validated;
  6 +import org.springframework.web.bind.annotation.RequestMapping;
  7 +import org.springframework.web.bind.annotation.RestController;
  8 +
  9 +@Api(tags = "管理后台 - OAuth2.0 授权")
  10 +@RestController
  11 +@RequestMapping("/system/oauth2")
  12 +@Validated
  13 +@Slf4j
  14 +public class OAuth2Controller {
  15 +
  16 +// POST oauth/token TokenEndpoint:Password、Implicit、Code、Refresh Token
  17 +
  18 +// POST oauth/check_token CheckTokenEndpoint
  19 +
  20 +// DELETE oauth/token ConsumerTokenServices#revokeToken
  21 +
  22 +// GET oauth/authorize AuthorizationEndpoint
  23 +
  24 +}
... ...
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/dal/dataobject/auth/OAuth2AccessTokenDO.java
... ... @@ -10,8 +10,9 @@ import lombok.experimental.Accessors;
10 10 import java.util.Date;
11 11  
12 12 /**
13   - * OAuth2 访问令牌
  13 + * OAuth2 访问令牌 DO
14 14 *
  15 + * @author 芋道源码
15 16 */
16 17 @TableName("system_oauth2_access_token")
17 18 @Data
... ... @@ -20,7 +21,7 @@ import java.util.Date;
20 21 public class OAuth2AccessTokenDO extends BaseDO {
21 22  
22 23 /**
23   - * 编号,数据库字典
  24 + * 编号,数据库递增
24 25 */
25 26 private Long id;
26 27 /**
... ... @@ -38,18 +39,14 @@ public class OAuth2AccessTokenDO extends BaseDO {
38 39 */
39 40 private Integer userType;
40 41 /**
41   - * 刷新令牌
  42 + * 应用编号
42 43 *
43   - * 关联 {@link OAuth2RefreshTokenDO#getRefreshToken()}
  44 + * 关联 {@link OAuth2ApplicationDO#getId()}
44 45 */
45   - private String refreshToken;
  46 + private Long applicationId;
46 47 /**
47 48 * 过期时间
48 49 */
49 50 private Date expiresTime;
50   - /**
51   - * 创建 IP
52   - */
53   - private String createIp;
54 51  
55 52 }
... ...
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/dal/dataobject/auth/OAuth2ApplicationDO.java 0 → 100644
  1 +package cn.iocoder.yudao.module.system.dal.dataobject.auth;
  2 +
  3 +import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
  4 +import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO;
  5 +import com.baomidou.mybatisplus.annotation.TableName;
  6 +import lombok.Data;
  7 +import lombok.EqualsAndHashCode;
  8 +import lombok.experimental.Accessors;
  9 +
  10 +import java.util.List;
  11 +
  12 +/**
  13 + * OAuth2 客户端 DO
  14 + *
  15 + * 为什么不使用 Client 作为表名?
  16 + * 1. clientId 字段被占用,导致表的 id 无法有合适的缩写
  17 + * 2. 大多数 Github、Gitee 等平台,都会习惯称为第三方接入应用
  18 + *
  19 + * 如下字段,考虑到使用相对不是很高频,主要是一些开关,暂时不支持:
  20 + * authorized_grant_types、authorities、access_token_validity、refresh_token_validity、additional_information、autoapprove、resource_ids、scope
  21 + *
  22 + * @author 芋道源码
  23 + */
  24 +@TableName("system_oauth2_application")
  25 +@Data
  26 +@EqualsAndHashCode(callSuper = true)
  27 +@Accessors(chain = true)
  28 +public class OAuth2ApplicationDO extends BaseDO {
  29 +
  30 + /**
  31 + * 编号,数据库递增
  32 + */
  33 + private Long id;
  34 + /**
  35 + * 客户端编号
  36 + */
  37 + private String clientId;
  38 + /**
  39 + * 客户端密钥
  40 + */
  41 + private String clientSecret;
  42 + /**
  43 + * 可重定向的 URI 地址
  44 + */
  45 + private List<String> redirectUris;
  46 + /**
  47 + * 应用名
  48 + */
  49 + private String name;
  50 + /**
  51 + * 应用图标
  52 + */
  53 + private String logo;
  54 + /**
  55 + * 应用描述
  56 + */
  57 + private String description;
  58 + /**
  59 + * 状态
  60 + *
  61 + * 枚举 {@link CommonStatusEnum}
  62 + */
  63 + private Integer status;
  64 +
  65 +}
... ...
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/dal/dataobject/auth/OAuth2CodeDO.java 0 → 100644
  1 +package cn.iocoder.yudao.module.system.dal.dataobject.auth;
  2 +
  3 +import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
  4 +import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO;
  5 +import com.baomidou.mybatisplus.annotation.TableName;
  6 +import lombok.Data;
  7 +import lombok.EqualsAndHashCode;
  8 +import lombok.experimental.Accessors;
  9 +
  10 +import java.util.Date;
  11 +
  12 +/**
  13 + * OAuth2 授权码 DO
  14 + *
  15 + * @author 芋道源码
  16 + */
  17 +@TableName("system_oauth2_code")
  18 +@Data
  19 +@EqualsAndHashCode(callSuper = true)
  20 +@Accessors(chain = true)
  21 +public class OAuth2CodeDO extends BaseDO {
  22 +
  23 + /**
  24 + * 编号,数据库递增
  25 + */
  26 + private Long id;
  27 + /**
  28 + * 授权码
  29 + */
  30 + private String code;
  31 + /**
  32 + * 用户编号
  33 + */
  34 + private Long userId;
  35 + /**
  36 + * 用户类型
  37 + *
  38 + * 枚举 {@link UserTypeEnum}
  39 + */
  40 + private Integer userType;
  41 + /**
  42 + * 应用编号
  43 + *
  44 + * 关联 {@link OAuth2ApplicationDO#getId()}
  45 + */
  46 + private Long applicationId;
  47 + /**
  48 + * 刷新令牌
  49 + *
  50 + * 关联 {@link OAuth2RefreshTokenDO#getRefreshToken()}
  51 + */
  52 + private String refreshToken;
  53 + /**
  54 + * 过期时间
  55 + */
  56 + private Date expiresTime;
  57 + /**
  58 + * 创建 IP
  59 + */
  60 + private String createIp;
  61 +
  62 +}
... ...
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/dal/dataobject/auth/UserSessionDO.java
... ... @@ -24,6 +24,7 @@ import java.util.Date;
24 24 @Data
25 25 @Builder
26 26 @EqualsAndHashCode(callSuper = true)
  27 +@Deprecated
27 28 public class UserSessionDO extends BaseDO {
28 29  
29 30 /**
... ...
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/auth/AdminAuthService.java
1 1 package cn.iocoder.yudao.module.system.service.auth;
2 2  
3   -import cn.iocoder.yudao.framework.security.core.service.SecurityAuthFrameworkService;
4 3 import cn.iocoder.yudao.module.system.controller.admin.auth.vo.auth.*;
5 4  
6 5 import javax.validation.Valid;
... ...
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/auth/AdminOAuth2Service.java 0 → 100644
  1 +package cn.iocoder.yudao.module.system.service.auth;
  2 +
  3 +/**
  4 + * 管理后台的 OAuth2 Service 接口
  5 + *
  6 + * 将自身的 AdminUser 用户,授权给第三方应用,采用 OAuth2.0 的协议。
  7 + *
  8 + * 问题:为什么自身也作为一个第三方应用,也走这套流程呢?
  9 + * 回复:当然可以这么做,采用 Implicit 模式。考虑到大多数开发者使用不到这个特性,OAuth2.0 毕竟有一定学习成本,所以暂时没有采取这种方式。
  10 + *
  11 + * @author 芋道源码
  12 + */
  13 +public interface AdminOAuth2Service {
  14 +}
... ...
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/auth/OAuth2CodeService.java 0 → 100644
  1 +package cn.iocoder.yudao.module.system.service.auth;
  2 +
  3 +/**
  4 + * OAuth2.0 授权码 Service 接口
  5 + *
  6 + * 从功能上,和 Spring Security OAuth 的 JdbcAuthorizationCodeServices 的功能,提供授权码的操作
  7 + *
  8 + * @author 芋道源码
  9 + */
  10 +public class OAuth2CodeService {
  11 +}
... ...
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/auth/OAuth2ServiceImpl.java
... ... @@ -5,10 +5,12 @@ import org.springframework.stereotype.Service;
5 5 /**
6 6 * OAuth2.0 Service 实现类
7 7 *
  8 + *
  9 + *
8 10 * @author 芋道源码
9 11 */
10 12 @Service
11   -public class OAuth2ServiceImpl implements OAuth2Service {
  13 +public class OAuth2ServiceImpl implements OAuth2TokenService {
12 14  
13 15 // @Autowired
14 16 // private SystemBizProperties systemBizProperties;
... ...
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/auth/OAuth2Service.java renamed to yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/auth/OAuth2TokenService.java
1 1 package cn.iocoder.yudao.module.system.service.auth;
2 2  
3 3 /**
4   - * OAuth2.0 Service 接口
  4 + * OAuth2.0 Token Service 接口
  5 + *
  6 + * 从功能上,和 Spring Security OAuth 的 JdbcTokenStore 的功能,提供访问令牌、刷新令牌的操作
5 7 *
6 8 * @author 芋道源码
7 9 */
8   -public interface OAuth2Service {
  10 +public interface OAuth2TokenService {
9 11  
10 12 // OAuth2AccessTokenDO createAccessToken(Long userId, Integer userType, String createIp);
11 13 //
... ...