﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-allen-文章分类-sso</title><link>http://www.blogjava.net/zhouzhou163/category/16974.html</link><description>深圳,专注于java ee技术,包括struts,jsf,webwork,spring,hibernate,ibatis</description><language>zh-cn</language><lastBuildDate>Tue, 27 Feb 2007 12:33:09 GMT</lastBuildDate><pubDate>Tue, 27 Feb 2007 12:33:09 GMT</pubDate><ttl>60</ttl><item><title>SSO学习有CAS SSO配置</title><link>http://www.blogjava.net/zhouzhou163/articles/79625.html</link><dc:creator>allenzhou</dc:creator><author>allenzhou</author><pubDate>Tue, 07 Nov 2006 07:37:00 GMT</pubDate><guid>http://www.blogjava.net/zhouzhou163/articles/79625.html</guid><wfw:comment>http://www.blogjava.net/zhouzhou163/comments/79625.html</wfw:comment><comments>http://www.blogjava.net/zhouzhou163/articles/79625.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/zhouzhou163/comments/commentRss/79625.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/zhouzhou163/services/trackbacks/79625.html</trackback:ping><description><![CDATA[ CAS SSO为耶鲁大学开发的一个开源的SSO(单点登录系统),下载地址为: 
<div><a href="http://www.ja-sig.org/products/cas/"><font color="#0000ff">http://www.ja-sig.org/products/cas/</font></a></div><div>目前SSO应用较为广泛，IBM,BEA都有自己商业方案，一般如有Portal，都会应用SSO.</div><div>Sun成立了OpenSSO.，在进行SSO的开发。</div><div>.net主要有passport方案</div><div>另有一个java开源的JOSSO，不过网上评价不高，</div><div>CAS目前讨论得比较多的地方是BEA广州UserGroup，地址为:</div><div><a href="http://dev2dev.bea.com.cn/bbs/forum.jspa?forumID=29304&amp;start=0"><font color="#0938f7">http://dev2dev.bea.com.cn/bbs/forum.jspa?forumID=29304&amp;start=0</font></a></div><div>版主为David，java安全信息的专家，对cas有很深的研究，他的blog为</div><div><a href="/security"><font color="#0033ff">www.blogjava.net/security</font></a></div><div><a href="/openssl"><font color="#0033ff">http://www.blogjava.net/openssl</font></a></div><div><a href="http://security.javaeye.com/"><font color="#0033ff">http://security.javaeye.com/</font></a></div><div><font color="#0033ff">当然SSO也可以自己编写，关键是多</font>个应用如何共享用户信息及数据安全，以及如何跨语言，跨域等.</div><div>可以参考fins的一篇文章(<a href="http://fins.javaeye.com/blog/31947"><font color="#0000ff">http://fins.javaeye.com/blog/31947</font></a>)和</div><div>王昱的一文(<a href="http://biaoming.spaces.live.com/blog/cns!905abeb7a7abc122!118.entry"><font color="#0000ff">http://biaoming.spaces.live.com/blog/cns!905abeb7a7abc122!118.entry</font></a>)</div><div>以上都是基于java的实现.</div><div>.net中的自己编写实现有<a href="http://www.asp121.com/wlbc/23/430.shtml"><font color="#0033ff">http://www.asp121.com/wlbc/23/430.shtml</font></a></div><div> </div><div>CAS只提供一个简单的身分认证，认证方式很简单，只要用户名和密码相同，即通过，如果应用数据库验证，还需要自己编写。授权和权限没有提供，留给子系统去做。</div><div>CAS demo中的asp例子，可能不大完善，主要原因可以是，在tomcat中建立了和casserver的信任，但在IIS还没有。需要在IIS中建立证书，加入SSL.如需要更好的应用需要多了解SSL和PKI，及SSL在CasServer和CasClient之间ticket的交换.</div><div>如果应用CAS，还需要做的是，如何将yale的登录模块，定制成自己应用的Login模块.</div><div> </div><div><strong><font size="4">在tomcat中配置CAS过程如下:</font></strong></div><div> 1:建立证书</div><div>keytool -genkey -alias tomcat -keyalg RSA  -keystore tomcat.keystore </div><div>在输入用户名时，如果是本机请输入localhost，否则输入域名</div><div> </div><div>2:导入证书</div><div>     keytool -export -file myserver.cert -alias tomcat ?keystore tomcat.keystore</div><div> </div><div>3:导入到JVM中</div><div>     keytool -import -keystore d:\jdk\jre\lib\security\cacerts(根据jdk的安装位置输入) -file myserver.cert -alias tomcat</div><div>以上操作最好放在tomcat的home目录下建立，需要熟悉jdk的命令 keytool</div><div> </div><div>开放SSL 8443端口</div><div>编辑tomcat的配置文件server.xml，去掉下面SSL Connector的注释,修改为如下：</div><div><font color="#0033ff">&lt;Connector port="8443" </font></div><div><font color="#0033ff">               maxThreads="150" minSpareThreads="25" maxSpareThreads="75"</font></div><div><font color="#0033ff">               enableLookups="false" disableUploadTimeout="true"</font></div><div><font color="#0033ff">               acceptCount="100" debug="0" scheme="https" secure="true"</font></div><div><font color="#0033ff">               clientAuth="false" sslProtocol="TLS" &gt;</font></div><div><font color="#0033ff">                        &lt;Factory className="org.apache.coyote.tomcat5.CoyoteServerSocketFactory"</font></div><div><font color="#0033ff">                keystoreFile="jama.keystore"</font></div><div><font color="#0033ff">               keystorePass="xx" clientAuth="false" protocol="TLS" /&gt;</font></div><div><font color="#0033ff">&lt;/Connector&gt;</font></div><div>keystorePass为建立证书的密码</div><div>keystoreFile为建立证书的文件</div><div> </div><div>5.将CAS server3.0.2中target目录中的CAS.war复制到%tomcat_home%\webapps目录下.</div><div>（或者\cas-server-2.0.12\lib目录中的CAS.war也可以）</div><div> </div><div>6.将cas-client-java-2.1.1\dist\casclient.jar文件复制到%tomcat_home%\webapps\servlets-examples\WEB-INF\lib中(没有lib文件夹，自己建一个)</div><div> </div><div>修改tomcat自带的servlet-examples的web.xml, 加入cas的过滤器:</div><div> </div><div>&lt;filter&gt; </div><div> </div><div>    &lt;filter-name&gt;CASFilter&lt;/filter-name&gt;</div><div> </div><div>    &lt;filter-class&gt;edu.yale.its.tp.cas.client.filter.CASFilter&lt;/filter-class&gt;</div><div> </div><div>    &lt;init-param&gt;</div><div> </div><div>        &lt;param-name&gt;edu.yale.its.tp.cas.client.filter.loginUrl&lt;/param-name&gt;</div><div> </div><div>        &lt;param-value&gt;https://localhost:8443/cas/login&lt;/param-value&gt;</div><div> </div><div>    &lt;/init-param&gt;</div><div> </div><div>    &lt;init-param&gt;</div><div> </div><div>        &lt;param-name&gt;edu.yale.its.tp.cas.client.filter.validateUrl&lt;/param-name&gt;</div><div> </div><div>        &lt;param-value&gt;https://localhost:8443/cas/proxyValidate&lt;/param-value&gt;</div><div> </div><div>    &lt;/init-param&gt;</div><div> </div><div>    &lt;init-param&gt;</div><div> </div><div>        <font color="#3300ff">&lt;param-name&gt;edu.yale.its.tp.cas.client.filter.serverName&lt;/param-name&gt;</font></div><div><font color="#3300ff"></font> </div><div><font color="#3300ff">        &lt;param-value&gt;localhost:8080&lt;/param-value&gt;</font></div><div>&lt;!―localhost:8080为自己的服务器名</div><div>    &lt;/init-param&gt;</div><div> </div><div>&lt;/filter&gt;</div><div> </div><div>&lt;filter-mapping&gt;</div><div> </div><div>    &lt;filter-name&gt;CASFilter&lt;/filter-name&gt;</div><div> </div><div>    &lt;url-pattern&gt;/*&lt;/url-pattern&gt;</div><div> </div><div>&lt;/filter-mapping&gt;</div><div> </div><div>6.启动tomcat !,CAS.war文件被自动在webapps下释放出CAS目录</div><div> </div><div>进入http://localhost:8080/servlets-examples, 被自动转发到CAS的登陆页面.</div><div> </div><div>输入相同的用户名和密码，之后跳转回原来页面</div><div>注意：</div><div> </div><div>在制作一个自签名的credential了, 在生成keystore文件的时候密码是:changeit(这是tomcat默认的),你的名字一定要是:localhost,当然这是你需要把CAS client和CAS server放在同一台机器上进行测试用的</div><div> </div><div> </div><div>在浏览器-工具-internet选项里导入myserver.cert后就不会出现安全警报</div><div><font color="#ff0000" size="4">IIS中配置</font></div><div><font color="#ff0000" size="4">将asp demo的cas.asp copy到某一虚拟目录下.本例中建立了一个虚拟目录test</font></div><div><font color="#ff0000" size="4">修改cas.asp内容，见红色内容</font></div><div>&lt;%@ Language=JScript %&gt;</div><div>&lt;%</div><div>// Sample ASP code that uses CAS</div><div>// By Howard Gilbert</div><div> </div><div>// If you logon, it says "Hello " followed by your userid</div><div>// For the Web server to talk to the CAS server, this code depends on the</div><div>// Microsoft ServerXMLHTTP control provided with MSXML. If the MS XML</div><div>// parser is not already installed on the IIS host machine, </div><div>// download version 3.0 SP1 or better from http://www.microsoft.com/xml</div><div> </div><div>// Insert name of CAS Server at your location</div><div>//var CAS_Server = "https://secure.its.yale.edu/cas/servlet/";</div><div><font color="#ff0000"><strong>var CAS_Server = "https://localhost:8443/cas/";  --cas验证服务器地址</strong></font></div><div> </div><div>// Insert public name of IIS Server hosting this script</div><div>// Note: Request.ServerVariables("SERVER_NAME") or anything based on</div><div>// the HTTP "Host" header should NOT be used; this header is supplied by</div><div>// the client and isn't trusted. (--SB)</div><div><font color="#ff0000"><strong>var MyServer = "http://192.168.0.11/test/";  //此处为虚拟目录路径</strong></font></div><div> </div><div>              var http = Server.CreateObject("MSXML2.ServerXMLHTTP.4.0");</div><div>              <font color="#f73809"><strong>var url =CAS_Server+"validate?ticket="+ticket+"&amp;"+</strong></font></div><div><font color="#f73809"><strong>                     "service="+MyServer+"HelloCas/default.asp";  //认证通过后转向的页面</strong></font></div><div><font color="#f73809"><strong>//这里转向HelloCas/default.asp 所以需要在test目录中建立HelloCas目录和default.asp</strong></font></div><div>              http.open("GET",url,false); // HTTP transaction to CAS server</div><div>              http.send();</div><div>              </div><div>              var resp=http.responseText.split('\n'); // Lines become array members</div><div>              if (resp[0]=="yes")   // Logon successful</div><div>                     greeting=resp[1]; // get userid for message</div><div>              Session.Contents("Netid")=resp[1];      // Save for subsequent calls</div><div>       }</div><div>}</div><div>%&gt;</div><div>&lt;HTML&gt;</div><div>&lt;HEAD&gt;&lt;title&gt;CAS ASP Example application&lt;/title&gt;&lt;/HEAD&gt;</div><div>&lt;BODY&gt;</div><div>&lt;P&gt;Hello &lt;%=greeting%&gt;&lt;/P&gt;</div><div>&lt;/BODY&gt;</div><div>&lt;/HTML&gt;</div><div> </div><div><strong><font size="4">Asp.net中调用,</font></strong></div><div><strong><font size="4">建立CASP.cs文件，内容如下.</font></strong></div><div><strong><font size="4">在其它处调用这个文件.</font></strong></div><div>/** </div><div>  CASP.cs </div><div>CAS over ASP.NET! </div><div>  * Created by John Tantalo, john.tantalo@case.edu </div><div> * Case Western Reserve University </div><div>  *  </div><div>  * Modification History: </div><div> *  </div><div>  * 12/09/05 jnt5, created class </div><div>* 12/12/05 jnt5, removed cookie check </div><div>  * stores CASNetworkID in session instead of cache </div><div> * clears Page session variable after ticket verification </div><div>  * 12/13/05 jnt5, removed Page session variable </div><div>  *  fixed bug which would cause loop due to incorrect service parameter </div><div>  * 04/04/06 jnt5, adapted serviceURL code courtesy Ali Cakmak </div><div> * 04/10/06 jnt5, added new comments </div><div> *  </div><div>  * References: </div><div>  *  </div><div> * http://wiki.case.edu/Central_Authentication_Service </div><div>  * https://clearinghouse.ja-sig.org/wiki/display/CAS/CAS+2.0+Protocol+Specification </div><div>  */ </div><div>//以上为正式文件</div><div> </div><div> using System ; </div><div> using System.Web.UI ; </div><div> using System.Net ; </div><div>using System.IO ; </div><div> using System.Web.SessionState; </div><div> </div><div><font style="BACKGROUND-COLOR: #b39e4d"> /</font><font style="BACKGROUND-COLOR: #f0ecdb">**    调用方式</font></div><div><font style="BACKGROUND-COLOR: #f0ecdb" color="#3300ff"><strong>  * CASP general usage: 使用方法　</strong></font></div><div><font style="BACKGROUND-COLOR: #f0ecdb" color="#3300ff"><strong>  *  </strong></font></div><div><font style="BACKGROUND-COLOR: #f0ecdb" color="#3300ff"><strong>  *      private void Page_Load(object sender, System.EventArgs e) </strong></font></div><div><font style="BACKGROUND-COLOR: #f0ecdb" color="#3300ff"><strong>  *      { </strong></font></div><div><font style="BACKGROUND-COLOR: #f0ecdb" color="#3300ff"><strong>  *            String NetworkID = CASP.Authenticate( "https://login.case.edu/cas/login", "https://login.case.edu/cas/validate", this ) ; </strong></font></div><div><font style="BACKGROUND-COLOR: #f0ecdb"><strong><font color="#3300ff"> *      }</font></strong></font></div><div><font style="BACKGROUND-COLOR: #f0ecdb"><strong> */</strong></font></div><div>         </div><div>public class CASP </div><div> { </div><div>     /** </div><div>      * Authenticates a user with the given login and validation pages. After authentication </div><div>         * the user's browser is redirected to the original page. </div><div>     */ </div><div>         </div><div>        public static String Authenticate( String LoginURL, String ValidateURL, Page Page ) </div><div>         { </div><div>                return Authenticate( LoginURL, ValidateURL, Page, Page.Request.Url.AbsoluteUri.Split('?')[0] ) ; </div><div>        } </div><div> </div><div>        /** </div><div>         * Authenticates a user with the given login and validation pages. After authentication </div><div>         * the user's browser is redirected to the location given as the service URL. </div><div>          */ </div><div>         public static String Authenticate( String LoginURL, String ValidateURL, Page Page, String ServiceURL ) </div><div>        { </div><div>                if( Page.Session["CASNetworkID"] != null ) // user already logged in </div><div>                        return Page.Session["CASNetworkID"].ToString() ; </div><div>               else // user hasn't logged in </div><div>              { </div><div>                       if( Page.Request.QueryString["ticket"] != null ) // ticket received </div><div>                       { </div><div>                              try // read ticket and request validation </div><div>                              { </div><div>                                        StreamReader Reader = new StreamReader( new WebClient().OpenRead( ValidateURL + "?ticket=" + Page.Request.QueryString["ticket"] + "&amp;service=" + ServiceURL ) ) ; </div><div>                                                                      if( "yes".Equals( Reader.ReadLine() ) ) // ticket validated </div><div>                                       { </div><div>                                               // store network id in sesssion, return value </div><div> </div><div>                                                return (String) ( Page.Session["CASNetworkID"] = Reader.ReadLine() ) ; </div><div> </div><div>                                        } </div><div>                              }  </div><div>                               catch( WebException ) {} </div><div>                       }  </div><div>         </div><div>                         // ticket was invalid, or didn't exist, so request ticket </div><div>                 </div><div>                        Page.Response.Redirect( LoginURL + "?service=" + ServiceURL, true ) ; </div><div>                        return null ; </div><div>                 } </div><div>         } </div><div> }</div><div> </div><img src ="http://www.blogjava.net/zhouzhou163/aggbug/79625.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/zhouzhou163/" target="_blank">allenzhou</a> 2006-11-07 15:37 <a href="http://www.blogjava.net/zhouzhou163/articles/79625.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>