Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
G
guns-vip
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
chenjunxiong
guns-vip
Commits
c23a342b
Commit
c23a342b
authored
Jun 11, 2019
by
fengshuonan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
更新oauth2绑定逻辑
parent
e96b8d87
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
29 additions
and
18 deletions
+29
-18
guns-base-third-login/src/main/java/cn/stylefeng/guns/oauth/modular/service/impl/DefaultLoginService.java
+29
-18
No files found.
guns-base-third-login/src/main/java/cn/stylefeng/guns/oauth/modular/service/impl/DefaultLoginService.java
View file @
c23a342b
...
...
@@ -15,6 +15,7 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import
me.zhyd.oauth.model.AuthUser
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Service
;
import
org.springframework.transaction.annotation.Transactional
;
import
java.util.List
;
...
...
@@ -34,6 +35,7 @@ public class DefaultLoginService implements LoginService {
private
OauthUserInfoService
oauthUserInfoService
;
@Override
@Transactional
(
rollbackFor
=
Exception
.
class
)
public
String
oauthLogin
(
AuthUser
oauthUser
)
{
if
(
oauthUser
==
null
)
{
...
...
@@ -53,11 +55,11 @@ public class DefaultLoginService implements LoginService {
}
else
{
//当前无登录用户,
则新创建登录用户
createOAuthUser
(
oauthUser
);
//当前无登录用户,
创建用户或根据已有绑定用户的账号登录
String
account
=
getOauthUserAccount
(
oauthUser
);
//执行shiro的登录逻辑
OAuthToken
token
=
new
OAuthToken
(
oauthUser
.
getUsername
()
);
OAuthToken
token
=
new
OAuthToken
(
account
);
ShiroKit
.
getSubject
().
login
(
token
);
return
"redirect:/"
;
...
...
@@ -84,12 +86,12 @@ public class DefaultLoginService implements LoginService {
//已有人绑定,抛出异常
if
(
oauthUserInfos
!=
null
&&
oauthUserInfos
.
size
()
>
0
)
{
throw
new
OAuthLoginException
(
OAuthExceptionEnum
.
OPEN_ID_ALREADY_BIND
);
}
else
{
//新建一条绑定记录
OauthUserInfo
oAuthUserInfo
=
OAuthUserInfoFactory
.
createOAuthUserInfo
(
userId
,
oauthUser
);
this
.
oauthUserInfoService
.
save
(
oAuthUserInfo
);
}
//新建一条绑定记录
OauthUserInfo
oAuthUserInfo
=
OAuthUserInfoFactory
.
createOAuthUserInfo
(
userId
,
oauthUser
);
this
.
oauthUserInfoService
.
save
(
oAuthUserInfo
);
}
/**
...
...
@@ -98,21 +100,30 @@ public class DefaultLoginService implements LoginService {
* @author fengshuonan
* @Date 2019/6/9 19:07
*/
private
void
createOAuthUser
(
AuthUser
oauthUser
)
{
private
String
getOauthUserAccount
(
AuthUser
oauthUser
)
{
// 判断账号是否重复
User
theUser
=
this
.
userService
.
getByAccount
(
oauthUser
.
getUsername
());
if
(
theUser
!=
null
)
{
return
;
}
//先判断当前系统这个openId有没有人用
QueryWrapper
<
OauthUserInfo
>
queryWrapper
=
new
QueryWrapper
<
OauthUserInfo
>()
.
eq
(
"source"
,
oauthUser
.
getSource
().
name
())
.
and
(
i
->
i
.
eq
(
"uuid"
,
oauthUser
.
getUuid
()));
OauthUserInfo
oauthUserInfos
=
this
.
oauthUserInfoService
.
getOne
(
queryWrapper
);
//已有人绑定,直接返回这个人的账号,进行登录
if
(
oauthUserInfos
!=
null
)
{
Long
userId
=
oauthUserInfos
.
getUserId
();
return
this
.
userService
.
getById
(
userId
).
getAccount
();
}
else
{
//创建
用户
User
user
=
OAuthUserInfoFactory
.
createOAuthUser
(
oauthUser
);
this
.
userService
.
save
(
user
);
//没有人绑定的创建这个人的本系统
用户
User
user
=
OAuthUserInfoFactory
.
createOAuthUser
(
oauthUser
);
this
.
userService
.
save
(
user
);
//创建第三方绑定信息
this
.
bindOAuthUser
(
user
.
getUserId
(),
oauthUser
);
//新建一条oauth2绑定记录
OauthUserInfo
oAuthUserInfo
=
OAuthUserInfoFactory
.
createOAuthUserInfo
(
user
.
getUserId
(),
oauthUser
);
this
.
oauthUserInfoService
.
save
(
oAuthUserInfo
);
return
user
.
getAccount
();
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment