java单点登录系统CAS的简单使用
转:http://blog.csdn.net/yunye114105/article/details/7997041
背景
使用步骤
第一步 部署CAS系统服务端
- <bean id="ticketGrantingTicketCookieGenerator" class="org.jasig.cas.web.support.CookieRetrievingCookieGenerator"
- p:cookieSecure="false"
- p:cookieMaxAge="-1"
- p:cookieName="CASTGC"
- p:cookiePath="/cas" />
3.配置登录的验证逻辑, 修改配置文件cas\WEB-INF\deployerConfigContext.xml。在authenticationHandlers中配置验证方式,我这里配置数据库查询语句来实现用户名和密码的验证。
- <property name="authenticationHandlers">
- <list>
- <!--
- | This is the authentication handler that authenticates services by means of callback via SSL, thereby validating
- | a server side SSL certificate.
- +-->
- <bean class="org.jasig.cas.authentication.handler.support.HttpBasedServiceCredentialsAuthenticationHandler"
- p:httpClient-ref="httpClient" />
- <!--
- | This is the authentication handler declaration that every CAS deployer will need to change before deploying CAS
- | into production. The default SimpleTestUsernamePasswordAuthenticationHandler authenticates UsernamePasswordCredentials
- | where the username equals the password. You will need to replace this with an AuthenticationHandler that implements your
- | local authentication strategy. You might accomplish this by coding a new such handler and declaring
- | edu.someschool.its.cas.MySpecialHandler here, or you might use one of the handlers provided in the adaptors modules.
- +-->
- <!-- <bean
- class="org.jasig.cas.authentication.handler.support.SimpleTestUsernamePasswordAuthenticationHandler" /> -->
- <bean class="org.jasig.cas.adaptors.jdbc.QueryDatabaseAuthenticationHandler">
- <property name="sql" value="select password from userTable where userName=?" />
- <property name="passwordEncoder" ref="passwordEncoder"/>
- <property name="dataSource" ref="dataSource" />
- </bean>
- </list>
- </property>
密码加密方法我这里使用MD5, 配置passwordEncoder的bean
- <bean id="passwordEncoder" class="org.jasig.cas.authentication.handler.DefaultPasswordEncoder" autowire="byName">
- <constructor-arg value="MD5"/>
- </bean>
在配置一个名称为dataSource的数据源
- <bean id="dataSource" class="org.logicalcobwebs.proxool.ProxoolDataSource">
- <property name="driver" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"></property>
- <property name="driverUrl" value="jdbc:sqlserver://localhost:1433;databaseName=testDB;"></property>
- <property name="user" value="sa"></property>
- <property name="password" value="123456"></property>
- <property name="maximumConnectionCount" value="100"></property>
- <property name="minimumConnectionCount" value="1"></property>
- </bean>
数据源的配置根据自己的实际情况来配置, 需要的jar如果lib下面没有,自己复制进去, 不然数据源连不上报错。
第二步 客户端web应用程序集成CAS
- <!-- CAS 单点登录(SSO) 过滤器配置 (start) -->
- <!-- 该过滤器用于实现单点登出功能。-->
- <filter>
- <filter-name>CAS Single Sign Out Filter</filter-name>
- <filter-class>org.jasig.cas.client.session.SingleSignOutFilter</filter-class>
- </filter>
- <filter-mapping>
- <filter-name>CAS Single Sign Out Filter</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- <!-- CAS: 用于单点退出 -->
- <listener>
- <listener-class>org.jasig.cas.client.session.SingleSignOutHttpSessionListener</listener-class>
- </listener>
- <!-- 该过滤器负责用户的认证工作,必须启用它 -->
- <filter>
- <filter-name>CASFilter</filter-name>
- <filter-class>org.jasig.cas.client.authentication.AuthenticationFilter</filter-class>
- <init-param>
- <param-name>casServerLoginUrl</param-name>
- <!-- 下面的URL是Cas服务器的登录地址 -->
- <param-value>http://CAS服务端所在服务器IP:8080/cas/login</param-value>
- </init-param>
- <init-param>
- <param-name>serverName</param-name>
- <!-- 下面的URL是具体某一个应用的访问地址 -->
- <param-value>http://具体web应用程序所在服务器IP:8080</param-value>
- </init-param>
- </filter>
- <filter-mapping>
- <filter-name>CASFilter</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- <!-- 该过滤器负责对Ticket的校验工作,必须启用它 -->
- <filter>
- <filter-name>CAS Validation Filter</filter-name>
- <filter-class>org.jasig.cas.client.validation.Cas20ProxyReceivingTicketValidationFilter</filter-class>
- <init-param>
- <param-name>casServerUrlPrefix</param-name>
- <!-- 下面的URL是Cas服务器的认证地址 -->
- <param-value>http://CAS服务端所在服务器IP:8080/cas</param-value>
- </init-param>
- <init-param>
- <param-name>serverName</param-name>
- <!-- 下面的URL是具体某一个应用的访问地址 -->
- <param-value>http://具体web应用程序所在服务器IP:8080</param-value>
- </init-param>
- <init-param>
- <param-name>renew</param-name>
- <param-value>false</param-value>
- </init-param>
- <init-param>
- <param-name>gateway</param-name>
- <param-value>false</param-value>
- </init-param>
- </filter>
- <filter-mapping>
- <filter-name>CAS Validation Filter</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- <!--
- 该过滤器负责实现HttpServletRequest请求的包裹,
- 比如允许开发者通过HttpServletRequest的getRemoteUser()方法获得SSO登录用户的登录名,可选配置。
- -->
- <filter>
- <filter-name>CAS HttpServletRequest Wrapper Filter</filter-name>
- <filter-class>org.jasig.cas.client.util.HttpServletRequestWrapperFilter</filter-class>
- </filter>
- <filter-mapping>
- <filter-name>CAS HttpServletRequest Wrapper Filter</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- <!--
- 该过滤器使得开发者可以通过org.jasig.cas.client.util.AssertionHolder来获取用户的登录名。
- 比如AssertionHolder.getAssertion().getPrincipal().getName()。
- -->
- <filter>
- <filter-name>CAS Assertion Thread Local Filter</filter-name>
- <filter-class>org.jasig.cas.client.util.AssertionThreadLocalFilter</filter-class>
- </filter>
- <filter-mapping>
- <filter-name>CAS Assertion Thread Local Filter</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- <!-- 自动根据单点登录的结果设置本系统的用户信息(具体某一个应用实现) -->
- <filter>
- <filter-name>CasForInvokeContextFilter</filter-name>
- <filter-class>com.cm.demo.filter.CasForInvokeContextFilter</filter-class>
- <init-param>
- <param-name>appId</param-name>
- <param-value>a5ea611bbff7474a81753697a1714fb0</param-value>
- </init-param>
- </filter>
- <filter-mapping>
- <filter-name>CasForInvokeContextFilter</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- <!-- CAS 单点登录(SSO) 过滤器配置 (end) -->
4. 注意上步配置文件中,过滤器CasForInvokeContextFilter的实现是需要在具体的应用中实现的,他的目的是, CAS服务端登录验证成功后,会将登录用户的用户名携带回来, 这时客户端web应用程序需要根据用户名从数据库用户表中查询到用户的Id等信息, 并填充到Session中, 这样,客户端应用程序原来的验证逻辑就不会出问题了, 因为我们一般都是通过验证session中是否含有当前登录的用户的ID来进行登录验证的。
- /**
- * 该过滤器用户从CAS认证服务器中获取登录用户用户名,并填充必要的Session.
- * @author jiarong_cheng
- * @created 2012-7-12
- */
- public class CasForInvokeContextFilter implements Filter {
- @Override
- public void destroy() {
- }
- @Override
- public void doFilter(ServletRequest request, ServletResponse response,
- FilterChain chain) throws IOException, ServletException {
- HttpSession session = ((HttpServletRequest) request).getSession();
- //如果session中没有用户信息,则填充用户信息
- if (session.getAttribute("j_userId") == null) {
- //从Cas服务器获取登录账户的用户名
- Assertion assertion = AssertionHolder.getAssertion();
- String userName = assertion.getPrincipal().getName();
- try {
- //根据单点登录的账户的用户名,从数据库用户表查找用户信息, 填充到session中
- User user = UserDao.getUserByName(userName);
- session.setAttribute("username", userName);
- session.setAttribute("userId", user.getId());
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- chain.doFilter(request, response);
- }
- @Override
- public void init(FilterConfig config) throws ServletException {
- }
- }
到此,就完成了, 当你访问具体应用的网址, 如http://具体应用IP: 8080/ ,就会跳转到CAS服务器的登陆页面: http://CAS服务器IP: 8080/ 进行登录验证, 验证通过后, 又会跳转回应用的网址。
第三步 单点登出
http://CAS服务器IP:8080/cas/logout
java单点登录系统CAS的简单使用的更多相关文章
- 单点登录系统CAS筹建及取得更多用户信息的实现
国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...
- 开源单点登录系统CAS入门
一.什么是CAS CAS 是 Yale 大学发起的一个开源项目,旨在为 Web 应用系统提供一种可靠的单点登录方法,CAS 在 2004 年 12 月正式成为 JA-SIG 的一个项目.CAS 具有以 ...
- 【Tech】单点登录系统CAS客户端demo
服务器端配置请参考: http://www.cnblogs.com/sunshineatnoon/p/4064632.html 工具:myeclipse或者javaee-eclipse 1.启动jav ...
- 【Tech】单点登录系统CAS服务器端搭建及实现用户名密码由MYSQL数据库验证
CAS是YALE大学发起的一个开源项目,旨在为web应用系统提供一种可靠的单点登录方法.它主要分为client和server端,server端负责对用户的认证工作,client端负责处理对客户端受保护 ...
- 单点登录系统cas资料汇总
http://jasig.github.io/cas/4.0.x/index.html 主页 https://jasigcas.herokuapp.com ...
- CAS单点登录系统入门--分布式登录验证
1.开源单点登录系统CAS入门 1.1 什么是单点登录 单点登录(Single Sign On),简称为 SSO,是目前比较流行的企业业务整合的解决方案之一.SSO的定义是在多个应用系统中,用户只需要 ...
- CAS实现的单点登录系统
单点登录(single sign on ),简称SSO. 纯属学习用,对来自网络的部分如果侵害了您的权力,请联系我.QQ:262800095 SSO的定义是在多个应用系统中,用户只需要登录一次就可以访 ...
- 单点登录系统(SSO)之CAS(中央认证服务)
SSO(Single Sign On)单点登录系统,是在多个系统中值要求只要登录一次,就可以用已登录的身份访问多个系统,去掉了每次访问不同的系统都要重新登录的弊端. CAS(中央/集中认证服务):Th ...
- CAS单点登录系统整合——注册的问题
最近一段时间在搞CAS单点登录系统,涉及到几个子系统的整合问题.对于注册,这里遇到了一个选择: 在子系统内完成注册,然后把信息同步到CAS系统: 在CAS系统中完成基本信息的注册,比如:用户名.邮箱. ...
随机推荐
- MySQL基本配置
>>添加环境变量 把MySQL Server的bin目录添加到系统path中. >>MySQL启动和停止命令 net start mysql56 net stop mysql5 ...
- 【Java】集合_Collections_学习记录
一.Collections工具类概述 1.为List.Set.Map等集合提供大量方法对集合元素进行排序.查询和修改等操作. 2.将集合对象设置为不可变. 3.对集合对象实现同步控制等. 二.排序操作 ...
- 小程序原理,生成SQL SERVER 2008 数据库所有表的结构文档
作者:wide288 , 日期:2013-7-31 以前开发中,用 MYSQL 数据库,有个小程序 生成数据库结构文档.很方便,做为开发组的文档很有用. 现在开发中用到了 SQL SERVER 200 ...
- [RxJS] Marble diagrams in ASCII form
There are many operators available, and in order to understand them we need to have a simple way of ...
- LINUX 中的 TCP/IP协议 参数详解
Ipsysctl tutorial 1.0.4 Prev Chapter 3. IPv4 variable reference Next https://www.frozentux.net/ipsys ...
- Emoji表情处理
//php对于 Emoji表情的处理 //当接收内容需要转换时: //preg_replace_callback('/[\xf0-\xf7].{3}/','cal_fun', $str) functi ...
- eclipse 常见问题及解决
1. Target runtime Apache Tomcat v6.0 is not defined.错误解决方法 原文:http://blog.csdn.net/xw13106209/articl ...
- [转] json in javascript
JavaScript is a general purpose programming language that was introduced as the page scripting langu ...
- [转] JavaScript 原型理解与创建对象应用
这段时间把之前的 JavaScript 的笔记复习了一遍,又学习了一些新的内容,所以把自己的学习笔记加上个人理解在这里总结一下,并提供一个简单的应用示例,希望能帮助一些刚入门的朋友.主 要参考< ...
- 你的网站为什么会慢?——用YSlow为你的网站提速
在前面的文章我翻译的文章中分别从内容.服务器.JavaScript和CSS.图片.Coockies和移动应用等几个方面总结了34条提高网站性能的黄金守则,但是这些守则中,有一些是不常用到的,若非有实力 ...