修改cas-server(三),自定义登录验证方式。

在修改cas-server(二)中,虽然修改了jdbc,但是还是只能判断用户名/密码。

如果需要多增加一个参数,如systemid,则需要修改cas-server-core.jar的源码。


参考:http://blog.csdn.net/lovesummerforever/article/details/38023385


1.修改login-webflow.xml

  
              
              
         
        

2.修改casLoginView.jsp

增加系统id






3.修改cas-server-core.jar的源代码。

修改UsernamePasswordCredentials.java

/* 
 * Copyright 2007 The JA-SIG Collaborative. All rights reserved. See license 
 * distributed with this file and available online at 
 * http://www.ja-sig.org/products/cas/overview/license/ 
 */  
package org.jasig.cas.authentication.principal;  
  
import javax.validation.constraints.NotNull;  
import javax.validation.constraints.Size;  
  
/** 
 * UsernamePasswordCredentials respresents the username and password that a user 
 * may provide in order to prove the authenticity of who they say they are. 
 *  
 * @author Scott Battaglia 
 * @version $Revision: 1.2 $ $Date: 2007/01/22 20:35:26 $ 
 * @since 3.0 
 * 

* This is a published and supported CAS Server 3 API. *

*/ public class UsernamePasswordCredentials implements Credentials { /** Unique ID for serialization. */ private static final long serialVersionUID = -8343864967200862794L; /** The username. */ @NotNull @Size(min=1,message = "required.username") private String username; /** The password. */ @NotNull @Size(min=1, message = "required.password") private String password; /** The systemId for xxx2.0 for sql validate xx add 2014��7��21��16:12:51. */ @NotNull @Size(min=1, message = "required.systemId") private String systemId; /*systemId begin*/ /** * @return Returns the systemId. */ public String getSystemId() { return systemId; } public void setSystemId(String systemId) { this.systemId = systemId; } public String toStringSystemId() { return "[systemId: " + this.systemId + "]"; } /*end */ /** * @return Returns the password. */ public final String getPassword() { return this.password; } /** * @param password The password to set. */ public final void setPassword(final String password) { this.password = password; } /** * @return Returns the userName. */ public final String getUsername() { return this.username; } /** * @param userName The userName to set. */ public final void setUsername(final String userName) { this.username = userName; } public String toString() { return "[username: " + this.username + "]"; } @Override public boolean equals(final Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; UsernamePasswordCredentials that = (UsernamePasswordCredentials) o; if (password != null ? !password.equals(that.password) : that.password != null) return false; if (username != null ? !username.equals(that.username) : that.username != null) return false; return true; } @Override public int hashCode() { int result = username != null ? username.hashCode() : 0; result = 31 * result + (password != null ? password.hashCode() : 0); return result; } }

4.修改上一篇文章的RsCasDaoAuthenticationHandler.java

package org.jasig.cas.authentication.handler;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.sql.DataSource;

import org.jasig.cas.authentication.handler.support.AbstractUsernamePasswordAuthenticationHandler;
import org.jasig.cas.authentication.handler.util.Crypt;
import org.jasig.cas.authentication.principal.UsernamePasswordCredentials;

/**
 * ClassName:RsCasDaoAuthenticationHandler 
* Function: TODO ADD FUNCTION.
* Reason: TODO ADD REASON.
* Date: 2013-4-25 下午04:20:35
* * @author Administrator * @version * @since JDK 1.5 * @see */ public final class RsCasDaoAuthenticationHandler extends AbstractUsernamePasswordAuthenticationHandler { private DataSource dataSource; // 构造方法 public RsCasDaoAuthenticationHandler() { } @Override protected boolean authenticateUsernamePasswordInternal( UsernamePasswordCredentials credentials) throws AuthenticationException { // 标志位 Boolean bool = false; String username = credentials.getUsername(); String password = credentials.getPassword(); String systemid = credentials.getSystemId(); // 取得MD5加密后的字符串 password = new Crypt().encode(password); System.out.println("开始CAS认证方式 RsCasDaoAuthenticationHandler......"); System.out.println("userName:" + username); System.out.println("password:" + password); // 连接数据库 Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; String user = "root"; String pwd = "123456"; String url = "jdbc:mysql://localhost:3306/frj-cas?useUnicode=true&characterEncoding=UTF-8"; try { try { Class.forName("com.mysql.jdbc.Driver"); } catch(ClassNotFoundException e) { e.printStackTrace(); } conn = dataSource.getConnection(); //conn = DriverManager.getConnection(url, user, pwd); String sql = "select count(*) from user2 where username='" + username + "' and password='" + password + "' and systemid='"+systemid; ps = conn.prepareStatement(sql); rs = ps.executeQuery(); if (rs != null && rs.next()) { int i = rs.getInt(1); if (i > 0) { // 只要有对应的一条记录通过,就返回true bool = true; } } } catch(SQLException sql) { sql.printStackTrace(); } finally { try { if (rs != null) { rs.close(); } if (ps != null) { ps.close(); } if (conn != null) { conn.close(); } } catch(SQLException e) { e.printStackTrace(); } } return bool; } public DataSource getDataSource() { return dataSource; } public void setDataSource(DataSource dataSource) { this.dataSource = dataSource; } }


你可能感兴趣的:(cas)