Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
A
apollo
Project
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
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
openSource
apollo
Commits
7c2c539d
Commit
7c2c539d
authored
Jun 03, 2016
by
Jason Song
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #246 from lepdou/restructure
sso相关代码重构
parents
a779c281
f3c7ccd3
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
102 additions
and
55 deletions
+102
-55
CtripLogoutHandler.java
...trip/framework/apollo/portal/auth/CtripLogoutHandler.java
+39
-0
CtripUserInfoHolder.java
...rip/framework/apollo/portal/auth/CtripUserInfoHolder.java
+2
-2
DefaultLogoutHandler.java
...ip/framework/apollo/portal/auth/DefaultLogoutHandler.java
+18
-0
DefaultUserInfoHolder.java
...p/framework/apollo/portal/auth/DefaultUserInfoHolder.java
+2
-2
LogoutHandler.java
...com/ctrip/framework/apollo/portal/auth/LogoutHandler.java
+10
-0
AuthConfiguration.java
...mework/apollo/portal/configutation/AuthConfiguration.java
+22
-5
ServletContextConfiguration.java
...llo/portal/configutation/ServletContextConfiguration.java
+5
-3
PortalUserInfoController.java
...rk/apollo/portal/controller/PortalUserInfoController.java
+4
-43
No files found.
apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/auth/CtripLogoutHandler.java
0 → 100644
View file @
7c2c539d
package
com
.
ctrip
.
framework
.
apollo
.
portal
.
auth
;
import
com.ctrip.framework.apollo.portal.repository.ServerConfigRepository
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
java.io.IOException
;
import
javax.servlet.http.Cookie
;
import
javax.servlet.http.HttpServletRequest
;
import
javax.servlet.http.HttpServletResponse
;
public
class
CtripLogoutHandler
implements
LogoutHandler
{
@Autowired
private
ServerConfigRepository
serverConfigRepository
;
@Override
public
void
logout
(
HttpServletRequest
request
,
HttpServletResponse
response
)
{
//将session销毁
request
.
getSession
().
invalidate
();
Cookie
cookie
=
new
Cookie
(
"memCacheAssertionID"
,
null
);
//将cookie的有效期设置为0,命令浏览器删除该cookie
cookie
.
setMaxAge
(
0
);
cookie
.
setPath
(
request
.
getContextPath
()
+
"/"
);
response
.
addCookie
(
cookie
);
//重定向到SSO的logout地址
String
casServerUrl
=
serverConfigRepository
.
findByKey
(
"casServerUrlPrefix"
).
getValue
();
String
serverName
=
serverConfigRepository
.
findByKey
(
"serverName"
).
getValue
();
try
{
response
.
sendRedirect
(
casServerUrl
+
"/logout?service="
+
serverName
);
}
catch
(
IOException
e
)
{
throw
new
RuntimeException
(
e
);
}
}
}
apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/auth/CtripUserInfoHolder.java
View file @
7c2c539d
...
...
@@ -21,7 +21,7 @@ public class CtripUserInfoHolder implements UserInfoHolder{
assertionHolder
=
clazz
.
newInstance
();
getAssertion
=
assertionHolder
.
getClass
().
getMethod
(
"getAssertion"
);
}
catch
(
Exception
e
)
{
throw
new
RuntimeException
(
"in
stance listen
er fail"
,
e
);
throw
new
RuntimeException
(
"in
it AssertionHold
er fail"
,
e
);
}
}
...
...
@@ -40,7 +40,7 @@ public class CtripUserInfoHolder implements UserInfoHolder{
return
userInfo
;
}
catch
(
Exception
e
)
{
throw
new
RuntimeException
(
""
,
e
);
throw
new
RuntimeException
(
"
get user info from assertion holder error
"
,
e
);
}
}
...
...
apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/auth/DefaultLogoutHandler.java
0 → 100644
View file @
7c2c539d
package
com
.
ctrip
.
framework
.
apollo
.
portal
.
auth
;
import
java.io.IOException
;
import
javax.servlet.http.HttpServletRequest
;
import
javax.servlet.http.HttpServletResponse
;
public
class
DefaultLogoutHandler
implements
LogoutHandler
{
@Override
public
void
logout
(
HttpServletRequest
request
,
HttpServletResponse
response
)
{
try
{
response
.
sendRedirect
(
"/"
);
}
catch
(
IOException
e
)
{
throw
new
RuntimeException
(
e
);
}
}
}
apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/auth/
NotCtrip
UserInfoHolder.java
→
apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/auth/
Default
UserInfoHolder.java
View file @
7c2c539d
...
...
@@ -5,10 +5,10 @@ import com.ctrip.framework.apollo.portal.entity.po.UserInfo;
/**
* 不是ctrip的公司默认提供一个假用户
*/
public
class
NotCtrip
UserInfoHolder
implements
UserInfoHolder
{
public
class
Default
UserInfoHolder
implements
UserInfoHolder
{
public
NotCtrip
UserInfoHolder
(){
public
Default
UserInfoHolder
(){
}
...
...
apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/auth/LogoutHandler.java
0 → 100644
View file @
7c2c539d
package
com
.
ctrip
.
framework
.
apollo
.
portal
.
auth
;
import
javax.servlet.http.HttpServletRequest
;
import
javax.servlet.http.HttpServletResponse
;
public
interface
LogoutHandler
{
void
logout
(
HttpServletRequest
request
,
HttpServletResponse
response
);
}
apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/configutation/AuthConfiguration.java
View file @
7c2c539d
package
com
.
ctrip
.
framework
.
apollo
.
portal
.
configutation
;
import
com.ctrip.framework.apollo.portal.auth.CtripLogoutHandler
;
import
com.ctrip.framework.apollo.portal.auth.CtripUserInfoHolder
;
import
com.ctrip.framework.apollo.portal.auth.NotCtripUserInfoHolder
;
import
com.ctrip.framework.apollo.portal.auth.DefaultLogoutHandler
;
import
com.ctrip.framework.apollo.portal.auth.DefaultUserInfoHolder
;
import
com.ctrip.framework.apollo.portal.auth.LogoutHandler
;
import
com.ctrip.framework.apollo.portal.auth.UserInfoHolder
;
import
com.ctrip.framework.apollo.portal.repository.ServerConfigRepository
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean
;
import
org.springframework.boot.context.embedded.FilterRegistrationBean
;
import
org.springframework.boot.context.embedded.ServletListenerRegistrationBean
;
import
org.springframework.context.annotation.Bean
;
...
...
@@ -31,7 +36,7 @@ public class AuthConfiguration {
*/
@Configuration
@Profile
(
"ctrip"
)
static
class
Ctrip
ProfileConfiguration
{
static
class
Ctrip
AuthAutoConfiguration
{
@Autowired
private
ServerConfigRepository
serverConfigRepository
;
...
...
@@ -109,6 +114,11 @@ public class AuthConfiguration {
return
new
CtripUserInfoHolder
();
}
@Bean
public
CtripLogoutHandler
logoutHandler
(){
return
new
CtripLogoutHandler
();
}
private
Filter
filter
(
String
className
){
Class
clazz
=
null
;
try
{
...
...
@@ -137,11 +147,18 @@ public class AuthConfiguration {
* 默认实现
*/
@Configuration
static
class
NotCtripProfileConfiguration
{
static
class
DefaultAuthAutoConfiguration
{
@Bean
@ConditionalOnMissingBean
(
UserInfoHolder
.
class
)
public
DefaultUserInfoHolder
notCtripUserInfoHolder
(){
return
new
DefaultUserInfoHolder
();
}
@Bean
public
NotCtripUserInfoHolder
notCtripUserInfoHolder
(){
return
new
NotCtripUserInfoHolder
();
@ConditionalOnMissingBean
(
LogoutHandler
.
class
)
public
DefaultLogoutHandler
logoutHandler
(){
return
new
DefaultLogoutHandler
();
}
}
...
...
apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/configutation/ServletContextConfiguration.java
View file @
7c2c539d
...
...
@@ -7,11 +7,13 @@ import org.springframework.beans.factory.annotation.Autowired;
import
org.springframework.boot.context.embedded.ServletContextInitializer
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.context.annotation.Profile
;
import
javax.servlet.ServletContext
;
import
javax.servlet.ServletException
;
@Configuration
@Profile
(
"ctrip"
)
public
class
ServletContextConfiguration
{
@Autowired
...
...
@@ -27,9 +29,9 @@ public class ServletContextConfiguration {
ServerConfig
loggingServerIP
=
serverConfigRepository
.
findByKey
(
"loggingServerIP"
);
ServerConfig
loggingServerPort
=
serverConfigRepository
.
findByKey
(
"loggingServerPort"
);
ServerConfig
credisServiceUrl
=
serverConfigRepository
.
findByKey
(
"credisServiceUrl"
);
servletContext
.
setInitParameter
(
"loggingServerIP"
,
loggingServerIP
==
null
?
""
:
loggingServerIP
.
getValue
());
servletContext
.
setInitParameter
(
"loggingServerPort"
,
loggingServerPort
==
null
?
""
:
loggingServerPort
.
getValue
());
servletContext
.
setInitParameter
(
"credisServiceUrl"
,
credisServiceUrl
==
null
?
""
:
credisServiceUrl
.
getValue
());
servletContext
.
setInitParameter
(
"loggingServerIP"
,
loggingServerIP
==
null
?
""
:
loggingServerIP
.
getValue
());
servletContext
.
setInitParameter
(
"loggingServerPort"
,
loggingServerPort
==
null
?
""
:
loggingServerPort
.
getValue
());
servletContext
.
setInitParameter
(
"credisServiceUrl"
,
credisServiceUrl
==
null
?
""
:
credisServiceUrl
.
getValue
());
}
};
}
...
...
apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/controller/PortalUserInfoController.java
View file @
7c2c539d
package
com
.
ctrip
.
framework
.
apollo
.
portal
.
controller
;
import
com.ctrip.framework.apollo.core.exception.BadRequestException
;
import
com.ctrip.framework.apollo.portal.auth.CtripUserInfoHolder
;
import
com.ctrip.framework.apollo.portal.auth.NotCtripUserInfoHolder
;
import
com.ctrip.framework.apollo.portal.auth.LogoutHandler
;
import
com.ctrip.framework.apollo.portal.auth.UserInfoHolder
;
import
com.ctrip.framework.apollo.portal.entity.po.UserInfo
;
import
com.ctrip.framework.apollo.portal.repository.ServerConfigRepository
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.beans.factory.NoSuchBeanDefinitionException
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.context.ApplicationContext
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RestController
;
import
java.io.IOException
;
import
javax.annotation.PostConstruct
;
import
javax.servlet.http.Cookie
;
import
javax.servlet.http.HttpServletRequest
;
import
javax.servlet.http.HttpServletResponse
;
@RestController
public
class
PortalUserInfoController
{
private
static
Logger
logger
=
LoggerFactory
.
getLogger
(
PortalUserInfoController
.
class
);
@Autowired
private
ApplicationContext
applicationContext
;
@Autowired
private
ServerConfigRepository
serverConfigRepository
;
private
UserInfoHolder
userInfoHolder
;
@PostConstruct
public
void
post
()
{
try
{
userInfoHolder
=
applicationContext
.
getBean
(
CtripUserInfoHolder
.
class
);
}
catch
(
NoSuchBeanDefinitionException
e
)
{
logger
.
debug
(
"default user info holder"
);
userInfoHolder
=
applicationContext
.
getBean
(
NotCtripUserInfoHolder
.
class
);
}
}
@Autowired
private
LogoutHandler
logoutHandler
;
@RequestMapping
(
"/user"
)
public
UserInfo
getCurrentUserName
()
{
try
{
return
userInfoHolder
.
getUser
();
}
catch
(
Exception
e
)
{
throw
new
BadRequestException
(
"请先登录"
);
}
}
@RequestMapping
(
"/user/logout"
)
public
void
logout
(
HttpServletRequest
request
,
HttpServletResponse
response
)
throws
IOException
{
//将session销毁
request
.
getSession
().
invalidate
();
Cookie
cookie
=
new
Cookie
(
"memCacheAssertionID"
,
null
);
//将cookie的有效期设置为0,命令浏览器删除该cookie
cookie
.
setMaxAge
(
0
);
cookie
.
setPath
(
request
.
getContextPath
()
+
"/"
);
response
.
addCookie
(
cookie
);
//重定向到SSO的logout地址
String
casServerUrl
=
serverConfigRepository
.
findByKey
(
"casServerUrlPrefix"
).
getValue
();
String
serverName
=
serverConfigRepository
.
findByKey
(
"serverName"
).
getValue
();
response
.
sendRedirect
(
casServerUrl
+
"/logout?service="
+
serverName
);
logoutHandler
.
logout
(
request
,
response
);
}
}
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