Yale CAS最佳实践 --全部

Yale CAS最佳实践 第一部分:配置篇
1.环境准备
Jdk1.4.2

Tomcat5.0.28
cas-server-2.0.12(没有采用更高版本,是因为它最简单明了)
cas-client-java-2.1.1
2.在jdk上配置SSL
到http://java.sun.com/products/jsse/去下载jsse,我用的是1.0.3;下载下来后是一个zip包,把里边lib目录下的jar包复制到你的jdk目录下的jre\lib\ext目录中,是三个文件:jsse.jar;jnet.jar;jcert.jar

3.SSL验证证书
3.1.生成
keytool -genkey -alias tomcat -keyalg RSA
? 如果C:\Documents and Settings\Administrator\.keystore已经存在,请先删除。
? 输入tomcat本身的缺省口令changeit
? 用户前名和用户后名都用localhost
keytool -export -alias tomcat -file server.crt
只能输入tomcat的缺省口令changeit
keytool -import -trustcacerts -alias tomcat -file server.crt -keystore %java_home%/jre/lib/security/cacerts
3.2.显示
keytool -list -v -keystore %java_home%/jre/lib/security/cacerts > t.txt
3.3.删除
keytool -delete -alias tomcat -keystore  %java_home%/jre/lib/security/cacerts -keypass changeit
只能输入tomcat的缺省口令changeit
3.4.keytool参考
%JAVA_HOME%\bin\keytool -delete -alias tomcat -keypass changeit
%JAVA_HOME%\bin\keytool -genkey -alias tomcat -keypass changeit -keyalg RSA
%JAVA_HOME%\bin\keytool -export -alias tomcat -keypass changeit  -file %FILE_NAME%
%JAVA_HOME%\bin\keytool -import -file server.crt -keypass changeit
-keystore %JAVA_HOME%/jre/lib/security/cacerts
%JAVA_HOME%\bin\keytool -import -file server.crt -keypass changeit
%JAVA_HOME%\bin\keytool -genkey -alias tomcat -keypass changeit -keyalg RSA -validity 365

4.Tomcat配置
4.1.拷贝
拷贝C:\Documents and Settings\Administrator\.keystore到%tomcat_home%\conf\
4.2.配置
编辑%tomcat_home%\conf\server.xml,去掉ssl的注释,并更改为如下配置
    <Connector port="8443" keystorePass="changeit" keystoreFile="conf/.keystore"
               maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
               enableLookups="false" disableUploadTimeout="true"
               acceptCount="100" debug="0" scheme="https" secure="true"
               clientAuth="false" sslProtocol="TLS" />
4.3.注意
请保持C:\Documents and Settings\Administrator\.keystore与%tomcat_home%\conf\.keystore一致

5.Tomcat jmx bug
在sun网站上http://java.sun.com/javase/technologies/core/mntr-mgmt/javamanagement/download.jsp
下载jmx-1_2_1-ri.zip,解压后,把jmxri.jar重命名为jmx.jar,覆盖tomcat/bin目录下的jmx.jar
这个适用于cas-server-3.*


Yale CAS最佳实践 第二部分:原理篇

6.概念术语
CAS(Central Authentication Service)
TGT(Ticket Granting Ticket)
ST(Service Ticket)
PGT(Proxy Granting Ticket)
7.原理剖析
Yale CAS使用了Ticket Granting Cookie (简称TGC)去作为获取Service Ticket(简称ST)的凭据,这个TGC 是保存在客户端的cookie,即当第2次被其他CAS Client重定向的时候,CAS Server实际上已经从用户的Cookie中抓取到TGC,然后知道TGC对应的用户,因此避免了再次登录,如果CAS Server抓取不到TGC,则用户需要登陆。

众所周知,cookie是不能跨域的。但是CAS能够做abc.com和xyz.com的sso,因为CAS Server缓存了所有的ticket,所以Client无需共享cookies。

Yale CAS最佳实践 第三部分:源代码研读

