CAS自定义Credentials登录

先看了这篇文章http://www.blogjava.net/junky/archive/2007/08/20/138136.html,但是这个的版本是3.1的,而最新的是3.5的,差别还是有一点的,网上找了很多资料,也看了一点CAS server的源代码,终于搞定,因为想用idea开发,结果环境不熟悉,蛋疼,虽然现在Eclipse越来越慢,也只好将就着用,废话不说,代码说话,首先定义自己的Credentials

public class NbrcCredentials implements Credentials {
private static final long serialVersionUID = 2053021031579470710L;

private String idtype;

private String username;

private String password;

//getter and setter...

}

然后修改登录页面,在password后面增加一个选项,这里直接用中文会乱码,需要到资源文件里去定义,为了方便我直接这么写了

<spring:message code="screen.welcome.label.password.accesskey" var="passwordAccessKey" />
<form:password cssClass="required" cssErrorClass="error" id="password" size="25" tabindex="2" path="password" accesskey="${passwordAccessKey}" htmlEscape="true" autocomplete="off" />
<br/>
User TYPE<select name="idtype" id="idtype">
<option value="1">ADMIN</option>
<option value="2">OPER</option>
</select>

找到/WEB-INF/下的login-webflow.xml文件,需要修改两个地方,第一把原来的Credentials注释,然后定义自己的

<!-- <var name="credentials" class="org.jasig.cas.authentication.principal.UsernamePasswordCredentials" /> -->
<var name="credentials" class="com.nbrc.sso.cas.principal.NbrcCredentials"/>

然后找到


<view-state id="viewLoginForm" view="casLoginView" model="credentials">
<binder>
<binding property="username" />
<binding property="password" />
<binding property="idtype"/> <!--增加这一行 -->
</binder>
...
</view-state>


这样数据就能绑定了,还要自定义一个处理登录过程的类

package com.nbrc.cas.handler.support;

import com.nbrc.sso.cas.principal.NbrcCredentials;
import org.jasig.cas.authentication.handler.*;
import org.jasig.cas.authentication.principal.Credentials;
import org.springframework.dao.IncorrectResultSizeDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;

public class NbrcAuthenticationHandler implements AuthenticationHandler {
private static final Class<NbrcCredentials> DEFAULT_CLASS = NbrcCredentials.class;

private PasswordEncoder passwordEncoder = new PlainTextPasswordEncoder();
private JdbcTemplate jdbcTemplate;

private DataSource dataSource;

private PrincipalNameTransformer principalNameTransformer = new NoOpPrincipalNameTransformer();
/** Class that this instance will support. */
private Class<?> classToSupport = DEFAULT_CLASS;
private boolean supportSubClasses = true;
@Override
public boolean authenticate(Credentials credentials) throws AuthenticationException {
final NbrcCredentials nc = (NbrcCredentials) credentials;
final String username = getPrincipalNameTransformer().transform(nc.getUsername());
final String password = nc.getPassword();
final String encryptedPassword = this.getPasswordEncoder().encode(
password);

if("1".equals(nc.getIdtype())){
try {
final String sql = "select `password` from t_admin_user where login_name=?";
final String dbPassword = getJdbcTemplate().queryForObject(
sql
, String.class, username);
return dbPassword.equals(encryptedPassword);
} catch (final IncorrectResultSizeDataAccessException e) {
// this means the username was not found.
return false;
}
}

if("2".equals(nc.getIdtype())){
try {
final String sql = "select `password` from t_oper_user where login_name=?";
final String dbPassword = getJdbcTemplate().queryForObject(
sql
, String.class, username);
return dbPassword.equals(encryptedPassword);
} catch (final IncorrectResultSizeDataAccessException e) {
// this means the username was not found.
return false;
}
}
return false; //To change body of implemented methods use File | Settings | File Templates.
}

@Override
public boolean supports(Credentials credentials) {
return credentials != null
&& (this.classToSupport.equals(credentials.getClass()) || (this.classToSupport
.isAssignableFrom(credentials.getClass()))
&& this.supportSubClasses);
}

public PasswordEncoder getPasswordEncoder() {
return passwordEncoder;
}

public void setPasswordEncoder(PasswordEncoder passwordEncoder) {
this.passwordEncoder = passwordEncoder;
}

public PrincipalNameTransformer getPrincipalNameTransformer() {
return principalNameTransformer;
}

public void setPrincipalNameTransformer(PrincipalNameTransformer principalNameTransformer) {
this.principalNameTransformer = principalNameTransformer;
}

public final void setDataSource(final DataSource dataSource) {
this.jdbcTemplate = new JdbcTemplate(dataSource);
this.dataSource = dataSource;
}

/**
* Method to return the jdbcTemplate
*
* @return a fully created JdbcTemplate.
*/
protected final JdbcTemplate getJdbcTemplate() {
return this.jdbcTemplate;
}

protected final DataSource getDataSource() {
return this.dataSource;
}
}


最后把处理类注册到/WEB-INF/deployerConfigContext.xml文件中,替换原有的AuthenticationHandler

<!--<bean class="org.jasig.cas.adaptors.jdbc.QueryDatabaseAuthenticationHandler">
<property name="dataSource" ref="dataSource"></property>
<property name="sql" value="select password from t_admin_user where login_name=?"></property>
<property name="passwordEncoder" ref="MD5PasswordEncoder"></property>
</bean>-->
<bean class="com.nbrc.cas.handler.support.NbrcAuthenticationHandler" >
<property name="dataSource" ref="dataSource"></property>
</bean>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值