提交 | 用户 | 时间
|
e7c126
|
1 |
package com.iailab.module.system.service.oauth2; |
H |
2 |
|
|
3 |
import cn.hutool.core.collection.CollUtil; |
|
4 |
import cn.hutool.core.util.ObjectUtil; |
|
5 |
import cn.hutool.core.util.StrUtil; |
|
6 |
import cn.hutool.extra.spring.SpringUtil; |
|
7 |
import com.iailab.framework.common.enums.CommonStatusEnum; |
|
8 |
import com.iailab.framework.common.pojo.PageResult; |
|
9 |
import com.iailab.framework.common.util.object.BeanUtils; |
|
10 |
import com.iailab.framework.common.util.string.StrUtils; |
|
11 |
import com.iailab.module.system.controller.admin.oauth2.vo.client.OAuth2ClientPageReqVO; |
|
12 |
import com.iailab.module.system.controller.admin.oauth2.vo.client.OAuth2ClientSaveReqVO; |
|
13 |
import com.iailab.module.system.dal.dataobject.oauth2.OAuth2ClientDO; |
|
14 |
import com.iailab.module.system.dal.mysql.oauth2.OAuth2ClientMapper; |
|
15 |
import com.iailab.module.system.dal.redis.RedisKeyConstants; |
|
16 |
import com.google.common.annotations.VisibleForTesting; |
|
17 |
import lombok.extern.slf4j.Slf4j; |
|
18 |
import org.springframework.cache.annotation.CacheEvict; |
|
19 |
import org.springframework.cache.annotation.Cacheable; |
|
20 |
import org.springframework.stereotype.Service; |
|
21 |
import org.springframework.validation.annotation.Validated; |
|
22 |
|
|
23 |
import javax.annotation.Resource; |
|
24 |
import java.util.Collection; |
|
25 |
|
|
26 |
import static com.iailab.framework.common.exception.util.ServiceExceptionUtil.exception; |
|
27 |
import static com.iailab.module.system.enums.ErrorCodeConstants.*; |
|
28 |
|
|
29 |
/** |
|
30 |
* OAuth2.0 Client Service 实现类 |
|
31 |
* |
|
32 |
* @author iailab |
|
33 |
*/ |
|
34 |
@Service |
|
35 |
@Validated |
|
36 |
@Slf4j |
|
37 |
public class OAuth2ClientServiceImpl implements OAuth2ClientService { |
|
38 |
|
|
39 |
@Resource |
|
40 |
private OAuth2ClientMapper oauth2ClientMapper; |
|
41 |
|
|
42 |
@Override |
|
43 |
public Long createOAuth2Client(OAuth2ClientSaveReqVO createReqVO) { |
|
44 |
validateClientIdExists(null, createReqVO.getClientId()); |
|
45 |
// 插入 |
|
46 |
OAuth2ClientDO client = BeanUtils.toBean(createReqVO, OAuth2ClientDO.class); |
|
47 |
oauth2ClientMapper.insert(client); |
|
48 |
return client.getId(); |
|
49 |
} |
|
50 |
|
|
51 |
@Override |
|
52 |
@CacheEvict(cacheNames = RedisKeyConstants.OAUTH_CLIENT, |
|
53 |
allEntries = true) // allEntries 清空所有缓存,因为可能修改到 clientId 字段,不好清理 |
|
54 |
public void updateOAuth2Client(OAuth2ClientSaveReqVO updateReqVO) { |
|
55 |
// 校验存在 |
|
56 |
validateOAuth2ClientExists(updateReqVO.getId()); |
|
57 |
// 校验 Client 未被占用 |
|
58 |
validateClientIdExists(updateReqVO.getId(), updateReqVO.getClientId()); |
|
59 |
|
|
60 |
// 更新 |
|
61 |
OAuth2ClientDO updateObj = BeanUtils.toBean(updateReqVO, OAuth2ClientDO.class); |
|
62 |
oauth2ClientMapper.updateById(updateObj); |
|
63 |
} |
|
64 |
|
|
65 |
@Override |
|
66 |
@CacheEvict(cacheNames = RedisKeyConstants.OAUTH_CLIENT, |
|
67 |
allEntries = true) // allEntries 清空所有缓存,因为 id 不是直接的缓存 key,不好清理 |
|
68 |
public void deleteOAuth2Client(Long id) { |
|
69 |
// 校验存在 |
|
70 |
validateOAuth2ClientExists(id); |
|
71 |
// 删除 |
|
72 |
oauth2ClientMapper.deleteById(id); |
|
73 |
} |
|
74 |
|
|
75 |
private void validateOAuth2ClientExists(Long id) { |
|
76 |
if (oauth2ClientMapper.selectById(id) == null) { |
|
77 |
throw exception(OAUTH2_CLIENT_NOT_EXISTS); |
|
78 |
} |
|
79 |
} |
|
80 |
|
|
81 |
@VisibleForTesting |
|
82 |
void validateClientIdExists(Long id, String clientId) { |
|
83 |
OAuth2ClientDO client = oauth2ClientMapper.selectByClientId(clientId); |
|
84 |
if (client == null) { |
|
85 |
return; |
|
86 |
} |
|
87 |
// 如果 id 为空,说明不用比较是否为相同 id 的客户端 |
|
88 |
if (id == null) { |
|
89 |
throw exception(OAUTH2_CLIENT_EXISTS); |
|
90 |
} |
|
91 |
if (!client.getId().equals(id)) { |
|
92 |
throw exception(OAUTH2_CLIENT_EXISTS); |
|
93 |
} |
|
94 |
} |
|
95 |
|
|
96 |
@Override |
|
97 |
public OAuth2ClientDO getOAuth2Client(Long id) { |
|
98 |
return oauth2ClientMapper.selectById(id); |
|
99 |
} |
|
100 |
|
|
101 |
@Override |
|
102 |
@Cacheable(cacheNames = RedisKeyConstants.OAUTH_CLIENT, key = "#clientId", |
|
103 |
unless = "#result == null") |
|
104 |
public OAuth2ClientDO getOAuth2ClientFromCache(String clientId) { |
|
105 |
return oauth2ClientMapper.selectByClientId(clientId); |
|
106 |
} |
|
107 |
|
|
108 |
@Override |
|
109 |
public PageResult<OAuth2ClientDO> getOAuth2ClientPage(OAuth2ClientPageReqVO pageReqVO) { |
|
110 |
return oauth2ClientMapper.selectPage(pageReqVO); |
|
111 |
} |
|
112 |
|
|
113 |
@Override |
|
114 |
public OAuth2ClientDO validOAuthClientFromCache(String clientId, String clientSecret, String authorizedGrantType, |
|
115 |
Collection<String> scopes, String redirectUri) { |
|
116 |
// 校验客户端存在、且开启 |
|
117 |
OAuth2ClientDO client = getSelf().getOAuth2ClientFromCache(clientId); |
|
118 |
if (client == null) { |
|
119 |
throw exception(OAUTH2_CLIENT_NOT_EXISTS); |
|
120 |
} |
|
121 |
if (CommonStatusEnum.isDisable(client.getStatus())) { |
|
122 |
throw exception(OAUTH2_CLIENT_DISABLE); |
|
123 |
} |
|
124 |
|
|
125 |
// 校验客户端密钥 |
|
126 |
if (StrUtil.isNotEmpty(clientSecret) && ObjectUtil.notEqual(client.getSecret(), clientSecret)) { |
|
127 |
throw exception(OAUTH2_CLIENT_CLIENT_SECRET_ERROR); |
|
128 |
} |
|
129 |
// 校验授权方式 |
|
130 |
if (StrUtil.isNotEmpty(authorizedGrantType) && !CollUtil.contains(client.getAuthorizedGrantTypes(), authorizedGrantType)) { |
|
131 |
throw exception(OAUTH2_CLIENT_AUTHORIZED_GRANT_TYPE_NOT_EXISTS); |
|
132 |
} |
|
133 |
// 校验授权范围 |
|
134 |
if (CollUtil.isNotEmpty(scopes) && !CollUtil.containsAll(client.getScopes(), scopes)) { |
|
135 |
throw exception(OAUTH2_CLIENT_SCOPE_OVER); |
|
136 |
} |
|
137 |
// 校验回调地址 |
|
138 |
if (StrUtil.isNotEmpty(redirectUri) && !StrUtils.startWithAny(redirectUri, client.getRedirectUris())) { |
|
139 |
throw exception(OAUTH2_CLIENT_REDIRECT_URI_NOT_MATCH, redirectUri); |
|
140 |
} |
|
141 |
return client; |
|
142 |
} |
|
143 |
|
|
144 |
/** |
|
145 |
* 获得自身的代理对象,解决 AOP 生效问题 |
|
146 |
* |
|
147 |
* @return 自己 |
|
148 |
*/ |
|
149 |
private OAuth2ClientServiceImpl getSelf() { |
|
150 |
return SpringUtil.getBean(getClass()); |
|
151 |
} |
|
152 |
|
|
153 |
} |