CAS框架单点登录,自定义验证登录方式(添加系统标识)

来源:互联网 发布:菩提老祖就是如来知乎 编辑:程序博客网 时间:2024/05/17 21:59

 应需求的变化,在登录cas的时候,默认根据用户名和密码进行验证,如果加上用户名,密码和一个系统标识进行验证呢?该如何做呢?

      我们知道cas默认的登录界面中,输入的用户名和密码,再配置一下deployerConfigContext.xml 这个文件中的bean  org.jasig.cas.adaptors.jdbc.QueryDatabaseAuthenticationHandler 的这个标签,写上对应的sql,以及在<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">中配置数据库驱动,数据库名称,以及登陆密码等。

      如果再加上一个其他的验证该怎么做呢?

      1  根据xml中bean标签的提示,很容器找到这个类QueryDatabaseAuthenticationHandler.java类,首先先修改login-webflow.xml,修改代码如下所示:

  1. <binder>  
  2.             <binding property="username" />  
  3.             <binding property="password" />  
  4.        <binding property="systemId" />  
  5.         </binder>  


      其中<bingding property="systemId" />与界面中传递过来的隐含域一致。

 

      2   casLoginView.jsp中增加的js代码如下所示,从登陆地址的url传递参数。

  1. <script language="javascript"  type="text/javascript">   
  2.     window.onload=function()//用window的onload事件,窗体加载完毕的时候  
  3. {  
  4.    //do something  
  5.    var result = location.search.match(new RegExp("[\?\&]" + 'systemId'"=([^\&]+)","i"));    
  6.     if(result == null || result.length < 1){    
  7.    
  8.      result ="";  
  9.     }   
  10.       
  11.           
  12.     $("#systemId")[0].value=result[1];  
  13. }  
  14.       
  15. </script>  


      参登陆页面地址为https://xxx.xxx.com:8443/cas/login?systemId=xxx2.0 ,在第一次登陆界面的时候会携带这两个参数https://xxx.xxx.com:8443/cas/login?service=http%3A%2F%2F172.xx.3.101%3A8080%2Fxxx2.0%2Fuser%2FtoMain%2F 其中的一个为我们的自定义的系统标识,第二个为cas验证数据库成功后转到的主界面。

      3   在登录界面中加上了hidden,以此来传递给CAS。

