提交 4c071efb 编写于 作者: L lepdou

sso相关代码重构

上级 8c57944b
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);
}
}
}
......@@ -21,7 +21,7 @@ public class CtripUserInfoHolder implements UserInfoHolder{
assertionHolder = clazz.newInstance();
getAssertion = assertionHolder.getClass().getMethod("getAssertion");
} catch (Exception e) {
throw new RuntimeException("instance listener fail", e);
throw new RuntimeException("init AssertionHolder 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);
}
}
......
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);
}
}
}
......@@ -5,10 +5,10 @@ import com.ctrip.framework.apollo.portal.entity.po.UserInfo;
/**
* 不是ctrip的公司默认提供一个假用户
*/
public class NotCtripUserInfoHolder implements UserInfoHolder{
public class DefaultUserInfoHolder implements UserInfoHolder{
public NotCtripUserInfoHolder(){
public DefaultUserInfoHolder(){
}
......
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);
}
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.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 +34,7 @@ public class AuthConfiguration {
*/
@Configuration
@Profile("ctrip")
static class CtripProfileConfiguration{
static class CtripAuthAutoConfiguration {
@Autowired
private ServerConfigRepository serverConfigRepository;
......@@ -109,6 +112,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 +145,17 @@ public class AuthConfiguration {
* 默认实现
*/
@Configuration
static class NotCtripProfileConfiguration{
@ConditionalOnMissingBean(CtripUserInfoHolder.class)
static class DefaultAuthAutoConfiguration {
@Bean
public DefaultUserInfoHolder notCtripUserInfoHolder(){
return new DefaultUserInfoHolder();
}
@Bean
public NotCtripUserInfoHolder notCtripUserInfoHolder(){
return new NotCtripUserInfoHolder();
public DefaultLogoutHandler logoutHandler(){
return new DefaultLogoutHandler();
}
}
......
......@@ -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());
}
};
}
......
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);
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册