CAS(5)数据库验证用户之自定义密码加密

1、自定义加密

自定义密码加密类,实现org.jasig.cas.authentication.handler.PasswordEncoder接口。例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class MyPasswordEncoder implements PasswordEncoder {

private static String salt="hello world";

public String encode(String password) {
String result="";
String newPW=password+salt;
try {
MessageDigest md=MessageDigest.getInstance("MD5");
BASE64Encoder base64en=new BASE64Encoder();
result=base64en.encode(md.digest(newPW.getBytes()));
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return result;
}
}

2、修改配置文件

修改Cas Server下的cas项目的配置文件cas/WEB-INF/deployerConfigContext.xml:

2.1、

删除以下配置,该配置为Cas Server默认的账号密码:

1
2
3
4
5
6
7
<bean id="primaryAuthenticationHandler" class="org.jasig.cas.authentication.AcceptUsersAuthenticationHandler">
<property name="users">
<map>
<entry key="casuser" value="Mellon"/>
</map>
</property>
</bean>

2.2、

添加以下配置(分别为MySQL所在服务器ip、MySQL账号、密码、自定义密码加密类,检索Cas密码的SQL语句):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
p:driverClass="com.mysql.jdbc.Driver"
p:jdbcUrl="jdbc:mysql://192.168.100.6:3336/cas?useUnicode=true&amp;characterEncoding=UTF-8&amp;zeroDateTimeBehavior=convertToNull"
p:user="root"
p:password="123456"
p:initialPoolSize="6"
p:minPoolSize="6"
p:maxPoolSize="18"
p:maxIdleTimeExcessConnections="120"
p:checkoutTimeout="10000"
p:acquireIncrement="6"
p:acquireRetryAttempts="5"
p:acquireRetryDelay="2000"
p:idleConnectionTestPeriod="30"
p:preferredTestQuery="select 1" />

<bean id="myPasswordEncoder" class="自定义密码加密类" />

<bean id="dbAuthHandler" class="org.jasig.cas.adaptors.jdbc.QueryDatabaseAuthenticationHandler"
p:dataSource-ref="dataSource"
p:sql="select password from user where username=?"
p:passwordEncoder-ref=" myPasswordEncoder " />

2.3、

修改以下配置(黄色部分为第二步中粉色部分):

1
2
3
4
5
6
7
8
9
<bean id="authenticationManager" class="org.jasig.cas.authentication.PolicyBasedAuthenticationManager">
<constructor-arg>
<map>
<entry key-ref="proxyAuthenticationHandler" value-ref="proxyPrincipalResolver" />
<entry key-ref="dbAuthHandler" value-ref="primaryPrincipalResolver" />
</map>
</constructor-arg>
...
</bean>

2.4、

通过使用自定义加密方法向MySQL插入账号密码;

2.5、

启动tomcat。输入Cas Server地址:http://127.0.0.1:8080/cas,输入MySQL中的账号密码,登录成功则说明配置成功。

>