由于多个项目需要帐号的互通,所以一开始就是用cas去做的,不得不说cas要配置的东西挺多的,但是项目安全性不需要太高,所以没有做https的请求,也就是没有弄证书,这虽然省了很多时间和精力,但是项目之间的安全性降低了不少。
- <bean id="ticketGrantingTicketCookieGenerator" class="org.jasig.cas.web.support.CookieRetrievingCookieGenerator"
- p:cookieSecure="false"
- p:cookieMaxAge="-1"
- p:cookieName="CASTGC"
- p:cookiePath="/cas" />
- <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>
- <bean id="passwordEncoder" class="org.jasig.cas.authentication.handler.DefaultPasswordEncoder" autowire="byName">
- <constructor-arg value="MD5"/>
- </bean>
- <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>
然后就是java客户端的配置
- <!-- 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) -->
- /**
- * 该过滤器用户从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/ 进行登录验证, 验证通过后, 又会跳转回应用的网址。
新建php工程:Phpcasclient1,将CAS文件夹和CAS.php复制到工程中,修改CAS/client.php,将其中的https改为http,将docs/examples/example_simple.php
复制到工程中,修改如下:
- <?php
- //
- // phpCAS simple client
- //
- // import phpCAS lib
- include_once('CAS.php');
- phpCAS::setDebug();
- // initialize phpCAS
- phpCAS::client(CAS_VERSION_2_0,'192.168.18.8',8080,'cas');
- // no SSL validation for the CAS server
- phpCAS::setNoCasServerValidation();
- // force CAS authentication
- phpCAS::forceAuthentication();
- // at this step, the user has been authenticated by the CAS server
- // and the user's login name can be read with phpCAS::getUser().
- // logout if desired
- if (isset($_REQUEST['logout'])) {
- $param=array("service"=>"http://localhost/Phpcasclient1/example_simple.php");//退出登录后返回
- phpCAS::logout($param);
- }
- // for this test, simply print that the authentication was successfull
- ?>
- <html>
- <head>
- <title>phpCAS simple client</title>
- </head>
- <body>
- <h1>Successfull Authentication!这是客户端1</h1>
- <p>the user's login is <b><?php echo phpCAS::getUser(); ?></b>.</p>
- <p>phpCAS version is <b><?php echo phpCAS::getVersion(); ?></b>.</p>
- <p><a href="http://192.168.18.8:8989/Casclient1/index.jsp">去java客户端1</a></p>
- <p><a href="?logout=">退出</a></p>
- </body>
- </html>
php配置需要开启php_curl,可以复制Phpcasclient1为Phpcasclient2
访问:http://localhost/Phpcasclient1/example_simple.php,跳转到登录页面,登录成功后访问Phpcasclient2,不需要登录,
php单点登录成功,这时再访问java客户端发现也不需要登录,php和java应用之间单点登录成功。
注:php的phpCAS::client(CAS_VERSION_2_0,'192.168.18.8',8080,'cas');地址需要和java的web.xml中的cas服务器地址一致,我开始一个写的ip:192.168.18.8,一个写的localhost,
php和java总是不能同步登录,郁闷了好久
----------------到这里java和php的客户端已经配置完成,现在你会发现php和java之间不能单点登出,php端退出java客户端也退出,反之java退出但是php却没有同步退出
这里需要做一个配置,在
phpCAS::setNoCasServerValidation();
// force CAS authentication
phpCAS::forceAuthentication();
这里加上
phpCAS::setNoCasServerValidation();
// force CAS authentication
phpCAS::handleLogoutRequests(); 这里会检测服务器端java退出的通知,就能实现php和java间同步登出了。
phpCAS::forceAuthentication();