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