本文借鉴单点登录CAS系列第04节
首先我们要将cas导入到myeclipse中,具体方法(自己创建项目,gradle转),我用的第一个方法,创建web项目后,将编译后的文件拷贝到项目中,目录一定要正确哦,
如图:
1、CSA
的默认登录用户密码配置在deployerConfigContext.xml
,所以就到deployerConfigContext.xml里面找
可以找到<bean id="primaryAuthenticationHandler" class="org.jasig...AcceptUsersAuthenticationHandler">
我们在AcceptUsersAuthenticationHandler.java中发现CAS是把配置的用户密码读取到全局Map<String, String>
中的
2、而AcceptUsersAuthenticationHandler.java是通过继承AbstractUsernamePasswordAuthenticationHandler.java才实现的认证
所以创建com.jadyer.sso.authentication.UserAuthenticationHandler extends AbstractUsernamePasswordAuthenticationHandler
再重写authenticateUsernamePasswordInternal()方法,在里面获取到前台页面输入的用户密码,再到数据库中校验就行了。
3、接下来创建\WEB-INF\spring-configuration\applicationContext-datasource.xml
它会在启动时被自动加载(web.xml中设定的)
然后在里面配置数据库连接池,连接池的用户名密码等可以配置在\WEB-INF\cas.properties
同时增加<context:component-scan base-package="com.jadyer.sso"/>
,使得可以在自定义类中应用Spring注解
4、新建一个UserDaoJdbc.java类,通过它利用SpringJDBCTemplate访问数据库
因为要连接数据库,所以还要把druid jar包以及mysql-connector-java jar包加入到lib目录中
5、最后记得deployerConfigContext.xml
里面把这段Bean配置给注释掉<bean id="primaryAuthenticationHandler">
并在自定义的UserAuthenticationHandler.java
中使用@Component(value="primaryAuthenticationHandler")
声明其为Bean
注意其名字应该是primaryAuthenticationHandler,因为deployerConfigContext.xml的其它配置引用了primaryAuthenticationHandler
否则你还要找到引用了primaryAuthenticationHandler的位置修改为新的Bean
下面我们来具体的执行:
1、认证类UserAuthenticationHandler.java
- package authentication;
- import java.security.GeneralSecurityException;
- import javax.annotation.Resource;
- import javax.security.auth.login.FailedLoginException;
- import org.jasig.cas.authentication.HandlerResult;
- import org.jasig.cas.authentication.PreventedException;
- import org.jasig.cas.authentication.UsernamePasswordCredential;
- import org.jasig.cas.authentication.handler.support.AbstractUsernamePasswordAuthenticationHandler;
- import org.jasig.cas.authentication.principal.SimplePrincipal;
- import org.springframework.stereotype.Component;
-
- /**
- * 自定义的用户登录认证类
- */
- @Component(value="primaryAuthenticationHandler")
- public class UserAuthenticationHandler extends AbstractUsernamePasswordAuthenticationHandler {
- @Resource
- private UserDaoJdbc userDaoJdbc;
-
- @Override
- protected HandlerResult authenticateUsernamePasswordInternal(UsernamePasswordCredential transformedCredential) throws GeneralSecurityException, PreventedException {
- //UsernamePasswordCredential参数包含了前台页面输入的用户信息
- String username = transformedCredential.getUsername();
- String password = transformedCredential.getPassword();
- //认证用户名和密码是否正确
- if(userDaoJdbc.verifyAccount(username, password)){
- return createHandlerResult(transformedCredential, new SimplePrincipal(username), null);
- }
- throw new FailedLoginException();
- }
- }
2、密码校验类UserDaoJdbc.java,为了符合jeesite中的认证方式,将jeesite的解密方法拿过来
- package authentication;
- import javax.annotation.Resource;
- import javax.sql.DataSource;
- import org.springframework.dao.EmptyResultDataAccessException;
- import org.springframework.jdbc.core.JdbcTemplate;
- import org.springframework.stereotype.Repository;
-
-
- @Repository
- public class UserDaoJdbc {
- private static final String SQL_VERIFY_ACCOUNT = "SELECT COUNT(*) FROM sys_user WHERE login_name=? AND del_flag=0";
- private static final String SQL_VERIFY_PASSWORD = "SELECT password FROM sys_user WHERE login_name=? AND del_flag=0";
- private JdbcTemplate jdbcTemplate;
- public static final int HASH_INTERATIONS = 1024;
- @Resource
- public void setDataSource(DataSource dataSource){
- this.jdbcTemplate = new JdbcTemplate(dataSource);
- }
- public boolean verifyAccount(String username, String plainPassword){
- try{
- //验证用户名和密码是否正确
- if(1==this.jdbcTemplate.queryForObject(SQL_VERIFY_ACCOUNT, new Object[]{username}, Integer.class)){
- String password =this.jdbcTemplate.queryForObject(SQL_VERIFY_PASSWORD, new Object[]{username},String.class);
- String plain = Encodes.unescapeHtml(plainPassword);
- byte[] salt = Encodes.decodeHex(password.substring(0,16));
- byte[] hashPassword = Digests.sha1(plain.getBytes(), salt, HASH_INTERATIONS);
- return password.equals(Encodes.encodeHex(salt)+Encodes.encodeHex(hashPassword));
- }
- return false;
-
- }catch(EmptyResultDataAccessException e){
- return false;
- }
- }
- }
3、链接数据库的配置文件spring-configuration\applicationContext-datasource.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
- <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
- <property name="url" value="jdbc:mysql://"/>
- <property name="username" value=""/>
- <property name="password" value=""/>
-
- <property name="initialSize" value="1"/>
- <property name="minIdle" value="1"/>
- <property name="maxActive" value="20"/>
-
- <property name="maxWait" value="60000"/>
-
- <property name="timeBetweenEvictionRunsMillis" value="60000"/>
-
- <property name="minEvictableIdleTimeMillis" value="300000"/>
- <property name="validationQuery" value="SELECT 'x'"/>
- <property name="testWhileIdle" value="true"/>
- <property name="testOnBorrow" value="false"/>
- <property name="testOnReturn" value="false"/>
-
-
- <property name="poolPreparedStatements" value="false"/>
- <property name="maxPoolPreparedStatementPerConnectionSize" value="-1"/>
-
- <property name="filters" value="wall,mergeStat"/>
- </bean>
- <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
- <property name="dataSource" ref="dataSource"/>
- </bean>
- <tx:annotation-driven transaction-manager="txManager"/>
-
- <context:component-scan base-package="authentication"/>
- </beans>
4、修改cas的认证方法。新添信息,以前的可以注释掉
- <bean id="authenticationManager" class="org.jasig.cas.authentication.PolicyBasedAuthenticationManager">
- <constructor-arg>
- <map>
- <entry key-ref="proxyAuthenticationHandler" value-ref="proxyPrincipalResolver" />
- <entry key-ref="primaryAuthenticationHandler" value-ref="primaryPrincipalResolver" />
-
-
-
-
- </map>
- </constructor-arg>
- <property name="authenticationPolicy">
- <bean class="org.jasig.cas.authentication.AnyAuthenticationPolicy" />
- </property>
- </bean>
- <!-- <bean id="primaryAuthenticationHandler"
- class="org.jasig.cas.authentication.AcceptUsersAuthenticationHandler">
- <property name="users">
- <map>
- <entry key="casuser" value="Mellon"/>
- </map>
- </property>
- </bean> -->
至此完毕,重启系统,使用项目中的用户登录。