单点登录 - CAS【九】CAS支持HTTP协议

我们知道CAS SSO 是基于HTTPS协议的单点登陆,如果要用HTTP协议进行传输,那么就需要修改CAS的相关的配置文件,图了方便,但是安全性大打折扣,对于单点登录,一旦被攻击,那么你的所有属于CAS管理的业务系统都可以被自由访问了。个人并不赞成使用HTTP协议,牺牲一点性能换取更好的安全性是值得的。

 

一、软件环境

  1、cas-client:cas-client-3.2.1-release

  2、cas-server:cas-server-3.5.2-release

 

二、修改步骤

  1、文件warnCookieGenerator.xml

     

Xml代码   收藏代码
  1. <bean id="warnCookieGenerator" class="org.jasig.cas.web.support.CookieRetrievingCookieGenerator"  
  2.         p:cookieSecure="true"  
  3.         p:cookieMaxAge="-1"  
  4.         p:cookieName="CASPRIVACY"  
  5.         p:cookiePath="/cas" />  

 

 

  2、文件ticketGrantingTicketCookieGenerator.xml

    

Xml代码   收藏代码
  1. <bean id="ticketGrantingTicketCookieGenerator" class="org.jasig.cas.web.support.CookieRetrievingCookieGenerator"  
  2.         p:cookieSecure="true"  
  3.         p:cookieMaxAge="-1"  
  4.         p:cookieName="CASTGC"  
  5.         p:cookiePath="/cas" />  

 

 

   将bean中的p:cookieSecure="true "修改为p:cookieSecure="false"

 

  3、文件deployerConfigContext.xml

    

Xml代码   收藏代码
  1. <bean class="org.jasig.cas.authentication.handler.support.HttpBasedServiceCredentialsAuthenticationHandler"  
  2.                     p:httpClient-ref="httpClient" />  

 添加p:requireSecure="false"

 

 

 如果我们使用的是基于Filter在web.xml中的方式,至此使用HTTP协议就可以单点登录了。

 如果我们使用的Java Core Object的方式,那么还需要进行的下面的步骤

   

  4、文件SecureURL.java

  

Java代码   收藏代码
  1. /* 
  2.  *  Copyright (c) 2000-2003 Yale University. All rights reserved. 
  3.  * 
  4.  *  THIS SOFTWARE IS PROVIDED "AS IS," AND ANY EXPRESS OR IMPLIED 
  5.  *  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
  6.  *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE EXPRESSLY 
  7.  *  DISCLAIMED. IN NO EVENT SHALL YALE UNIVERSITY OR ITS EMPLOYEES BE 
  8.  *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
  9.  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED, THE COSTS OF 
  10.  *  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA OR 
  11.  *  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
  12.  *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
  13.  *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
  14.  *  SOFTWARE, EVEN IF ADVISED IN ADVANCE OF THE POSSIBILITY OF SUCH 
  15.  *  DAMAGE. 
  16.  * 
  17.  *  Redistribution and use of this software in source or binary forms, 
  18.  *  with or without modification, are permitted, provided that the 
  19.  *  following conditions are met: 
  20.  * 
  21.  *  1. Any redistribution must include the above copyright notice and 
  22.  *  disclaimer and this list of conditions in any related documentation 
  23.  *  and, if feasible, in the redistributed software. 
  24.  * 
  25.  *  2. Any redistribution must include the acknowledgment, "This product 
  26.  *  includes software developed by Yale University," in any related 
  27.  *  documentation and, if feasible, in the redistributed software. 
  28.  * 
  29.  *  3. The names "Yale" and "Yale University" must not be used to endorse 
  30.  *  or promote products derived from this software. 
  31.  */  
  32.   
  33. package org.jasig.cas.client.corejavaobject.util;  
  34.   
  35. import java.io.BufferedReader;  
  36. import java.io.IOException;  
  37. import java.io.InputStreamReader;  
  38. import java.net.URL;  
  39. import java.net.URLConnection;  
  40.   
  41. import org.apache.commons.logging.Log;  
  42. import org.apache.commons.logging.LogFactory;  
  43.   
  44. /** 
  45.  * A class housing some utility functions exposing secure URL validation 
  46.  * and content retrieval.  The rules are intended to be about as restrictive 
  47.  * as a common browser with respect to server-certificate validation. 
  48.  */  
  49. public class SecureURL {  
  50.   
  51.         private static Log log = LogFactory.getLog(SecureURL.class);  
  52.       
  53.     /** 
  54.      * For testing only... 
  55.      */  
  56.     public static void main(String args[]) throws IOException {  
  57.         System.setProperty(  
  58.             "java.protocol.handler.pkgs",  
  59.             "com.sun.net.ssl.internal.www.protocol");  
  60.         System.out.println(SecureURL.retrieve(args[0]));  
  61.     }  
  62.   
  63.     /**  
  64.      * Retrieve the contents from the given URL as a String, assuming the 
  65.      * URL's server matches what we expect it to match. 
  66.      */  
  67.     public static String retrieve(String url) throws IOException {  
  68.         if (log.isTraceEnabled()){  
  69.             log.trace("entering retrieve(" + url + ")");  
  70.         }  
  71.         BufferedReader r = null;  
  72.         try {  
  73.             URL u = new URL(url);  
  74.             if (!u.getProtocol().equals("https")){  
  75.                 // IOException may not be the best exception we could throw here  
  76.                 // since the problem is with the URL argument we were passed, not  
  77.                 // IO. -awp9  
  78.                 log.error("retrieve(" + url + ") on an illegal URL since protocol was not https.");  
  79.                             throw new IOException("only 'https' URLs are valid for this method");  
  80.             }  
  81.                   
  82.             URLConnection uc = u.openConnection();  
  83.             uc.setRequestProperty("Connection""close");  
  84.             r = new BufferedReader(new InputStreamReader(uc.getInputStream()));  
  85.             String line;  
  86.             StringBuffer buf = new StringBuffer();  
  87.             while ((line = r.readLine()) != null)  
  88.                 buf.append(line + "\n");  
  89.             return buf.toString();  
  90.         } finally {  
  91.             try {  
  92.                 if (r != null)  
  93.                     r.close();  
  94.             } catch (IOException ex) {  
  95.                 // ignore  
  96.             }  
  97.         }  
  98.     }  
  99. }  

 

找到下面的部分

 

Java代码   收藏代码
  1. if (!u.getProtocol().equals("https")){  
  2.                 // IOException may not be the best exception we could throw here  
  3.                 // since the problem is with the URL argument we were passed, not  
  4.                 // IO. -awp9  
  5.                 log.error("retrieve(" + url + ") on an illegal URL since protocol was not https.");  
  6.                             throw new IOException("only 'https' URLs are valid for this method");  
  7.             }  

 相信大家应该明白了吧,只需要将此部分注释掉即可。

 

备注:cookieSecure都修改false,我们来看下其作用是什么?

   Secure是Cookie的一个属性。

   属性值

         如果客户端仅在使用安全超文本传输协议 (HTTPS) 的后继请求中返回 Cookie,则为 true;否则为 false。默认为 false。 

 

实际上,当此属性为 true 时,该 Cookie 只能通过 https:// 请求来发送。即使用http协议是无法传递Cookie的。

猜你喜欢

转载自blog.csdn.net/R1124679390/article/details/80267039