java學(xué)習(xí)

          java學(xué)習(xí)

           

          java實(shí)現(xiàn)單點(diǎn)登錄的兩種方式

          1.如果兩個(gè)網(wǎng)站域名的一級(jí)域名相同,可以使用cookiefilter實(shí)現(xiàn)單點(diǎn)登錄,因?yàn)榫W(wǎng)站有可能(具體看cookie的設(shè)置)可以共享cookie。例如:www.bbs.aa.cn    www.news.aa.cn

          第一個(gè)網(wǎng)站在登錄后,把用戶信息寫到cookie中,當(dāng)訪問第二個(gè)網(wǎng)站時(shí),第二個(gè)網(wǎng)站先經(jīng)過自己的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.如果兩個(gè)網(wǎng)站域名的一級(jí)域名不同,不可以使用cookiefilter實(shí)現(xiàn)單點(diǎn)登錄,因?yàn)榫W(wǎng)站不可以共享cookie。例如:www.bbs.cn    www.news.cn

          使用cas框架服務(wù)實(shí)現(xiàn)單點(diǎn)登錄。1.部署cas服務(wù)端。2.在服務(wù)器端的ticketGrantingTicketCookieGenerator.xml中修改文件。<bean id="ticketGrantingTicketCookieGenerator" class="org.jasig.cas.web.support.CookieRetrievingCookieGenerator"

                             p:cookieSecure="false"http://使用http協(xié)議

                             p:cookieMaxAge="-1"http://cookie有效時(shí)間

                             p:cookieName="yjwname"http://cookie名稱

                             p:cookiePath="/" />//項(xiàng)目名稱

          3.部署www.bbs.cn    www.news.cn服務(wù),在每個(gè)客戶端項(xiàng)目中加入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服務(wù)器地址-->

                                      <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下的資源都需要驗(yàn)證登錄-->

                             <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.啟動(dòng)服務(wù),這樣就可以實(shí)現(xiàn)單點(diǎn)登錄

          posted on 2016-06-05 11:17 楊軍威 閱讀(5954) 評(píng)論(0)  編輯  收藏


          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           

          導(dǎo)航

          統(tǒng)計(jì)

          常用鏈接

          留言簿

          隨筆檔案

          搜索

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 葵青区| 临泉县| 德格县| 沙湾县| 西宁市| 旬阳县| 瑞丽市| 莱芜市| 基隆市| 蒙阴县| 洛川县| 井陉县| 霞浦县| 靖安县| 泾阳县| 友谊县| 新宁县| 罗田县| 永善县| 墨脱县| 谢通门县| 中卫市| 兴业县| 博白县| 大庆市| 吉木萨尔县| 新建县| 中卫市| 漳州市| 阿坝| 萨迦县| 新泰市| 阿拉善右旗| 花莲市| 娄底市| 阿克陶县| 阳西县| 汾阳市| 蓬莱市| 凤冈县| 曲松县|