增加sso-client过滤器

This commit is contained in:
shuzheng 2016-12-10 21:32:50 +08:00
parent f1a38be9ec
commit 167251bb45
6 changed files with 94 additions and 16 deletions

View File

@ -73,11 +73,12 @@
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.0.0.v20130308</version>
<!--<version>9.0.0.v20130308</version>-->
<version>9.2.7.v20150116</version>
<configuration>
<scanIntervalSeconds>3</scanIntervalSeconds>
<webApp>
<contextPath>/zheng-cms-job</contextPath>
<contextPath>/</contextPath>
</webApp>
<httpConnector>
<port>2223</port>

View File

@ -46,6 +46,12 @@
<artifactId>rocketmq-client</artifactId>
<version>3.6.2.Final</version>
</dependency>
<!-- zheng-upms-client -->
<dependency>
<groupId>com.zheng</groupId>
<artifactId>zheng-upms-client</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
<profiles>

View File

@ -63,6 +63,24 @@
<url-pattern>/*</url-pattern>
</filter-mapping>
-->
<!-- zhang-upms-client -->
<filter>
<filter-name>zheng-upms-client</filter-name>
<filter-class>com.zheng.upms.client.filter.SSOFilter</filter-class>
<init-param>
<param-name>system_name</param-name>
<param-value>zheng-cms-web</param-value>
</init-param>
<init-param>
<param-name>sso_server_url</param-name>
<param-value>http://upms.zhengshuzheng.cn:1111/sso/login</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>zheng-upms-client</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Druid连接池监控页面 -->
<servlet>

View File

@ -27,7 +27,7 @@
</dependency>
<dependency>
<groupId>com.zheng</groupId>
<artifactId>zheng-upms-service</artifactId>
<artifactId>zheng-common</artifactId>
<version>1.0.0</version>
<type>jar</type>
</dependency>

View File

@ -1,13 +0,0 @@
package com.zheng;
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
System.out.println( "Hello World!" );
}
}

View File

@ -0,0 +1,66 @@
package com.zheng.upms.client.filter;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.net.URLEncoder;
/**
* Created by shuzheng on 2016/12/10.
*/
public class SSOFilter implements Filter {
private static Logger _log = LoggerFactory.getLogger(SSOFilter.class);
private String SYSTEM_NAME = "system_name";
private String SSO_SERVER_URL = "sso_server_url";
private FilterConfig filterConfig;
@Override
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) servletRequest;
HttpServletResponse response = (HttpServletResponse) servletResponse;
HttpSession session = request.getSession();
// 已登录
if (null != session.getAttribute("isLogin")) {
filterChain.doFilter(request, response);
return;
}
// 未登录
else {
// 跳转sso-server认证中心并带上回调地址和系统名称参数
// 认证中心地址
StringBuffer sso_server_url = new StringBuffer(filterConfig.getInitParameter(SSO_SERVER_URL));
// 参数system_name
sso_server_url.append("?").append(SYSTEM_NAME).append("=").append(filterConfig.getInitParameter(SYSTEM_NAME));
// 参数backurl
StringBuffer backurl = request.getRequestURL();
String queryString = request.getQueryString();
if (!StringUtils.isEmpty(queryString)) {
backurl.append("?").append(queryString);
}
sso_server_url.append("&").append("backurl").append("=").append(URLEncoder.encode(backurl.toString(), "utf-8"));
_log.info("未登录,跳转认证中心:{}", sso_server_url);
response.sendRedirect(sso_server_url.toString());
}
}
@Override
public void destroy() {
}
}