commit | author | age
|
9bcb19
|
1 |
/* |
I |
2 |
Copyright [2020] [https://www.xiaonuo.vip] |
|
3 |
|
|
4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
|
5 |
you may not use this file except in compliance with the License. |
|
6 |
You may obtain a copy of the License at |
|
7 |
|
|
8 |
http://www.apache.org/licenses/LICENSE-2.0 |
|
9 |
|
|
10 |
Unless required by applicable law or agreed to in writing, software |
|
11 |
distributed under the License is distributed on an "AS IS" BASIS, |
|
12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13 |
See the License for the specific language governing permissions and |
|
14 |
limitations under the License. |
|
15 |
|
|
16 |
Snowy采用APACHE LICENSE 2.0开源协议,您在使用过程中,需要注意以下几点: |
|
17 |
|
|
18 |
1.请不要删除和修改根目录下的LICENSE文件。 |
|
19 |
2.请不要删除和修改Snowy源码头部的版权声明。 |
|
20 |
3.请保留源码和相关描述文件的项目出处,作者声明等。 |
|
21 |
4.分发源码时候,请注明软件出处 https://gitee.com/xiaonuobase/snowy |
|
22 |
5.在修改包名,模块名称,项目代码等时,请注明软件出处 https://gitee.com/xiaonuobase/snowy |
|
23 |
6.若您的项目无法满足以上几点,可申请商业授权,获取Snowy商业授权许可,请在官网购买授权,地址为 https://www.xiaonuo.vip |
|
24 |
*/ |
|
25 |
package vip.xiaonuo.sys.modular.oauth.service.impl; |
|
26 |
|
|
27 |
import cn.hutool.core.util.ObjectUtil; |
|
28 |
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
|
29 |
import com.baomidou.mybatisplus.core.toolkit.IdWorker; |
|
30 |
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
|
31 |
import me.zhyd.oauth.config.AuthConfig; |
|
32 |
import me.zhyd.oauth.model.AuthCallback; |
|
33 |
import me.zhyd.oauth.model.AuthResponse; |
|
34 |
import me.zhyd.oauth.model.AuthUser; |
|
35 |
import me.zhyd.oauth.request.AuthGiteeRequest; |
|
36 |
import me.zhyd.oauth.request.AuthGithubRequest; |
|
37 |
import me.zhyd.oauth.request.AuthRequest; |
|
38 |
import me.zhyd.oauth.utils.AuthStateUtils; |
|
39 |
import org.springframework.stereotype.Service; |
|
40 |
import org.springframework.transaction.annotation.Transactional; |
|
41 |
import vip.xiaonuo.core.context.constant.ConstantContextHolder; |
|
42 |
import vip.xiaonuo.core.exception.ServiceException; |
|
43 |
import vip.xiaonuo.core.pojo.oauth.OauthConfigs; |
|
44 |
import vip.xiaonuo.sys.core.cache.OauthCache; |
|
45 |
import vip.xiaonuo.sys.core.enums.OauthPlatformEnum; |
|
46 |
import vip.xiaonuo.sys.modular.auth.service.AuthService; |
|
47 |
import vip.xiaonuo.sys.modular.oauth.entity.SysOauthUser; |
|
48 |
import vip.xiaonuo.sys.modular.oauth.enums.SysOauthExceptionEnum; |
|
49 |
import vip.xiaonuo.sys.modular.oauth.mapper.SysOauthMapper; |
|
50 |
import vip.xiaonuo.sys.modular.oauth.service.SysOauthService; |
|
51 |
import vip.xiaonuo.sys.modular.user.entity.SysUser; |
|
52 |
import vip.xiaonuo.sys.modular.user.service.SysUserService; |
|
53 |
|
|
54 |
import javax.annotation.Resource; |
|
55 |
import javax.servlet.http.HttpServletRequest; |
|
56 |
|
|
57 |
|
|
58 |
/** |
|
59 |
* Oauth登录相关service接口实现类 |
|
60 |
* |
|
61 |
* @author xuyuxiang |
|
62 |
* @date 2020/7/28 17:07 |
|
63 |
**/ |
|
64 |
@Service |
|
65 |
public class SysOauthServiceImpl extends ServiceImpl<SysOauthMapper, SysOauthUser> implements SysOauthService { |
|
66 |
|
|
67 |
@Resource |
|
68 |
private OauthCache oauthCache; |
|
69 |
|
|
70 |
@Resource |
|
71 |
private AuthService authService; |
|
72 |
|
|
73 |
@Resource |
|
74 |
private SysUserService sysUserService; |
|
75 |
|
|
76 |
|
|
77 |
@Override |
|
78 |
public String getAuthorizeUrl(String source) { |
|
79 |
Boolean enableOauthLogin = ConstantContextHolder.getEnableOauthLogin(); |
|
80 |
if (!enableOauthLogin) { |
|
81 |
throw new ServiceException(SysOauthExceptionEnum.OAUTH_DISABLED); |
|
82 |
} |
|
83 |
AuthRequest authRequest = this.getAuthRequest(source); |
|
84 |
return authRequest.authorize(AuthStateUtils.createState()); |
|
85 |
} |
|
86 |
|
|
87 |
@SuppressWarnings("all") |
|
88 |
@Override |
|
89 |
public String callback(String source, AuthCallback callback, HttpServletRequest request) { |
|
90 |
AuthRequest authRequest = this.getAuthRequest(source); |
|
91 |
AuthResponse<AuthUser> response = authRequest.login(callback); |
|
92 |
if (response.ok()) { |
|
93 |
AuthUser authUser = response.getData(); |
|
94 |
return doLogin(authUser); |
|
95 |
} else { |
|
96 |
throw new ServiceException(response.getCode(), response.getMsg()); |
|
97 |
} |
|
98 |
} |
|
99 |
|
|
100 |
/** |
|
101 |
* 根据用户授权信息进行登录 |
|
102 |
* |
|
103 |
* @param authUser 用户授权信息 |
|
104 |
* @return token |
|
105 |
* @author xuyuxiang |
|
106 |
* @date 2020/7/29 9:54 |
|
107 |
**/ |
|
108 |
@Transactional(rollbackFor = Exception.class) |
|
109 |
public String doLogin(AuthUser authUser) { |
|
110 |
//获取uuid |
|
111 |
String uuid = authUser.getUuid(); |
|
112 |
LambdaQueryWrapper<SysOauthUser> queryWrapper = new LambdaQueryWrapper<>(); |
|
113 |
queryWrapper.eq(SysOauthUser::getUuid, uuid); |
|
114 |
SysOauthUser oauthUser = this.getOne(queryWrapper); |
|
115 |
//从没授权登录过 |
|
116 |
if (ObjectUtil.isNull(oauthUser)) { |
|
117 |
//将授权的用户信息保存到sys_oauth_user表和sys_user表 |
|
118 |
this.saveByAuthUser(authUser); |
|
119 |
//再获取oauthUser用户 |
|
120 |
oauthUser = this.getOne(queryWrapper); |
|
121 |
} |
|
122 |
//获取用户账户信息进行登录 |
|
123 |
Long userId = oauthUser.getId(); |
|
124 |
SysUser sysUser = sysUserService.getUserById(userId); |
|
125 |
return authService.doLogin(sysUser); |
|
126 |
} |
|
127 |
|
|
128 |
/** |
|
129 |
* 将授权的用户信息保存到sys_oauth_user表和sys_user表 |
|
130 |
* |
|
131 |
* @param authUser 用户授权信息 |
|
132 |
* @return void |
|
133 |
* @author xuyuxiang |
|
134 |
* @date 2020/7/29 10:16 |
|
135 |
**/ |
|
136 |
@Transactional(rollbackFor = Exception.class) |
|
137 |
public void saveByAuthUser(AuthUser authUser) { |
|
138 |
//生成用户id |
|
139 |
long userId = IdWorker.getId(); |
|
140 |
//创建oauthUser对象 |
|
141 |
SysOauthUser oauthUser = new SysOauthUser(); |
|
142 |
oauthUser.setId(userId); |
|
143 |
this.fillOauthUserInfo(oauthUser, authUser); |
|
144 |
//创建sysUser对象 |
|
145 |
SysUser sysUser = new SysUser(); |
|
146 |
sysUser.setId(userId); |
|
147 |
//将授权的用户信息保存到user表 |
|
148 |
sysUserService.saveAuthUserToUser(authUser, sysUser); |
|
149 |
this.save(oauthUser); |
|
150 |
} |
|
151 |
|
|
152 |
/** |
|
153 |
* 根据具体的授权来源,获取授权请求 |
|
154 |
* |
|
155 |
* @param source 授权平台来源 |
|
156 |
* @return 授权请求 |
|
157 |
* @author xuyuxiang |
|
158 |
* @date 2020/7/28 17:28 |
|
159 |
**/ |
|
160 |
private AuthRequest getAuthRequest(String source) { |
|
161 |
AuthRequest authRequest; |
|
162 |
if (source.toLowerCase().equals(OauthPlatformEnum.GITEE.getCode())) { |
|
163 |
OauthConfigs giteeOauthConfigs = ConstantContextHolder.getGiteeOauthConfigs(); |
|
164 |
authRequest = new AuthGiteeRequest(AuthConfig.builder() |
|
165 |
.clientId(giteeOauthConfigs.getClientId()) |
|
166 |
.clientSecret(giteeOauthConfigs.getClientSecret()) |
|
167 |
.redirectUri(giteeOauthConfigs.getRedirectUri()) |
|
168 |
.build(), oauthCache); |
|
169 |
} else if (source.toLowerCase().equals(OauthPlatformEnum.GITHUB.getCode())) { |
|
170 |
OauthConfigs githubOauthConfigs = ConstantContextHolder.getGithubOauthConfigs(); |
|
171 |
authRequest = new AuthGithubRequest(AuthConfig.builder() |
|
172 |
.clientId(githubOauthConfigs.getClientId()) |
|
173 |
.clientSecret(githubOauthConfigs.getClientSecret()) |
|
174 |
.redirectUri(githubOauthConfigs.getRedirectUri()) |
|
175 |
.build(), oauthCache); |
|
176 |
} else { |
|
177 |
throw new ServiceException(SysOauthExceptionEnum.OAUTH_NOT_SUPPORT); |
|
178 |
} |
|
179 |
return authRequest; |
|
180 |
} |
|
181 |
|
|
182 |
/** |
|
183 |
* 将授权用户信息填充到oauthUser |
|
184 |
* |
|
185 |
* @param oauthUser 系统授权用户信息 |
|
186 |
* @param authUser 平台授权用户信息 |
|
187 |
* @return void |
|
188 |
* @author xuyuxiang |
|
189 |
* @date 2020/7/29 10:42 |
|
190 |
**/ |
|
191 |
private void fillOauthUserInfo(SysOauthUser oauthUser, AuthUser authUser) { |
|
192 |
oauthUser.setUuid(authUser.getUuid()); |
|
193 |
oauthUser.setAccessToken(authUser.getToken().getAccessToken()); |
|
194 |
oauthUser.setNickName(authUser.getNickname()); |
|
195 |
oauthUser.setAvatar(authUser.getAvatar()); |
|
196 |
oauthUser.setBlog(authUser.getBlog()); |
|
197 |
oauthUser.setCompany(authUser.getCompany()); |
|
198 |
oauthUser.setLocation(authUser.getLocation()); |
|
199 |
oauthUser.setEmail(authUser.getEmail()); |
|
200 |
oauthUser.setSource(authUser.getSource()); |
|
201 |
oauthUser.setRemark(authUser.getRemark()); |
|
202 |
if (ObjectUtil.isNotNull(authUser.getGender())) { |
|
203 |
oauthUser.setGender(authUser.getGender().getDesc()); |
|
204 |
} |
|
205 |
} |
|
206 |
} |