8.源代码研读
8.1.web.xml参数定义:
edu.yale.its.tp.cas.client.filter.loginUrl: CAS server的login URL. (Required)
edu.yale.its.tp.cas.client.filter.validateUrl: CAS server的URL验证器. (Required)
edu.yale.its.tp.cas.client.filter.serviceUrl: URL of this service. (Required if serverName is not specified)
edu.yale.its.tp.cas.client.filter.serverName: 主机名:端口号
edu.yale.its.tp.cas.client.filter.authorizedProxy: 身份验证代理,用空格隔开.必须有一个. (缺省,只接受ST,不接受PT)
edu.yale.its.tp.cas.client.filter.proxyCallbackUrl: 本地代理回调监听器,用来接受PGT/PGTIOU. (可选)
edu.yale.its.tp.cas.client.filter.renew: 是否重新登录参数(缺省为false)
edu.yale.its.tp.cas.client.filter.gateway: 是否使用网关......虽然英文不难明白,但的确不懂到底有什么用,因而不敢断章取义,望哪位高手能补充一下,在此谢过
edu.yale.its.tp.cas.client.filter.wrapRequest: 封装了HttpServletRequest,重载了getRemoteUser()方法. 如果设置为"true", request.getRemoteUser()将返回登录用户名.(可选的,缺省为false.)
8.2.总体结构图

8.3.client源代码研读

8.4.server源代码研读

Yale CAS最佳实践 第四部分:试试身手

1.测试类
我写了一个RequestInfoServlet,可以显示HttpRequest的绝大部分有用信息,经过sso后,可以查看 HttpRequest里面还有什么东西。呵呵,我喜欢这样干。

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @author Liujj Date 2006-12-19 <br>
 *         Description: <br>
 *         RequestHeadersServlet
 */
public class RequestInfoServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		//header
		Enumeration headerNames = request.getHeaderNames();
		out.println("<B>*********[1].begin print headerNames************</B><p>");
		while (headerNames.hasMoreElements()) {
			String name = (String) headerNames.nextElement();
			String value = request.getHeader(name);
			out.println(name + " = " + value + "<p>");
		}
		out.println("<B>*********[1].end print headerNames************</B><p><p>");
		
		//attributeNames
		out.println("<B>*********[2].begin print attributeNames************</B><p>");
		Enumeration attributeNames = request.getAttributeNames();
		while (attributeNames.hasMoreElements()) {
			String name = (String) attributeNames.nextElement();
			String value = (String)request.getAttribute(name);
			out.println(name + " = " + value + "<p>");
		}
		out.println("<B>*********[2].end print attributeNames************</B><p><p>");
		
		//parameterNames
		out.println("<B>*********[3].begin print parameterNames************</B><p>");
		Enumeration parameterNames = request.getParameterNames();
		while (parameterNames.hasMoreElements()) {
			String name = (String) parameterNames.nextElement();
			String value = request.getParameter(name);
			out.println(name + " = " + value + "<p>");
		}
		out.println("<B>*********[3].end print parameterNames************</B><p><p>");
		
		//session
		out.println("<B>*********[4].begin print session AttributeNames************</B><p>");
		HttpSession session = request.getSession();
		Enumeration sAttributeNames = session.getAttributeNames();
		while (sAttributeNames.hasMoreElements()) {
			String name = (String) sAttributeNames.nextElement();
			Object value = session.getAttribute(name);
			out.println(name + " = " + value + "<p>");
		}
		out.println("<B>*********[4].end print session attributeNames************</B><p><p>");
		
		//cookie
		out.println("<B>*********[4].begin print Cookie************</B><p><p>");
		Cookie[] cookie = request.getCookies();
		for(int i=0; i< cookie.length; i++) {
			String name = cookie[i].getName();
			String value = cookie[i].getValue();
			out.println(name + " = " + value + "<p>");
		}   
		out.println("<B>*********[4].end print Cookie************</B><p><p>");
		
	}

}

2.cas log不起作用
目前那个log4j不起作用,天天用着的,算是个小bug,哪个兄弟下载后能帮忙解决一下,再次谢过^_^

你可能感兴趣的:(tomcat,应用服务器,weblogic,Security,SSO)