CAS server 自定义认证方式和返回多个参数
2017-03-30 19:48
239 查看
Mac上搭建cas
http://blog.csdn.net/qq_33264648/article/details/66475964
我们可以去修改它,添加我们自己认证的方式。新建ValidUserQueryAuthenticationHandler类,继承org.jasig.cas.authentication.handler.support.AbstractUsernamePasswordAuthenticationHandler这个类,在authenticateUsernamePasswordInternal这个方法中写自己的验证逻辑,我这里用jdbc的方式认证,首先在pom.xm中添加
在cas-server-support-jdbc这个模块中有4个可以继承的类,在这里我选择继承org.jasig.cas.adaptors.jdbc.AbstractJdbcUsernamePasswordAuthenticationHandler这个类,
然后在deployerConfigContext.xml文件中写上
它原来的认证逻辑要注释掉,换上自己的,这里有一个passwordEncoder,这是密码加密方式,可以不写,也可以自定义,只要实现接口org.jasig.cas.authentication.handler.PasswordEncoder就可以了,dataSource是数据源,自己另外配置。
整个流程就完成了。
StubPersonAttributeDao这个类就是对返回的值的处理,我们可以继承这个类来处理。新建MultipleAttributeUserDao类,继承StubPersonAttributeDao,在deployerConfigContext.xml文件中把上面的代码换成下面的代码。
在MultipleAttributeUserDao这个类中重写public IPersonAttributes getPerson(String uid)这个方法,这个类就是处理返回值的方法,uid就是登陆传的username,类似下面的代码。
http://blog.csdn.net/qq_33264648/article/details/66475964
自定义认证方式
在项目的webapp/WEB-INF文件夹下,有个deployerConfigContext.xml文件,打开,找到这个地方。<bean id="authenticationManager" class="org.jasig.cas.authentication.PolicyBasedAuthenticationManager"> <constructor-arg> <map> <!-- | IMPORTANT | Every handler requires a unique name. | If more than one instance of the same handler class is configured, you must explicitly | set its name to something other than its default name (typically the simple class name). --> <entry key-ref="proxyAuthenticationHandler" value-ref="proxyPrincipalResolver"/> <entry key-ref="primaryAuthenticationHandler" value-ref="primaryPrincipalResolver"/> </map> </constructor-arg> <!-- Uncomment the metadata populator to allow clearpass to capture and cache the password This switch effectively will turn on clearpass. <property name="authenticationMetaDataPopulators"> <util:list> <bean class="org.jasig.cas.extension.clearpass.CacheCredentialsMetaDataPopulator" c:credentialCache-ref="encryptedMap" /> </util:list> </property> --> <!-- | Defines the security policy around authentication. Some alternative policies that ship with CAS: | | * NotPreventedAuthenticationPolicy - all credential must either pass or fail authentication | * AllAuthenticationPolicy - all presented credential must be authenticated successfully | * RequiredHandlerAuthenticationPolicy - specifies a handler that must authenticate its credential to pass --> <property name="authenticationPolicy"> <bean class="org.jasig.cas.authentication.AnyAuthenticationPolicy"/> </property> </bean>
<entry key-ref="primaryAuthenticationHandler" value-ref="primaryPrincipalResolver"/> primaryAuthenticationHandler对应的bean就是cas默认的认证方式
我们可以去修改它,添加我们自己认证的方式。新建ValidUserQueryAuthenticationHandler类,继承org.jasig.cas.authentication.handler.support.AbstractUsernamePasswordAuthenticationHandler这个类,在authenticateUsernamePasswordInternal这个方法中写自己的验证逻辑,我这里用jdbc的方式认证,首先在pom.xm中添加
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>${druid.version}</version> </dependency> <dependency> <groupId>org.jasig.cas</groupId> <artifactId>cas-server-support-jdbc</artifactId> <version>${cas.version}</version> </dependency>
在cas-server-support-jdbc这个模块中有4个可以继承的类,在这里我选择继承org.jasig.cas.adaptors.jdbc.AbstractJdbcUsernamePasswordAuthenticationHandler这个类,
public class ValidUserQueryAuthenticationHandler extends AbstractJdbcUsernamePasswordAuthenticationHandler { private final String sql; public ValidUserQueryAuthenticationHandler() { sql = "SELECT password FROM user where name=?"; } @Override protected final 4000 HandlerResult authenticateUsernamePasswordInternal(final UsernamePasswordCredential credential) throws GeneralSecurityException, PreventedException { String username = credential.getUsername(); String password = credential.getPassword(); try { if (username == null || username.length() == 0) { throw new FailedLoginException("username can't be null"); } String e = this.getJdbcTemplate().queryForObject(this.sql, String.class, username); String encryptedPassword = this.getPasswordEncoder().encode(password); if (!e.equals(encryptedPassword)) { throw new FailedLoginException("Password does not match value on record."); } } catch (IncorrectResultSizeDataAccessException var5) { if (var5.getActualSize() == 0) { throw new AccountNotFoundException(username + " not found with SQL query"); } throw new FailedLoginException("Multiple records found for " + username); } catch (DataAccessException var6) { throw new PreventedException("SQL exception while executing query for " + username, var6); } return this.createHandlerResult(credential, new SimplePrincipal(username), null); } }
然后在deployerConfigContext.xml文件中写上
<!--<bean id="primaryAuthenticationHandler" class="org.jasig.cas.authentication.AcceptUsersAuthenticationHandler"> <property name="users"> <map> <entry key="casuser" value="Mellon"/> </map> </property> </bean>--> <bean id="primaryAuthenticationHandler" class="com.castest.ValidUserQueryAuthenticationHandler" p:dataSource-ref="dataSource" p:passwordEncoder-ref="passwordEncoder"/> <bean id="passwordEncoder" class="org.jasig.cas.authentication.handler.DefaultPasswordEncoder" c:_0="md5"/> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/webtest?useUnicode=true&characterEncoding=utf-8"/> <property name="username" value="root"/> <property name="password" value="123456"/> <property name="validationQuery" value="select 1"/> </bean>
它原来的认证逻辑要注释掉,换上自己的,这里有一个passwordEncoder,这是密码加密方式,可以不写,也可以自定义,只要实现接口org.jasig.cas.authentication.handler.PasswordEncoder就可以了,dataSource是数据源,自己另外配置。
整个流程就完成了。
返回多个参数
在deployerConfigContext.xml文件中找到这段代码<bean id="attributeRepository" class="org.jasig.services.persondir.support.StubPersonAttributeDao" p:backingMap-ref="attrRepoBackingMap"/> <util:map id="attrRepoBackingMap"> <entry key="uid" value="uid"/> <entry key="eduPersonAffiliation" value="eduPersonAffiliation"/> <entry key="groupMembership" value="groupMembership"/> </util:map>
StubPersonAttributeDao这个类就是对返回的值的处理,我们可以继承这个类来处理。新建MultipleAttributeUserDao类,继承StubPersonAttributeDao,在deployerConfigContext.xml文件中把上面的代码换成下面的代码。
<bean id="attributeRepository" class="com.castest.MultipleAttributeUserDao"/>
在MultipleAttributeUserDao这个类中重写public IPersonAttributes getPerson(String uid)这个方法,这个类就是处理返回值的方法,uid就是登陆传的username,类似下面的代码。
@Override public IPersonAttributes getPerson(String uid) { Map<String, List<Object>> attributes = new HashMap<String, List<Object>>(); attributes.put("from_name", Collections.<Object>singletonList(uid)); attributes.put("email", Collections.<Object>singletonList("123456@qq.com")); attributes.put("phone", Collections.<Object>singletonList("123456")); return new AttributeNamedPersonImpl(attributes); }
相关文章推荐
- 以post方式携窗体等参数向服务器发送请求 发出去的字节流 返回的内容
- 添加自定义参数及设置方式
- Django使用自定义认证方式
- MySQL存储过程和自定义函数、Navicat、创建存储过程和函数、调用存储过程和函数、三种方式、In和Out类型参数、
- android开发(26) 和其他应用交换数据方式一,使用intent指定自定义action调用其他程序里的activity,并获得其返回的结果
- XFire实现自定义类型参数传递和调用返回自定义类型方法
- datatable ajax加载自定义的返回参数
- 自定义Struts 返回json的2中方式
- android post方式传递参数并获取返回数据代码
- 如何将自定义的搜索参数便捷的添加到js方式的bootstrap table的参数中
- axis2使用教程、webservices自定义参数,webservices自定义返回值
- Android开发 AIDL使用自定义对象作参数或返回值
- 通过post方式传递json参数,并返回网页源码
- PopupWindow返回参数至Activity两种方式:接口和Rxjava
- android开发(26) 和其他应用交换数据方式一,使用intent指定自定义action调用其他程序里的activity,并获得其返回的结果
- 多参数返回类型_自定义tuple
- asp.net 通过ajax方式调用webmethod方法使用自定义类传参及获取返回参数
- CAS 实现单点登录(SSO)数据库查询认证机制-自定义编码方式(四)
- javaweb中去除某个get方式的参数,并且返回路径
- spring mvc参数复杂混合传递方式,混合返回方式