<input type="hidden" name="systemId" id="systemId">

 

      4  修改CAS源代码,UsernamePasswordCredentials.java,代码如下所示。

  1. /* 
  2.  * Copyright 2007 The JA-SIG Collaborative. All rights reserved. See license 
  3.  * distributed with this file and available online at 
  4.  * http://www.ja-sig.org/products/cas/overview/license/ 
  5.  */  
  6. package org.jasig.cas.authentication.principal;  
  7.   
  8. import javax.validation.constraints.NotNull;  
  9. import javax.validation.constraints.Size;  
  10.   
  11. /** 
  12.  * UsernamePasswordCredentials respresents the username and password that a user 
  13.  * may provide in order to prove the authenticity of who they say they are. 
  14.  *  
  15.  * @author Scott Battaglia 
  16.  * @version $Revision: 1.2 $ $Date: 2007/01/22 20:35:26 $ 
  17.  * @since 3.0 
  18.  * <p> 
  19.  * This is a published and supported CAS Server 3 API. 
  20.  * </p> 
  21.  */  
  22. public class UsernamePasswordCredentials implements Credentials {  
  23.   
  24.     /** Unique ID for serialization. */  
  25.     private static final long serialVersionUID = -8343864967200862794L;  
  26.   
  27.     /** The username. */  
  28.     @NotNull  
  29.     @Size(min=1,message = "required.username")  
  30.     private String username;  
  31.   
  32.     /** The password. */  
  33.     @NotNull  
  34.     @Size(min=1, message = "required.password")  
  35.     private String password;  
  36.   
  37.     /** The systemId for xxx2.0 for sql validate xx add 2014��7��21��16:12:51. */  
  38.     @NotNull  
  39.     @Size(min=1, message = "required.systemId")  
  40.     private String systemId;  
  41.     /*systemId  begin*/  
  42.   
  43.     /** 
  44.      * @return Returns the systemId. 
  45.      */  
  46.      
  47.     public String getSystemId() {  
  48.         return systemId;  
  49.     }  
  50.   
  51.     public void setSystemId(String systemId) {  
  52.         this.systemId = systemId;  
  53.     }  
  54.   
  55.      public String toStringSystemId() {  
  56.         return "[systemId: " + this.systemId + "]";  
  57.     }  
  58.   
  59.     /*end */  
  60.   
  61.   
  62.     /** 
  63.      * @return Returns the password. 
  64.      */  
  65.     public final String getPassword() {  
  66.         return this.password;  
  67.     }  
  68.   
  69.     /** 
  70.      * @param password The password to set. 
  71.      */  
  72.     public final void setPassword(final String password) {  
  73.         this.password = password;  
  74.     }  
  75.   
  76.     /** 
  77.      * @return Returns the userName. 
  78.      */  
  79.     public final String getUsername() {  
  80.         return this.username;  
  81.     }  
  82.   
  83.     /** 
  84.      * @param userName The userName to set. 
  85.      */  
  86.     public final void setUsername(final String userName) {  
  87.         this.username = userName;  
  88.     }  
  89.   
  90.     public String toString() {  
  91.         return "[username: " + this.username + "]";  
  92.     }  
  93.   
  94.     @Override  
  95.     public boolean equals(final Object o) {  
  96.         if (this == o) return true;  
  97.         if (o == null || getClass() != o.getClass()) return false;  
  98.   
  99.         UsernamePasswordCredentials that = (UsernamePasswordCredentials) o;  
  100.   
  101.         if (password != null ? !password.equals(that.password) : that.password != nullreturn false;  
  102.         if (username != null ? !username.equals(that.username) : that.username != nullreturn false;  
  103.   
  104.         return true;  
  105.     }  
  106.   
  107.     @Override  
  108.     public int hashCode() {  
  109.         int result = username != null ? username.hashCode() : 0;  
  110.         result = 31 * result + (password != null ? password.hashCode() : 0);  
  111.         return result;  
  112.     }  
  113. }  


      除了cas自己的用户名和密码,添加自己的systemId标识。

      5  修改QueryDatabaseAuthenticationHandler.java类 , 代码如下所示。

  1. /* 
  2.  * Copyright 2007 The JA-SIG Collaborative. All rights reserved. See license 
  3.  * distributed with this file and available online at 
  4.  * http://www.ja-sig.org/products/cas/overview/license/ 
  5.  */  
  6. package org.jasig.cas.adaptors.jdbc;  
  7.   
  8. import org.jasig.cas.authentication.handler.AuthenticationException;  
  9. import org.jasig.cas.authentication.principal.UsernamePasswordCredentials;  
  10. import org.springframework.dao.IncorrectResultSizeDataAccessException;  
  11.   
  12. import javax.validation.constraints.NotNull;  
  13.   
  14. /** 
  15.  * Class that if provided a query that returns a password (parameter of query 
  16.  * must be username) will compare that password to a translated version of the 
  17.  * password provided by the user. If they match, then authentication succeeds. 
  18.  * Default password translator is plaintext translator. 
  19.  *  
  20.  * @author Scott Battaglia 
  21.  * @author Dmitriy Kopylenko 
  22.  * @version $Revision$ $Date$ 
  23.  * @since 3.0 
  24.  */  
  25. public final class QueryDatabaseAuthenticationHandler extends  
  26.     AbstractJdbcUsernamePasswordAuthenticationHandler {  
  27.   
  28.     @NotNull  
  29.     private String sql;  
  30.   
  31.     protected final boolean authenticateUsernamePasswordInternal(final UsernamePasswordCredentials credentials) throws AuthenticationException {  
  32.         final String username = getPrincipalNameTransformer().transform(credentials.getUsername());  
  33.         final String password = credentials.getPassword();  
  34.         //xx add 2014 7 21  16:27:58 for xxx2.0 systemid begin----------  
  35.           //final String systemId = credentials.getSystemId();  
  36.         String mySystemId = credentials.getSystemId();  
  37.         String[] systemIdGroup=mySystemId.split(",");  
  38.         String systemId= systemIdGroup[0];  
  39.           System.out.println("systemId---------"+systemId+"----------------systemid value");  
  40.           //xxadd 2014 7  21   16:27:58 for xxx2.0 systemid end----------  
  41.         final String encryptedPassword = this.getPasswordEncoder().encode(  
  42.             password);  
  43.           
  44.         try {  
  45.             final String dbPassword = getJdbcTemplate().queryForObject(  
  46.                 this.sql, String.class, username,systemId);  
  47.             return dbPassword.equals(encryptedPassword);  
  48.         } catch (final IncorrectResultSizeDataAccessException e) {  
  49.             // this means the username was not found.  
  50.             return false;  
  51.         }  
  52.     }  
  53.   
  54.     /** 
  55.      * @param sql The sql to set. 
  56.      */  
  57.     public void setSql(final String sql) {  
  58.         this.sql = sql;  
  59.     }  
  60. }  


      在这过程中学习:

      部署的项目如何调试:当我无法在自己的本地上附上cas源代码,进行断点调试,就只能根据CAS的日志文件来看到底是哪里出的错误,cas的日志文件一大堆,到底是哪个我需要的日志文件,删了刷新看到底哪个文件变化,这都是我需要学习的。

      面对你认为的庞然大物时:第一次接触CAS陌生,根着文档一步一步的做,中间出现一些错误,再不断的改正错误,从CAS一些基本的样式和功能不符合需求的时候,就需要改动CAS源代码了,总是把他捧的高高在上,总是感觉自己触不可及这都是错误的心态;其实当你打开他的源代码,静下心来研究,也会感觉没有什么,和自己的项目又有什么不同呢?换做是自己要开发一个CAS的项目,是怎样的一个思路?

      对CAS的认识:第一次能弄出登录界面,十分兴奋,到后来不断的发现CAS的缺点,不断的需要改动CAS源代码,不断的要替换他的文件,只能说CAS虽然是开源的,有很多我们学习的地方,但我认为对于最好不使用CAS还是不要使用,他的可配置性,灵活性,可扩展性能不是十分的友好,还是慎重选择吧。


原文网址:http://blog.csdn.net/lovesummerforever/article/details/38023385

但是存在一个问题:

用户成功功能A系统后,即使B系统不准许登陆,但是还是直接登陆。需要按照CasServer添加子系统登陆权限验证进行补充


原创粉丝点击