1,首先下载cas-server.war 包
2,新建web工程,将war包内的配置文件拷贝进工程里面,测试可以正常启动
3,找到deployerConfigContext.xml配置文件中的authenticationManager bean,
map标签下留下
<entry key-ref="primaryAuthenticationHandler" value-ref="primaryPrincipalResolver" />
<bean id="primaryAuthenticationHandler" class="com.cgd.cas.handler.MyAuthenticationHandler"> <property name="dataSource" ref="dataSource"/> <property name="sql" value="SELECT u.user_id as userId,u.password , u.del_flag as delFlag, u.LOGINNAME as salt ,d.role_id as roleId FROM sys_org_user as u LEFT JOIN sys_auth_distribute as d ON u.user_id=d.user_id AND (d.role_id=1354 or d.role_id=1) WHERE u.LOGINNAME=? and u.dis_flag=0 and (u.effect_date is null or u.effect_date <=date(NOW())) and (u.expiry_date is null or u.expiry_date >=date(NOW()))"/> </bean>
可选注入DataSource和sql,
MyAuthenticationHandler 要继承自 AbstractJdbcUsernamePasswordAuthenticationHandler
protected final HandlerResult createHandlerResult(Credential credential, Principal principal, List<Message> warnings)
{
return new HandlerResult(this, new BasicCredentialMetaData(credential), principal, warnings);
}
@Override protected HandlerResult authenticateUsernamePasswordInternal(UsernamePasswordCredential credential) throws GeneralSecurityException,PreventedException { final String username = credential.getUsername(); final String encryptedPassword = this.getPasswordEncoder().encode(credential.getPassword()); final String dbPassword = "admin"; if (!dbPassword.equals(credential.getPassword())) { throw new CustomUsernameOrPassswordErrorException(); } return createHandlerResult(credential, new SimplePrincipal(username), null); }
4,也可以留下
<entry key-ref="dbAuthHandler" value-ref="primaryPrincipalResolver" />
<!-- 指定c3p0数据源 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="com.mysql.jdbc.Driver" /> <property name="jdbcUrl" value="jdbc:mysql://192.168.164.129:3306/member?useUnicode=true&characterEncoding=UTF-8" /> <property name="user" value="root" /> <property name="password" value="root" /> </bean> <!-- 访问本地数据库 --> <bean id="dbAuthHandler" class="org.jasig.cas.adaptors.jdbc.QueryDatabaseAuthenticationHandler" p:dataSource-ref="dataSource" p:sql="SELECT u.`password` FROM `sys_org_user` u WHERE u.`loginname` = ?" />
如此配置