当前位置:Gxlcms > 数据库问题 > 修改CAS源码是的基于DB的认证方式配置更灵活

修改CAS源码是的基于DB的认证方式配置更灵活

时间:2021-07-01 10:21:17 帮助过:20人阅读

package org.jasig.cas.adaptors.jdbc; 2 3 import java.security.GeneralSecurityException; 4 5 import org.jasig.cas.authentication.HandlerResult; 6 import org.jasig.cas.authentication.PreventedException; 7 import org.jasig.cas.authentication.UsernamePasswordCredential; 8 import org.jasig.cas.authentication.principal.SimplePrincipal; 9 import org.springframework.dao.DataAccessException; 10 import org.springframework.dao.IncorrectResultSizeDataAccessException; 11 12 import javax.security.auth.login.AccountNotFoundException; 13 import javax.security.auth.login.FailedLoginException; 14 import javax.validation.constraints.NotNull; 15 16 /** 17 * Class that if provided a query that returns a password (parameter of query 18 * must be username) will compare that password to a translated version of the 19 * password provided by the user. If they match, then authentication succeeds. 20 * Default password translator is plaintext translator. 21 * 22 * @author Scott Battaglia 23 * @author Dmitriy Kopylenko 24 * @author Marvin S. Addison 25 * 26 * @since 3.0 27 */ 28 public class TyQueryDatabaseAuthenticationHandler extends AbstractJdbcUsernamePasswordAuthenticationHandler { 29 30 @NotNull 31 private String sql; 32 33 private boolean useDefaultPassword; 34 35 private String defaultPassword; 36 37 /** {@inheritDoc} */ 38 @Override 39 protected final HandlerResult authenticateUsernamePasswordInternal(final UsernamePasswordCredential credential) 40 throws GeneralSecurityException, PreventedException { 41 42 final String username = credential.getUsername(); 43 final String password = useDefaultPassword ? defaultPassword : credential.getPassword(); 44 final String encryptedPassword = this.getPasswordEncoder().encode(password); 45 try { 46 final String dbPassword = getJdbcTemplate().queryForObject(this.sql, String.class, username); 47 if (!dbPassword.equals(encryptedPassword)) { 48 throw new FailedLoginException("Password does not match value on record."); 49 } 50 } catch (final IncorrectResultSizeDataAccessException e) { 51 if (e.getActualSize() == 0) { 52 throw new AccountNotFoundException(username + " not found with SQL query"); 53 } else { 54 throw new FailedLoginException("Multiple records found for " + username); 55 } 56 } catch (final DataAccessException e) { 57 throw new PreventedException("SQL exception while executing query for " + username, e); 58 } 59 return createHandlerResult(credential, new SimplePrincipal(username), null); 60 } 61 62 /** 63 * @param sql The sql to set. 64 */ 65 public void setSql(final String sql) { 66 this.sql = sql; 67 } 68 69 /** 70 * @param isUseDefaultPassword The useDefaultPassword to set. 71 */ 72 public void setUseDefaultPassword(final boolean isUseDefaultPassword) { 73 this.useDefaultPassword = isUseDefaultPassword; 74 } 75 76 /** 77 * @param defaultPassword The defaultPassword to set. 78 */ 79 public void setDefaultPassword(final String defaultPassword) { 80 this.defaultPassword = defaultPassword; 81 } 82 83 }

  第三步:修改你的CAS部署包代码

  解压你的部署包,找到文件deployerConfigContext.xml

  如果你的代码修改代码如下:  

1   <bean id="dbAuthenticationHandler"
2     class="org.jasig.cas.adaptors.jdbc.TyQueryDatabaseAuthenticationHandler">
3       <property name="dataSource" ref="dataSource"></property>
4       <property name="sql" value="select EmpPass as password from SsoAccount where EmpCode=? "></property>
5       <property name="passwordEncoder" ref="passwordEncoder"></property>
6       <property name="useDefaultPassword" value="true"></property>
7       <property name="defaultPassword" value="111111"></property>
8   </bean>

  id=dbAuthenticationHandle的bean表示登录账号以及密码的认证方式处理配置,该配置被id=authenticationManager的bean配置所引用

  配置代码dbAuthenticationHandler中的TyQueryDatabaseAuthenticationHandler是上述自定义实现的代码,新增2属性,userDefaultPassword=true表示服务端验证登录页面提交的密码的时候,t提交的密码不作为实际的密码来源,而是从属性defaultPassword中取值,反之userDefaultPassword=false表示登录页面提交的密码作为服务端认证密码的密码来源。此处的11111模拟的是登录密码输入的密码,因此它是明文的,实际上用户是看不到这个密码的,而数据库里面存的不是111111而是111111经过加密后的密文,在内部进行111111进行验证的时候是需要对111111进行加密的。具体的加密过程是根据您的配置来实现的,本处则以上述第二步提到的方式为参考,实际上加密这块你可自行定制.

  datasSoure的配置本文不予列出

 

  第四步:编译cas-server-support-jdbc

  由于cas本文所采用的版本是基于mvn开发的,实际版本号是4.0.0,因此需要通过mvn口令来编译,实际操作如下:

  打开cas-server-support-jdbc源码所在文件夹,快捷组合ctrl+shift+鼠标右键(如果你左右键是反的请切换为鼠标右键)打开控制台,输入命令 mvn clean compile回车进行编译

  打开便后的目录cas-server-support-jdbc\target\classes\org\jasig\cas\adaptors\jdbc\,在此处你会找到TyQueryDatabaseAuthenticationHandler.class二进制文件

 

  第五步:打包jar文件

  打开cas-server-support-jdbc\target\classes\,快捷组合ctrl+shift+鼠标右键打开控制台,输入口令jar -cvf cas-server-support-jdbc-4.0.0.jar org回车就生成了jar包文件

  包文件名称解释:“cas-server-support-jdbc”表示mvn项目自模块名“-4.0.0”表示你的mvn主项目的版本号,mvn的子模块的版本号应与mvn主项目版本号保持一致

  

  第六步:部署

  复制上述编译出来的cas-server-support-jdbc-4.0.0.jar文件至您的cas部署包下面的lib目录下面,再按照上述第三步修改配置文件.Ok此时修改打包并部署完成。此时重启tomact访问你的网站飞起来可以看看效果了。

 

修改CAS源码是的基于DB的认证方式配置更灵活

标签:back   文件夹   org   throws   div   compile   row   .class   cut   

人气教程排行