package SSO;
import java.io.IOException;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
public class SSOFilter
implements Filter
{
private FilterConfig filterConfig = null;
private String cookieName = "WangYuDesktopSSOID";
private String SSOServiceURL = "http://wangyu.prc.sun.com:8080/SSOAuth/SSOAuth";
private String SSOLoginPage = "http://wangyu.prc.sun.com:8080/SSOAuth/login.jsp";
private static final boolean debug = true;
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
throws IOException, ServletException
{
log("SSOFilter:doFilter()");
HttpServletRequest request = (HttpServletRequest)req;
HttpServletResponse response = (HttpServletResponse)res;
String result = "failed";
String url = request.getRequestURL().toString();
String qstring = request.getQueryString();
if (qstring == null) qstring = "";
String cookieValue = "";
Cookie[] diskCookies = request.getCookies();
if (diskCookies != null)
{
for (int i = 0; i < diskCookies.length; i++) {
if (diskCookies[i].getName().equals(this.cookieName)) {
cookieValue = diskCookies[i].getValue();
result = SSOService(cookieValue);
log("found cookies!");
}
}
}
if (result.equals("failed")) {
response.sendRedirect(this.SSOLoginPage + "?goto=" + url);
} else if (qstring.indexOf("logout") > 1) {
log("logout action!");
logoutService(cookieValue);
response.sendRedirect(this.SSOLoginPage + "?goto=" + url);
} else {
request.setAttribute("SSOUser", result);
Throwable problem = null;
try {
chain.doFilter(req, res);
} catch (Throwable t) {
problem = t;
t.printStackTrace();
}
if (problem != null) {
if ((problem instanceof ServletException)) throw ((ServletException)problem);
if ((problem instanceof IOException)) throw ((IOException)problem);
sendProcessingError(problem, res);
}
}
}
public FilterConfig getFilterConfig()
{
return this.filterConfig;
}
public void setFilterConfig(FilterConfig filterConfig)
{
this.filterConfig = filterConfig;
}
public void destroy()
{
}
public void init(FilterConfig filterConfig)
{
this.filterConfig = filterConfig;
if (filterConfig != null)
{
log("SSOFilter:Initializing filter");
}
this.cookieName = filterConfig.getInitParameter("cookieName");
this.SSOServiceURL = filterConfig.getInitParameter("SSOServiceURL");
this.SSOLoginPage = filterConfig.getInitParameter("SSOLoginPage");
}
public String toString()
{
if (this.filterConfig == null) return "SSOFilter()";
StringBuffer sb = new StringBuffer("SSOFilter(");
sb.append(this.filterConfig);
sb.append(")");
return sb.toString();
}
private void sendProcessingError(Throwable t, ServletResponse response)
{
String stackTrace = getStackTrace(t);
if ((stackTrace != null) && (!stackTrace.equals("")))
{
try
{
response.setContentType("text/html");
PrintStream ps = new PrintStream(response.getOutputStream());
PrintWriter pw = new PrintWriter(ps);
pw.print("<html>\n<head>\n<title>Error</title>\n</head>\n<body>\n");
pw.print("<h1>The resource did not process correctly</h1>\n<pre>\n");
pw.print(stackTrace);
pw.print("</pre></body>\n</html>");
pw.close();
ps.close();
response.getOutputStream().close();
}
catch (Exception ex) {
}
}
else try {
PrintStream ps = new PrintStream(response.getOutputStream());
t.printStackTrace(ps);
ps.close();
response.getOutputStream().close();
}
catch (Exception ex)
{
}
}
public static String getStackTrace(Throwable t) { String stackTrace = null;
try
{
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
t.printStackTrace(pw);
pw.close();
sw.close();
stackTrace = sw.getBuffer().toString(); } catch (Exception ex) {
}
return stackTrace; }
private String SSOService(String cookievalue)
throws IOException
{
String authAction = "?action=authcookie&cookiename=";
HttpClient httpclient = new HttpClient();
GetMethod httpget = new GetMethod(this.SSOServiceURL + authAction + cookievalue);
try
{
httpclient.executeMethod(httpget);
String result = httpget.getResponseBodyAsString();
return result;
} finally {
httpget.releaseConnection();
}
}
private void logoutService(String cookievalue) throws IOException {
String authAction = "?action=logout&cookiename=";
HttpClient httpclient = new HttpClient();
GetMethod httpget = new GetMethod(this.SSOServiceURL + authAction + cookievalue);
try {
httpclient.executeMethod(httpget);
httpget.getResponseBodyAsString();
} finally {
httpget.releaseConnection();
}
}
public void log(String msg)
{
this.filterConfig.getServletContext().log(msg);
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
网上简单的SSO CAS 单点登录源码
共65个文件
jar:8个
png:6个
xml:6个
4星 · 超过85%的资源 需积分: 9 180 下载量 62 浏览量
2013-11-05
15:58:16
上传
评论
收藏 767KB RAR 举报
温馨提示
网上 简单的java实现简单的单点登录 的源码 这是链接:http://www.blogjava.net/xcp/archive/2010/04/13/318125.html
资源推荐
资源详情
资源评论
收起资源包目录
SSO.rar (65个子文件)
SSOWebDemo2
.project 2KB
.mymetadata 306B
src
SSO
SSOFilter.java 6KB
images
topback.png 236B
Thumbs.db 24KB
background2.gif 4KB
bottomback.png 230B
WebRoot
WEB-INF
classes
SSO
SSOFilter.class 6KB
lib
commons-logging-api.jar 26KB
commons-codec-1.3.jar 46KB
commons-httpclient-3.0.jar 273KB
commons-logging.jar 37KB
web.xml 1KB
index.jsp 834B
wangyu.css 3KB
META-INF
MANIFEST.MF 36B
test.jsp 747B
.myeclipse
.settings
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.common.project.facet.core.xml 206B
org.eclipse.jdt.core.prefs 395B
org.eclipse.wst.jsdt.ui.superType.name 6B
.jsdtscope 493B
.classpath 687B
SSOAuth
.project 2KB
.mymetadata 294B
src
DesktopSSO
SSOAuth.java 5KB
images
topback.png 236B
background2.gif 4KB
bottomback.png 230B
WebRoot
WEB-INF
classes
DesktopSSO
SSOAuth.class 5KB
lib
web.xml 1KB
index.jsp 834B
wangyu.css 3KB
META-INF
MANIFEST.MF 36B
failed.html 552B
login.jsp 2KB
.myeclipse
.settings
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.common.project.facet.core.xml 206B
org.eclipse.jdt.core.prefs 395B
org.eclipse.wst.jsdt.ui.superType.name 6B
.jsdtscope 493B
.classpath 362B
SSOWebDemo1
.project 2KB
.mymetadata 306B
src
SSO
SSOFilter.java 6KB
images
topback.png 236B
Thumbs.db 24KB
background2.gif 4KB
bottomback.png 230B
WebRoot
WEB-INF
classes
SSO
SSOFilter.class 6KB
lib
commons-logging-api.jar 26KB
commons-codec-1.3.jar 46KB
commons-httpclient-3.0.jar 273KB
commons-logging.jar 37KB
web.xml 1KB
index.jsp 834B
wangyu.css 3KB
META-INF
MANIFEST.MF 36B
test.jsp 747B
.myeclipse
.settings
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.common.project.facet.core.xml 206B
org.eclipse.jdt.core.prefs 395B
org.eclipse.wst.jsdt.ui.superType.name 6B
.jsdtscope 493B
.classpath 687B
共 65 条
- 1
zf0625
- 粉丝: 0
- 资源: 10
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页