java学习

java学习

 

java实现单点登录的两种方式

1.如果两个网站域名的一级域名相同,可以使用cookiefilter实现单点登录,因为网站有可能(具体看cookie的设置)可以共享cookie。例如:www.bbs.aa.cn    www.news.aa.cn

第一个网站在登录后,把用户信息写到cookie中,当访问第二个网站时,第二个网站先经过自己的filter,检查session,如果没有,查询cookie,取出用户信息,放在session中登录。

public void doFilter(ServletRequest req, ServletResponse resp,

                            FilterChain chain) throws IOException, ServletException {

 

                   HttpServletRequest request = (HttpServletRequest) req;

                  

                   if(request.getSession().getAttribute("user")== null){

                            Cookie[] cs = request.getCookies();

 

                            if (cs != null && cs.length > 0) {

                                     for (Cookie c : cs) {

                                               String cName = c.getName();

                                               if (cName.equals("sso")) {

                                                        String userName = c.getValue();

                                                        request.getSession().setAttribute("user", userName);

                                               }

                                     }

                            }

                   }

 

                   chain.doFilter(request, resp);

 

         }

2.如果两个网站域名的一级域名不同,不可以使用cookiefilter实现单点登录,因为网站不可以共享cookie。例如:www.bbs.cn    www.news.cn

使用cas框架服务实现单点登录。1.部署cas服务端。2.在服务器端的ticketGrantingTicketCookieGenerator.xml中修改文件。<bean id="ticketGrantingTicketCookieGenerator" class="org.jasig.cas.web.support.CookieRetrievingCookieGenerator"

                   p:cookieSecure="false"//使用http协议

                   p:cookieMaxAge="-1"//cookie有效时间

                   p:cookieName="yjwname"//cookie名称

                   p:cookiePath="/" />//项目名称

3.部署www.bbs.cn    www.news.cn服务,在每个客户端项目中加入casjar包,在web.xml中配置<?xml version="1.0" encoding="UTF-8"?>

<!--

Copyright (c) 2008, Martin W. Kirst

All rights reserved.

 

Redistribution and use in source and binary forms, with or without

modification, are permitted provided that the following conditions are met:

 

* Redistributions of source code must retain the above copyright notice,

 this list of conditions and the following disclaimer.

 

* Redistributions in binary form must reproduce the above copyright

 notice, this list of conditions and the following disclaimer in the

 documentation and/or other materials provided with the distribution.

 

* Neither the name of the Martin W. Kirst nor the names of its

 contributors may be used to endorse or promote products derived from

 this software without specific prior written permission.

 

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS

IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED

TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A

PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER

OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,

EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,

PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR

PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF

LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING

NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS

SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

-->

<web-app id="mywebapp" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

        

         <display-name>mywebapp</display-name>

        

         <description>

                  

                   Simple sample, how to use CAS Java Client 3.x.

                   In this sample exists a public area (/)

                   and a private area (/protected/*).

                 

         </description>

 

<!-- Sign out not yet implemented -->

<!--

         <filter>

                   <filter-name>CAS Single Sign Out Filter</filter-name>

                   <filter-class>org.jasig.cas.client.session.SingleSignOutFilter</filter-class>

         </filter>

-->

 

         <filter>

                   <filter-name>CAS Authentication Filter</filter-name>

                   <filter-class>org.jasig.cas.client.authentication.AuthenticationFilter</filter-class>

                   <init-param>

                            <!--cas服务器地址-->

                            <param-name>casServerLoginUrl</param-name>

                            <param-value>http://www.service.com:8081/login</param-value>

                   </init-param>

                   <init-param>

                            <!--自己的地址-->

                            <param-name>serverName</param-name>

                            <param-value>http://www.bbs.com:8081</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>

                   <filter-name>CAS Validation Filter</filter-name>

                   <filter-class>org.jasig.cas.client.validation.Cas20ProxyReceivingTicketValidationFilter</filter-class>

                   <init-param>

                            <param-name>casServerUrlPrefix</param-name>

                            <param-value>http://www.service.com:8081</param-value>

                   </init-param>

                   <init-param>

                            <param-name>serverName</param-name>

                            <param-value>http://www.bbs.com:8081</param-value>

                   </init-param>

         </filter>

        

         <filter>

                   <filter-name>CAS HttpServletRequest Wrapper Filter</filter-name>

                   <filter-class>org.jasig.cas.client.util.HttpServletRequestWrapperFilter</filter-class>

         </filter>

        

         <filter>

                   <filter-name>CAS Assertion Thread Local Filter</filter-name>

                   <filter-class>org.jasig.cas.client.util.AssertionThreadLocalFilter</filter-class>

         </filter>

 

         <!-- ************************* -->

 

<!-- Sign out not yet implemented -->

<!--

         <filter-mapping>

                   <filter-name>CAS Single Sign Out Filter</filter-name>

                   <url-pattern>/*</url-pattern>

         </filter-mapping>

-->

 

         <filter-mapping>

                   <filter-name>CAS Authentication Filter</filter-name>

                   <!--URL下的资源都需要验证登录-->

                   <url-pattern>/protected/*</url-pattern>

         </filter-mapping>

 

         <filter-mapping>

                   <filter-name>CAS Validation Filter</filter-name>

                   <url-pattern>/*</url-pattern>

         </filter-mapping>

          

         <filter-mapping>

                   <filter-name>CAS HttpServletRequest Wrapper Filter</filter-name>

                   <url-pattern>/*</url-pattern>

         </filter-mapping>

        

         <filter-mapping>

                   <filter-name>CAS Assertion Thread Local Filter</filter-name>

                   <url-pattern>/*</url-pattern>

         </filter-mapping>

        

         <filter-mapping>

                   <filter-name>CAS Validation Filter</filter-name>

                   <url-pattern>/proxyCallback</url-pattern>   

         </filter-mapping>

        

         <!-- *********************** -->

 

<!-- Sign out not yet implemented -->

<!--

         <listener>

                   <listener-class>org.jasig.cas.client.session.SingleSignOutHttpSessionListener</listener-class>

         </listener>

-->

 

        

        

</web-app>

4.启动服务,这样就可以实现单点登录

posted on 2016-06-05 11:17 杨军威 阅读(5949) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航:
 

导航

统计

常用链接

留言簿

随笔档案

搜索

最新评论

阅读排行榜

评论排行榜