Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
S
sh-ds
Project
Project
Details
Activity
Releases
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
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
李良停
sh-ds
Commits
f05b7b0a
Commit
f05b7b0a
authored
Apr 29, 2022
by
wangsong
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
change data
parent
023eac58
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
239 additions
and
147 deletions
+239
-147
EdenAuthcenterAgentApplication.java
...eden/authcenter/agent/EdenAuthcenterAgentApplication.java
+1
-1
ClientDataInfo.java
...ineseall/eden/authcenter/agent/client/ClientDataInfo.java
+1
-1
AuthController.java
...eall/eden/authcenter/agent/controller/AuthController.java
+235
-143
UcenterController.java
...l/eden/authcenter/agent/controller/UcenterController.java
+2
-2
No files found.
eden-authcenter-agent/src/main/java/com/chineseall/eden/authcenter/agent/EdenAuthcenterAgentApplication.java
View file @
f05b7b0a
...
...
@@ -7,7 +7,7 @@ import org.springframework.boot.web.support.SpringBootServletInitializer;
import
org.springframework.context.annotation.ComponentScan
;
@AlphaFrameworkApplication
@ComponentScan
(
basePackages
=
{
"com.chineseall.eden.authcenter.agent"
,
"com.chineseall.eden.authcenter.log"
})
@ComponentScan
(
basePackages
=
{
"com.chineseall.eden.authcenter.agent"
,
"com.chineseall.eden.authcenter.log"
,
"com.chineseall.eden.authcenter.config"
})
public
class
EdenAuthcenterAgentApplication
extends
SpringBootServletInitializer
{
@Override
protected
SpringApplicationBuilder
configure
(
SpringApplicationBuilder
builder
)
{
...
...
eden-authcenter-agent/src/main/java/com/chineseall/eden/authcenter/agent/client/ClientDataInfo.java
View file @
f05b7b0a
...
...
@@ -10,6 +10,6 @@ public class ClientDataInfo {
private
String
returnUrl
;
private
OauthType
oauthType
;
private
String
oauthType
;
}
eden-authcenter-agent/src/main/java/com/chineseall/eden/authcenter/agent/controller/AuthController.java
View file @
f05b7b0a
package
com
.
chineseall
.
eden
.
authcenter
.
agent
.
controller
;
import
cn.sh.chineseall.framework.core.util.ArrayUtils
;
import
cn.sh.chineseall.framework.core.util.CollectionUtils
;
import
cn.sh.chineseall.framework.core.util.StringUtils
;
import
cn.sh.chineseall.framework.lang.calendar.DateUtils
;
import
com.alibaba.fastjson.JSON
;
import
com.chineseall.eden.authcenter.agent.client.*
;
import
com.chineseall.eden.authcenter.agent.oauth.OauthConfig
;
import
com.chineseall.eden.authcenter.agent.oauth.OauthConfigItem
;
import
com.chineseall.eden.authcenter.agent.oauth.OauthType
;
import
com.chineseall.eden.authcenter.agent.client.AuthUserInfo
;
import
com.chineseall.eden.authcenter.agent.client.ClientDataInfo
;
import
com.chineseall.eden.authcenter.agent.client.UserType
;
import
com.chineseall.eden.authcenter.agent.utils.ConvertMessageMapToBean
;
import
com.chineseall.eden.authcenter.agent.utils.EncodeUtil
;
import
com.chineseall.eden.authcenter.config.entity.AuthClient
;
import
com.chineseall.eden.authcenter.config.entity.AuthSource
;
import
com.chineseall.eden.authcenter.config.service.AuthClientService
;
import
com.chineseall.eden.authcenter.config.service.AuthSourceService
;
import
com.chineseall.eden.authcenter.log.enums.LogType
;
import
com.chineseall.eden.authcenter.log.model.AuthLog
;
import
com.chineseall.eden.authcenter.log.model.LogUserInfo
;
...
...
@@ -28,10 +31,7 @@ import javax.servlet.http.HttpServletRequest;
import
javax.servlet.http.HttpServletResponse
;
import
java.io.IOException
;
import
java.net.URLEncoder
;
import
java.util.Date
;
import
java.util.HashMap
;
import
java.util.Map
;
import
java.util.Objects
;
import
java.util.*
;
import
java.util.function.Function
;
import
java.util.stream.Collectors
;
...
...
@@ -41,11 +41,16 @@ public class AuthController {
Logger
logger
=
Logger
.
getLogger
(
AuthController
.
class
);
@Autowired
private
OauthClient
oauthClient
;
// @Autowired
// private OauthClient oauthClient;
//
// @Autowired
// private OauthConfig oauthConfig;
@Autowired
private
OauthConfig
oauthConfig
;
private
AuthClientService
authClientService
;
@Autowired
private
AuthSourceService
authSourceService
;
@Resource
private
AuthLogService
authLogService
;
...
...
@@ -56,7 +61,10 @@ public class AuthController {
public
ModelAndView
index
(
HttpServletRequest
request
,
HttpServletResponse
response
)
{
ModelAndView
modelAndView
=
new
ModelAndView
();
modelAndView
.
setViewName
(
"ds"
);
Map
<
String
,
ClientItem
>
clientItemMap
=
oauthClient
.
getClients
().
stream
().
collect
(
Collectors
.
toMap
(
ClientItem:
:
getClientId
,
Function
.
identity
()));
List
<
AuthClient
>
authClientList
=
authClientService
.
listAll
();
List
<
AuthSource
>
authSourceList
=
authSourceService
.
listAll
();
Map
<
String
,
AuthClient
>
clientMap
=
authClientList
.
stream
().
collect
(
Collectors
.
toMap
(
AuthClient:
:
getKey
,
Function
.
identity
(),
(
o1
,
o2
)
->
o1
));
// Map<String, ClientItem> clientItemMap = oauthClient.getClients().stream().collect(Collectors.toMap(ClientItem::getClientId, Function.identity()));
try
{
String
clientId
=
request
.
getParameter
(
"client_id"
);
...
...
@@ -67,16 +75,16 @@ public class AuthController {
authLog
.
setLogType
(
LogType
.
login
);
if
(
StringUtils
.
isNotEmpty
(
clientId
)){
ClientItem
clientItem
=
clientItem
Map
.
get
(
clientId
);
AuthClient
clientItem
=
client
Map
.
get
(
clientId
);
if
(
null
==
clientItem
)
{
modelAndView
.
setViewName
(
"error"
);
modelAndView
.
addObject
(
"message"
,
"client_id为空或者不正确"
);
return
modelAndView
;
}
authLog
.
setClientId
(
clientId
);
authLog
.
setAuthSource
(
clientItem
.
get
Client
Name
());
authLog
.
setAuthSource
(
clientItem
.
getName
());
String
sign
=
request
.
getParameter
(
"sign"
);
String
signOrigin
=
clientItem
.
get
ClientId
()
+
"$$"
+
clientItem
.
getClien
tSecret
();
String
signOrigin
=
clientItem
.
get
Key
()
+
"$$"
+
clientItem
.
ge
tSecret
();
String
md5Hex
=
EncodeUtil
.
md5
(
signOrigin
);
if
(!
Objects
.
equals
(
sign
,
md5Hex
))
{
...
...
@@ -93,22 +101,21 @@ public class AuthController {
if
(
StringUtils
.
isNotEmpty
(
loginType
))
{
if
(
StringUtils
.
isNotEmpty
(
returnUrl
)
&&
(
returnUrl
.
contains
(
"readinglab"
)
||
returnUrl
.
contains
(
"read.etextbook.cn"
))){
// 阅览室日志分离
ClientItem
item
=
clientItem
Map
.
get
(
"readingroomClientId"
);
AuthClient
item
=
client
Map
.
get
(
"readingroomClientId"
);
authLog
.
setClientId
(
"readingroomClientId"
);
authLog
.
setAuthSource
(
item
.
get
Client
Name
());
authLog
.
setAuthSource
(
item
.
getName
());
}
else
if
(
"cloudcourse"
.
equals
(
business_client
)
||
"cloudcourse-test"
.
equals
(
business_client
)){
ClientItem
item
=
clientItem
Map
.
get
(
"cloudcourse"
);
AuthClient
item
=
client
Map
.
get
(
"cloudcourse"
);
authLog
.
setClientId
(
"cloudcourse"
);
authLog
.
setAuthSource
(
item
.
get
Client
Name
());
authLog
.
setAuthSource
(
item
.
getName
());
}
else
if
(
"adaptive-learning"
.
equals
(
business_client
)){
ClientItem
item
=
clientItem
Map
.
get
(
"adaptive-learning"
);
AuthClient
item
=
client
Map
.
get
(
"adaptive-learning"
);
authLog
.
setClientId
(
"adaptive-learning"
);
authLog
.
setAuthSource
(
item
.
get
Client
Name
());
authLog
.
setAuthSource
(
item
.
getName
());
}
OauthType
oauthType
=
OauthType
.
getValue
(
loginType
);
if
(
oauthType
!=
null
){
if
(
authSourceList
.
stream
().
anyMatch
(
p
->
Objects
.
equals
(
p
.
getKey
(),
loginType
))){
modelAndView
.
addObject
(
"loginType"
,
loginType
);
}
else
{
modelAndView
.
addObject
(
"loginType"
,
""
);
...
...
@@ -119,21 +126,29 @@ public class AuthController {
}
authLogService
.
save
(
authLog
);
String
logId
=
authLog
.
getId
();
String
dianjiaoguanLoinUrl
=
generateLoginUrl
(
returnUrl
,
OauthType
.
dianjiaoguan
.
getCode
(),
authLog
.
getId
());
modelAndView
.
addObject
(
"dianjiaoguanLoinUrl"
,
dianjiaoguanLoinUrl
);
String
edenoperationLoginUrl
=
generateLoginUrl
(
returnUrl
,
OauthType
.
edenoperation
.
getCode
(),
authLog
.
getId
());
modelAndView
.
addObject
(
"edenoperationLoginUrl"
,
edenoperationLoginUrl
);
OauthType
[]
oauthTypes
=
OauthType
.
values
();
for
(
OauthType
oauthType
:
oauthTypes
)
{
if
(
"idp"
.
equals
(
oauthType
.
getType
())){
if
(
CollectionUtils
.
isNotEmpty
(
authSourceList
)){
authSourceList
.
forEach
(
p
->
{
modelAndView
.
addObject
(
p
.
getKey
()+
"LoginUrl"
,
generateLoginUrl
(
returnUrl
,
p
,
logId
));
});
String
idpAuthTypeUrl
=
generateSpLoginUrl
(
returnUrl
,
oauthType
.
getCode
(),
authLog
.
getId
());
modelAndView
.
addObject
(
oauthType
.
getCode
()+
"LoginUrl"
,
idpAuthTypeUrl
);
}
}
// String dianjiaoguanLoinUrl = generateLoginUrl(returnUrl, OauthType.dianjiaoguan.getCode(), authLog.getId());
// modelAndView.addObject("dianjiaoguanLoinUrl", dianjiaoguanLoinUrl);
// String edenoperationLoginUrl = generateLoginUrl(returnUrl, OauthType.edenoperation.getCode(), authLog.getId());
// modelAndView.addObject("edenoperationLoginUrl", edenoperationLoginUrl);
//
// OauthType[] oauthTypes = OauthType.values();
// for (OauthType oauthType : oauthTypes) {
// if ("idp".equals(oauthType.getType())){
//
// String idpAuthTypeUrl = generateSpLoginUrl(returnUrl, oauthType.getCode(), authLog.getId());
// modelAndView.addObject(oauthType.getCode()+"LoginUrl", idpAuthTypeUrl);
// }
// }
modelAndView
.
addObject
(
"logId"
,
authLog
.
getId
());
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
...
...
@@ -155,8 +170,8 @@ public class AuthController {
String
os
=
request
.
getParameter
(
"os"
);
try
{
AuthLog
authLog
=
authLogService
.
getById
(
logId
);
OauthConfigItem
oauthConfigItem
=
oauthConfig
.
getItems
().
get
(
oauthType
);
authLog
.
setFowardUrl
(
oauthConfigItem
.
getOauthUrl
());
AuthSource
authSource
=
authSourceService
.
getAuthSourceByKey
(
oauthType
);
authLog
.
setFowardUrl
(
authSource
.
getOauthUrl
());
authLog
.
setOauthType
(
oauthType
);
authLog
.
setBrowser
(
browser
);
authLog
.
setVersion
(
version
);
...
...
@@ -184,17 +199,19 @@ public class AuthController {
public
ModelAndView
logout
(
HttpServletRequest
request
,
HttpServletResponse
response
)
{
ModelAndView
modelAndView
=
new
ModelAndView
();
modelAndView
.
setViewName
(
"logout"
);
Map
<
String
,
ClientItem
>
clientItemMap
=
oauthClient
.
getClients
().
stream
().
collect
(
Collectors
.
toMap
(
ClientItem:
:
getClientId
,
Function
.
identity
()));
List
<
AuthClient
>
authClientList
=
authClientService
.
listAll
();
Map
<
String
,
AuthClient
>
authClientMap
=
authClientList
.
stream
().
collect
(
Collectors
.
toMap
(
AuthClient:
:
getKey
,
Function
.
identity
(),
(
o1
,
o2
)
->
o1
));
// Map<String, ClientItem> clientItemMap = oauthClient.getClients().stream().collect(Collectors.toMap(ClientItem::getClientId, Function.identity()));
try
{
String
clientId
=
request
.
getParameter
(
"client_id"
);
ClientItem
clientItem
=
clientItem
Map
.
get
(
clientId
);
AuthClient
clientItem
=
authClient
Map
.
get
(
clientId
);
if
(
clientItem
==
null
)
{
modelAndView
.
setViewName
(
"error"
);
modelAndView
.
addObject
(
"message"
,
"client_id为空或者不正确"
);
return
modelAndView
;
}
String
sign
=
request
.
getParameter
(
"sign"
);
String
signOrigin
=
clientItem
.
get
ClientId
()
+
"$$"
+
clientItem
.
getClien
tSecret
();
String
signOrigin
=
clientItem
.
get
Key
()
+
"$$"
+
clientItem
.
ge
tSecret
();
String
md5Hex
=
EncodeUtil
.
md5
(
signOrigin
);
if
(!
Objects
.
equals
(
sign
,
md5Hex
))
{
...
...
@@ -214,8 +231,10 @@ public class AuthController {
modelAndView
.
addObject
(
"message"
,
"登出方式为空"
);
return
modelAndView
;
}
OauthType
type
=
OauthType
.
valueOf
(
oauthType
);
if
(
type
==
null
){
List
<
AuthSource
>
authSourceList
=
authSourceService
.
listAll
();
AuthSource
authSource
=
authSourceList
.
stream
().
filter
(
p
->
Objects
.
equals
(
p
.
getKey
(),
oauthType
)).
findFirst
().
orElse
(
null
);
if
(
authSource
==
null
){
modelAndView
.
setViewName
(
"error"
);
modelAndView
.
addObject
(
"message"
,
"登出方式错误"
);
return
modelAndView
;
...
...
@@ -224,19 +243,22 @@ public class AuthController {
authLog
.
setOauthType
(
oauthType
);
authLog
.
setReturnUrl
(
service
);
authLog
.
setClientId
(
clientId
);
authLog
.
setAuthSource
(
clientItem
.
get
Client
Name
());
authLog
.
setAuthSource
(
clientItem
.
getName
());
authLog
.
setLogType
(
LogType
.
logout
);
authLogService
.
save
(
authLog
);
if
(
StringUtils
.
isNotEmpty
(
service
))
{
if
(!
OauthType
.
dianjiaoguan
.
equals
(
type
)
&&
!
OauthType
.
edenoperation
.
equals
(
type
)){
if
(
OauthType
.
qpjy
.
equals
(
type
)){
response
.
sendRedirect
(
generateSpLogoutUrl
(
service
,
type
.
name
()));
}
}
else
{
response
.
sendRedirect
(
generateLogoutUrl
(
service
,
type
.
name
()));
}
if
(
Objects
.
equals
(
authSource
.
getAuthType
(),
1
)
||
Objects
.
equals
(
authSource
.
getKey
(),
"qpjy"
)){
response
.
sendRedirect
(
generateLogoutUrl
(
service
,
authSource
));
}
// if (!OauthType.dianjiaoguan.equals(type) && !OauthType.edenoperation.equals(type)){
// if (OauthType.qpjy.equals(type)){
// response.sendRedirect(generateSpLogoutUrl(service,type.name()));
//
// }
//
// }else{
// response.sendRedirect(generateLogoutUrl(service, type.name()));
// }
}
return
null
;
}
catch
(
Exception
e
)
{
...
...
@@ -288,9 +310,9 @@ public class AuthController {
Map
<
String
,
Object
>
userInfoMap
=
MyRestTemplate
.
exchange
(
HttpMethod
.
GET
,
userInfoUrl
,
null
,
null
,
HashMap
.
class
);
// 转换用户信息
AuthUserInfo
userInfo
=
null
;
if
(
oauthType
.
equals
(
OauthType
.
dianjiaoguan
.
toString
()
))
if
(
oauthType
.
equals
(
"dianjiaoguan"
))
userInfo
=
ConvertMessageMapToBean
.
converDianjiaoguanData
(
userInfoMap
);
else
if
(
oauthType
.
equals
(
OauthType
.
edenoperation
.
toString
()
))
else
if
(
oauthType
.
equals
(
"edenoperation"
))
userInfo
=
ConvertMessageMapToBean
.
converEdenoperationData
(
userInfoMap
);
// 封装下游数据
ClientDataInfo
clientDataInfo
=
new
ClientDataInfo
();
...
...
@@ -322,7 +344,7 @@ public class AuthController {
authLog
.
setLogUserInfo
(
logUserInfo
);
}
clientDataInfo
.
setAuthUserInfo
(
userInfo
);
clientDataInfo
.
setOauthType
(
OauthType
.
valueOf
(
oauthType
)
);
clientDataInfo
.
setOauthType
(
oauthType
);
modelAndView
.
addObject
(
"clientDataInfo"
,
clientDataInfo
);
modelAndView
.
addObject
(
"successPostUrl"
,
returnUrl
);
...
...
@@ -394,7 +416,7 @@ public class AuthController {
authLog
.
setLogUserInfo
(
logUserInfo
);
}
clientDataInfo
.
setAuthUserInfo
(
userInfo
);
clientDataInfo
.
setOauthType
(
OauthType
.
valueOf
(
oauthType
)
);
clientDataInfo
.
setOauthType
(
oauthType
);
modelAndView
.
addObject
(
"clientDataInfo"
,
clientDataInfo
);
modelAndView
.
addObject
(
"successPostUrl"
,
returnUrl
);
...
...
@@ -423,108 +445,168 @@ public class AuthController {
return
null
;
}
private
String
generateLoginUrl
(
String
returnUrl
,
String
oauthType
,
String
logId
)
{
//oauthConfig + "/authorize?client_id=testClentId&redirect_uri=http%3a%2f%2f192.168.17.129%3a7774%2fauth%2floginsuccess.do&state=state"
private
String
generateLoginUrl
(
String
returnUrl
,
AuthSource
authSource
,
String
logId
){
StringBuilder
loginUrlBuilder
=
new
StringBuilder
();
OauthConfigItem
oauthConfigItem
=
oauthConfig
.
getItems
().
get
(
oauthType
);
loginUrlBuilder
.
append
(
oauthConfigItem
.
getOauthUrl
());
if
(!
oauthConfigItem
.
getOauthUrl
().
endsWith
(
"/"
))
{
loginUrlBuilder
.
append
(
authSource
.
getOauthUrl
());
if
(!
authSource
.
getOauthUrl
().
endsWith
(
"/"
))
{
loginUrlBuilder
.
append
(
"/"
);
}
loginUrlBuilder
.
append
(
"oauth2.0/authorize?client_id="
).
append
(
oauthConfigItem
.
getClientId
());
String
loginSuccessUrl
=
oauthConfigItem
.
getLoginSuccessUrl
();
loginSuccessUrl
=
loginSuccessUrl
+
"?oauth_type="
+
oauthType
+
"&log_id="
+
logId
;
if
(
StringUtils
.
isNotEmpty
(
returnUrl
))
{
Map
<
String
,
String
>
param
=
new
HashMap
<>();
param
.
put
(
"return_url"
,
URLEncoder
.
encode
(
returnUrl
));
loginSuccessUrl
=
appendUrl
(
loginSuccessUrl
,
param
);
}
loginUrlBuilder
.
append
(
"&redirect_uri="
).
append
(
URLEncoder
.
encode
(
loginSuccessUrl
));
if
(
Objects
.
equals
(
authSource
.
getAuthType
(),
1
)){
// oauth 认证
loginUrlBuilder
.
append
(
"oauth2.0/authorize?client_id="
).
append
(
authSource
.
getClientId
());
loginUrlBuilder
.
append
(
"&state=state&response_type=code"
);
return
loginUrlBuilder
.
toString
();
}
else
if
(
Objects
.
equals
(
authSource
.
getAuthType
(),
2
)){
// idp 认证
loginUrlBuilder
.
append
(
"login?client_id="
).
append
(
authSource
.
getClientId
());
String
sign
=
EncodeUtil
.
md5
(
authSource
.
getClientId
()+
"$$"
+
authSource
.
getClientSecret
());
loginUrlBuilder
.
append
(
"&sign="
).
append
(
sign
);
}
private
String
generateSpLoginUrl
(
String
returnUrl
,
String
oauthType
,
String
logId
)
{
//oauthConfig + "/authorize?client_id=testClentId&redirect_uri=http%3a%2f%2f192.168.17.129%3a7774%2fauth%2floginsuccess.do&state=state"
StringBuilder
loginUrlBuilder
=
new
StringBuilder
();
OauthConfigItem
oauthConfigItem
=
oauthConfig
.
getItems
().
get
(
oauthType
);
loginUrlBuilder
.
append
(
oauthConfigItem
.
getOauthUrl
());
if
(!
oauthConfigItem
.
getOauthUrl
().
endsWith
(
"/"
))
{
loginUrlBuilder
.
append
(
"/"
);
}
loginUrlBuilder
.
append
(
"login?client_id="
).
append
(
oauthConfigItem
.
getClientId
());
String
loginSuccessUrl
=
oauthConfigItem
.
getLoginSuccessUrl
();
loginSuccessUrl
=
loginSuccessUrl
+
"?log_id="
+
logId
+
"&oauth_type="
+
oauthType
;
String
loginSuccessUrl
=
authSource
.
getLoginSuccessUrl
();
loginSuccessUrl
=
loginSuccessUrl
+
"?oauth_type="
+
authSource
.
getKey
()
+
"&log_id="
+
logId
;
if
(
StringUtils
.
isNotEmpty
(
returnUrl
))
{
Map
<
String
,
String
>
param
=
new
HashMap
<>();
param
.
put
(
"return_url"
,
URLEncoder
.
encode
(
returnUrl
));
loginSuccessUrl
=
appendUrl
(
loginSuccessUrl
,
param
);
}
loginUrlBuilder
.
append
(
"&redirect_uri="
).
append
(
URLEncoder
.
encode
(
loginSuccessUrl
));
String
sign
=
EncodeUtil
.
md5
(
oauthConfigItem
.
getClientId
()+
"$$"
+
oauthConfigItem
.
getClientSecret
());
loginUrlBuilder
.
append
(
"&sign="
+
sign
);
return
loginUrlBuilder
.
toString
();
}
private
String
generateLogoutUrl
(
String
returnUrl
,
String
oauthType
)
{
//oauthConfig + "/authorize?client_id=testClentId&redirect_uri=http%3a%2f%2f192.168.17.129%3a7774%2fauth%2floginsuccess.do&state=state"
StringBuilder
loginUrlBuilder
=
new
StringBuilder
();
OauthConfigItem
oauthConfigItem
=
oauthConfig
.
getItems
().
get
(
oauthType
);
loginUrlBuilder
.
append
(
oauthConfigItem
.
getOauthUrl
());
if
(!
oauthConfigItem
.
getOauthUrl
().
endsWith
(
"/"
))
{
loginUrlBuilder
.
append
(
"/"
);
}
String
service
=
oauthConfigItem
.
getLogoutSuccessUrl
();
if
(
StringUtils
.
isNotEmpty
(
returnUrl
))
{
Map
<
String
,
String
>
param
=
new
HashMap
<>();
param
.
put
(
"return_url"
,
URLEncoder
.
encode
(
returnUrl
));
service
=
appendUrl
(
service
,
param
);
}
loginUrlBuilder
.
append
(
"logout?service="
).
append
(
URLEncoder
.
encode
(
service
));
return
loginUrlBuilder
.
toString
();
}
private
String
generateSpLogoutUrl
(
String
returnUrl
,
String
oauthType
)
{
if
(
oauthType
.
equals
(
"qpjy"
)){
// private String generateLoginUrl(String returnUrl, String oauthType, String logId) {
// //oauthConfig + "/authorize?client_id=testClentId&redirect_uri=http%3a%2f%2f192.168.17.129%3a7774%2fauth%2floginsuccess.do&state=state"
// StringBuilder loginUrlBuilder = new StringBuilder();
// OauthConfigItem oauthConfigItem = oauthConfig.getItems().get(oauthType);
// loginUrlBuilder.append(oauthConfigItem.getOauthUrl());
// if (!oauthConfigItem.getOauthUrl().endsWith("/")) {
// loginUrlBuilder.append("/");
// }
// loginUrlBuilder.append("oauth2.0/authorize?client_id=").append(oauthConfigItem.getClientId());
// String loginSuccessUrl = oauthConfigItem.getLoginSuccessUrl();
// loginSuccessUrl = loginSuccessUrl + "?oauth_type=" + oauthType + "&log_id=" + logId;
// if (StringUtils.isNotEmpty(returnUrl)) {
// Map<String, String> param = new HashMap<>();
// param.put("return_url", URLEncoder.encode(returnUrl));
// loginSuccessUrl = appendUrl(loginSuccessUrl, param);
// }
// loginUrlBuilder.append("&redirect_uri=").append(URLEncoder.encode(loginSuccessUrl));
//
// loginUrlBuilder.append("&state=state&response_type=code");
// return loginUrlBuilder.toString();
// }
//
// private String generateSpLoginUrl(String returnUrl, String oauthType, String logId) {
// //oauthConfig + "/authorize?client_id=testClentId&redirect_uri=http%3a%2f%2f192.168.17.129%3a7774%2fauth%2floginsuccess.do&state=state"
// StringBuilder loginUrlBuilder = new StringBuilder();
// OauthConfigItem oauthConfigItem = oauthConfig.getItems().get(oauthType);
// loginUrlBuilder.append(oauthConfigItem.getOauthUrl());
// if (!oauthConfigItem.getOauthUrl().endsWith("/")) {
// loginUrlBuilder.append("/");
// }
// loginUrlBuilder.append("login?client_id=").append(oauthConfigItem.getClientId());
// String loginSuccessUrl = oauthConfigItem.getLoginSuccessUrl();
// loginSuccessUrl = loginSuccessUrl + "?log_id=" + logId+"&oauth_type=" + oauthType ;
// if (StringUtils.isNotEmpty(returnUrl)) {
// Map<String, String> param = new HashMap<>();
// param.put("return_url", URLEncoder.encode(returnUrl));
// loginSuccessUrl = appendUrl(loginSuccessUrl, param);
// }
// loginUrlBuilder.append("&redirect_uri=").append(URLEncoder.encode(loginSuccessUrl));
// String sign = EncodeUtil.md5( oauthConfigItem.getClientId()+ "$$" + oauthConfigItem.getClientSecret());
// loginUrlBuilder.append("&sign="+sign);
// return loginUrlBuilder.toString();
// }
private
String
generateLogoutUrl
(
String
returnUrl
,
AuthSource
authSource
){
if
(
Objects
.
equals
(
authSource
.
getKey
(),
"qpjy"
)){
returnUrl
=
"https://idp.qpedu.cn/logout/logout.html?redirect_url="
+
returnUrl
;
returnUrl
=
"https://sp.etextbook.cn/Shibboleth.sso/Logout?return="
+
URLEncoder
.
encode
(
returnUrl
);
return
returnUrl
;
}
StringBuilder
loginUrlBuilder
=
new
StringBuilder
();
OauthConfigItem
oauthConfigItem
=
oauthConfig
.
getItems
().
get
(
oauthType
);
loginUrlBuilder
.
append
(
oauthConfigItem
.
getOauthUrl
());
if
(!
oauthConfigItem
.
getOauthUrl
().
endsWith
(
"/"
))
{
StringBuilder
loginUrlBuilder
=
new
StringBuilder
();
loginUrlBuilder
.
append
(
authSource
.
getOauthUrl
());
if
(!
authSource
.
getOauthUrl
().
endsWith
(
"/"
))
{
loginUrlBuilder
.
append
(
"/"
);
}
String
service
=
oauthConfigItem
.
getLogoutSuccessUrl
();
String
service
=
authSource
.
getLogoutSuccessUrl
();
if
(
StringUtils
.
isNotEmpty
(
returnUrl
))
{
Map
<
String
,
String
>
param
=
new
HashMap
<>();
param
.
put
(
"return_url"
,
URLEncoder
.
encode
(
returnUrl
));
service
=
appendUrl
(
service
,
param
);
}
String
clientId
=
oauthConfigItem
.
getClientId
();
String
signOrigin
=
oauthConfigItem
.
getClientId
()
+
"$$"
+
oauthConfigItem
.
getClientSecret
();
String
md5Hex
=
EncodeUtil
.
md5
(
signOrigin
);
loginUrlBuilder
.
append
(
"logout?service="
).
append
(
URLEncoder
.
encode
(
service
));
if
(
Objects
.
equals
(
authSource
.
getAuthType
(),
2
)){
// idp 认证
String
clientId
=
authSource
.
getClientId
();
String
signOrigin
=
authSource
.
getClientId
()
+
"$$"
+
authSource
.
getClientSecret
();
String
md5Hex
=
EncodeUtil
.
md5
(
signOrigin
);
loginUrlBuilder
.
append
(
"&client_id="
+
clientId
);
loginUrlBuilder
.
append
(
"&sign="
+
md5Hex
);
}
return
loginUrlBuilder
.
toString
();
}
// private String generateLogoutUrl(String returnUrl, String oauthType) {
// //oauthConfig + "/authorize?client_id=testClentId&redirect_uri=http%3a%2f%2f192.168.17.129%3a7774%2fauth%2floginsuccess.do&state=state"
// StringBuilder loginUrlBuilder = new StringBuilder();
// OauthConfigItem oauthConfigItem = oauthConfig.getItems().get(oauthType);
//
// loginUrlBuilder.append(oauthConfigItem.getOauthUrl());
// if (!oauthConfigItem.getOauthUrl().endsWith("/")) {
// loginUrlBuilder.append("/");
// }
// String service = oauthConfigItem.getLogoutSuccessUrl();
// if (StringUtils.isNotEmpty(returnUrl)) {
// Map<String, String> param = new HashMap<>();
// param.put("return_url", URLEncoder.encode(returnUrl));
// service = appendUrl(service, param);
// }
// loginUrlBuilder.append("logout?service=").append(URLEncoder.encode(service));
// return loginUrlBuilder.toString();
// }
//
//
// private String generateSpLogoutUrl(String returnUrl, String oauthType) {
//
// if (oauthType.equals("qpjy")){
// returnUrl = "https://idp.qpedu.cn/logout/logout.html?redirect_url=" + returnUrl;
// returnUrl = "https://sp.etextbook.cn/Shibboleth.sso/Logout?return=" + URLEncoder.encode(returnUrl);
// return returnUrl;
// }
// StringBuilder loginUrlBuilder = new StringBuilder();
// OauthConfigItem oauthConfigItem = oauthConfig.getItems().get(oauthType);
//
// loginUrlBuilder.append(oauthConfigItem.getOauthUrl());
// if (!oauthConfigItem.getOauthUrl().endsWith("/")) {
// loginUrlBuilder.append("/");
// }
// String service = oauthConfigItem.getLogoutSuccessUrl();
// if (StringUtils.isNotEmpty(returnUrl)) {
// Map<String, String> param = new HashMap<>();
// param.put("return_url", URLEncoder.encode(returnUrl));
// service = appendUrl(service, param);
// }
//
// String clientId = oauthConfigItem.getClientId();
// String signOrigin = oauthConfigItem.getClientId() + "$$" + oauthConfigItem.getClientSecret();
// String md5Hex = EncodeUtil.md5(signOrigin);
//
// loginUrlBuilder.append("logout?service=").append(URLEncoder.encode(service));
// loginUrlBuilder.append("&client_id="+clientId);
// loginUrlBuilder.append("&sign="+md5Hex);
// return loginUrlBuilder.toString();
// }
private
String
generateUserInfoUrl
(
String
accessToken
,
String
oauthType
)
{
//String url = "http://castest.edu.sh.cn/CAS/oauth2.0/accessToken?client_id=testClentId&client_secret=testClientSecret&redirect_uri=http%3a%2f%2f192.168.17.129%3a7774%2fauth%2floginsuccess2.do&code=" + code;
StringBuilder
builder
=
new
StringBuilder
();
OauthConfigItem
oauthConfigItem
=
oauthConfig
.
getItems
().
get
(
oauthType
);
builder
.
append
(
oauthConfigItem
.
getOauthUrl
());
if
(!
oauthConfigItem
.
getOauthUrl
().
endsWith
(
"/"
))
{
// OauthConfigItem oauthConfigItem = oauthConfig.getItems().get(oauthType);
AuthSource
authSource
=
authSourceService
.
getAuthSourceByKey
(
oauthType
);
builder
.
append
(
authSource
.
getOauthUrl
());
if
(!
authSource
.
getOauthUrl
().
endsWith
(
"/"
))
{
builder
.
append
(
"/"
);
}
builder
.
append
(
"oauth2.0/profile?access_token="
).
append
(
accessToken
);
...
...
@@ -534,15 +616,16 @@ public class AuthController {
private
String
generateAccessTokenUrl
(
String
code
,
String
oauthType
)
{
//String url = "http://castest.edu.sh.cn/CAS/oauth2.0/accessToken?client_id=testClentId&client_secret=testClientSecret&redirect_uri=http%3a%2f%2f192.168.17.129%3a7774%2fauth%2floginsuccess2.do&code=" + code;
StringBuilder
builder
=
new
StringBuilder
();
OauthConfigItem
oauthConfigItem
=
oauthConfig
.
getItems
().
get
(
oauthType
);
builder
.
append
(
oauthConfigItem
.
getOauthUrl
());
if
(!
oauthConfigItem
.
getOauthUrl
().
endsWith
(
"/"
))
{
// OauthConfigItem oauthConfigItem = oauthConfig.getItems().get(oauthType);
AuthSource
authSource
=
authSourceService
.
getAuthSourceByKey
(
oauthType
);
builder
.
append
(
authSource
.
getOauthUrl
());
if
(!
authSource
.
getOauthUrl
().
endsWith
(
"/"
))
{
builder
.
append
(
"/"
);
}
builder
.
append
(
"oauth2.0/accessToken?client_id="
).
append
(
oauthConfigItem
.
getClientId
());
builder
.
append
(
"&client_secret="
).
append
(
oauthConfigItem
.
getClientSecret
());
builder
.
append
(
"oauth2.0/accessToken?client_id="
).
append
(
authSource
.
getClientId
());
builder
.
append
(
"&client_secret="
).
append
(
authSource
.
getClientSecret
());
builder
.
append
(
"&code="
).
append
(
code
);
builder
.
append
(
"&redirect_uri="
).
append
(
oauthConfigItem
.
getLoginSuccessUrl
());
builder
.
append
(
"&redirect_uri="
).
append
(
authSource
.
getLoginSuccessUrl
());
builder
.
append
(
"&grant_type=authorization_code"
);
return
builder
.
toString
();
}
...
...
@@ -574,11 +657,13 @@ public class AuthController {
public
ModelAndView
chineseallLogin
(
HttpServletRequest
request
,
HttpServletResponse
response
)
{
ModelAndView
modelAndView
=
new
ModelAndView
();
modelAndView
.
setViewName
(
"toward"
);
Map
<
String
,
ClientItem
>
clientItemMap
=
oauthClient
.
getClients
().
stream
().
collect
(
Collectors
.
toMap
(
ClientItem:
:
getClientId
,
Function
.
identity
()));
List
<
AuthClient
>
authClientList
=
authClientService
.
listAll
();
Map
<
String
,
AuthClient
>
clientMap
=
authClientList
.
stream
().
collect
(
Collectors
.
toMap
(
AuthClient:
:
getKey
,
Function
.
identity
(),
(
o1
,
o2
)
->
o1
));
// Map<String, ClientItem> clientItemMap = oauthClient.getClients().stream().collect(Collectors.toMap(ClientItem::getClientId, Function.identity()));
try
{
String
clientId
=
request
.
getParameter
(
"client_id"
);
String
returnUrl
=
request
.
getParameter
(
"redirect_uri"
);
ClientItem
clientItem
=
clientItem
Map
.
get
(
clientId
);
AuthClient
clientItem
=
client
Map
.
get
(
clientId
);
AuthLog
authLog
=
new
AuthLog
();
authLog
.
setLogType
(
LogType
.
login
);
if
(
null
==
clientItem
)
{
...
...
@@ -587,9 +672,9 @@ public class AuthController {
return
modelAndView
;
}
authLog
.
setClientId
(
clientId
);
authLog
.
setAuthSource
(
clientItem
.
get
Client
Name
());
authLog
.
setAuthSource
(
clientItem
.
getName
());
String
sign
=
request
.
getParameter
(
"sign"
);
String
signOrigin
=
clientItem
.
get
ClientId
()
+
"$$"
+
clientItem
.
getClien
tSecret
();
String
signOrigin
=
clientItem
.
get
Key
()
+
"$$"
+
clientItem
.
ge
tSecret
();
String
md5Hex
=
EncodeUtil
.
md5
(
signOrigin
);
if
(!
Objects
.
equals
(
sign
,
md5Hex
))
{
...
...
@@ -602,11 +687,13 @@ public class AuthController {
modelAndView
.
addObject
(
"message"
,
"登录成功回调地址为空"
);
return
modelAndView
;
}
authLog
.
setOauthType
(
OauthType
.
dianjiaoguan
.
getCode
());
OauthConfigItem
oauthConfigItem
=
oauthConfig
.
getItems
().
get
(
OauthType
.
dianjiaoguan
.
getCode
());
authLog
.
setFowardUrl
(
oauthConfigItem
.
getOauthUrl
());
String
key
=
"dianjiaoguan"
;
AuthSource
authSource
=
authSourceService
.
getAuthSourceByKey
(
key
);
authLog
.
setOauthType
(
key
);
// OauthConfigItem oauthConfigItem = oauthConfig.getItems().get(OauthType.dianjiaoguan.getCode());
authLog
.
setFowardUrl
(
authSource
.
getOauthUrl
());
authLogService
.
save
(
authLog
);
String
url
=
generateLoginUrl
(
returnUrl
,
OauthType
.
dianjiaoguan
.
getCode
()
,
authLog
.
getId
());
String
url
=
generateLoginUrl
(
returnUrl
,
authSource
,
authLog
.
getId
());
modelAndView
.
addObject
(
"loginUrl"
,
url
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
...
...
@@ -620,17 +707,19 @@ public class AuthController {
public
ModelAndView
shLogout
(
HttpServletRequest
request
,
HttpServletResponse
response
)
{
ModelAndView
modelAndView
=
new
ModelAndView
();
modelAndView
.
setViewName
(
"logout"
);
Map
<
String
,
ClientItem
>
clientItemMap
=
oauthClient
.
getClients
().
stream
().
collect
(
Collectors
.
toMap
(
ClientItem:
:
getClientId
,
Function
.
identity
()));
List
<
AuthClient
>
authClientList
=
authClientService
.
listAll
();
Map
<
String
,
AuthClient
>
clientMap
=
authClientList
.
stream
().
collect
(
Collectors
.
toMap
(
AuthClient:
:
getKey
,
Function
.
identity
(),
(
o1
,
o2
)
->
o1
));
// Map<String, ClientItem> clientItemMap = oauthClient.getClients().stream().collect(Collectors.toMap(ClientItem::getClientId, Function.identity()));
try
{
String
clientId
=
request
.
getParameter
(
"client_id"
);
ClientItem
clientItem
=
clientItem
Map
.
get
(
clientId
);
AuthClient
clientItem
=
client
Map
.
get
(
clientId
);
if
(
clientItem
==
null
)
{
modelAndView
.
setViewName
(
"error"
);
modelAndView
.
addObject
(
"message"
,
"client_id为空或者不正确"
);
return
modelAndView
;
}
String
sign
=
request
.
getParameter
(
"sign"
);
String
signOrigin
=
clientItem
.
get
ClientId
()
+
"$$"
+
clientItem
.
getClien
tSecret
();
String
signOrigin
=
clientItem
.
get
Key
()
+
"$$"
+
clientItem
.
ge
tSecret
();
String
md5Hex
=
EncodeUtil
.
md5
(
signOrigin
);
if
(!
Objects
.
equals
(
sign
,
md5Hex
))
{
...
...
@@ -645,15 +734,18 @@ public class AuthController {
return
modelAndView
;
}
String
key
=
"dianjiaoguan"
;
AuthSource
authSource
=
authSourceService
.
getAuthSourceByKey
(
key
);
AuthLog
authLog
=
new
AuthLog
();
authLog
.
setOauthType
(
OauthType
.
dianjiaoguan
.
getCode
()
);
authLog
.
setOauthType
(
key
);
authLog
.
setReturnUrl
(
service
);
authLog
.
setClientId
(
clientId
);
authLog
.
setAuthSource
(
clientItem
.
get
Client
Name
());
authLog
.
setAuthSource
(
clientItem
.
getName
());
authLog
.
setLogType
(
LogType
.
logout
);
authLogService
.
save
(
authLog
);
if
(
StringUtils
.
isNotEmpty
(
service
))
{
response
.
sendRedirect
(
generateLogoutUrl
(
service
,
OauthType
.
dianjiaoguan
.
getCode
()
));
response
.
sendRedirect
(
generateLogoutUrl
(
service
,
authSource
));
}
return
null
;
}
catch
(
Exception
e
)
{
...
...
eden-authcenter-agent/src/main/java/com/chineseall/eden/authcenter/agent/controller/UcenterController.java
View file @
f05b7b0a
...
...
@@ -321,7 +321,7 @@ public class UcenterController {
authLog
.
setLogUserInfo
(
logUserInfo
);
}
clientDataInfo
.
setAuthUserInfo
(
userInfo
);
clientDataInfo
.
setOauthType
(
OauthType
.
valueOf
(
oauthType
)
);
clientDataInfo
.
setOauthType
(
oauthType
);
modelAndView
.
addObject
(
"clientDataInfo"
,
clientDataInfo
);
modelAndView
.
addObject
(
"successPostUrl"
,
returnUrl
);
...
...
@@ -393,7 +393,7 @@ public class UcenterController {
authLog
.
setLogUserInfo
(
logUserInfo
);
}
clientDataInfo
.
setAuthUserInfo
(
userInfo
);
clientDataInfo
.
setOauthType
(
OauthType
.
valueOf
(
oauthType
)
);
clientDataInfo
.
setOauthType
(
oauthType
);
modelAndView
.
addObject
(
"clientDataInfo"
,
clientDataInfo
);
modelAndView
.
addObject
(
"successPostUrl"
,
returnUrl
);
...
...
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