CAS 4.2.1单点登录(六) 登录时添加除了用户名之外的更多属性

数据库的用户表中有一个权限字段roles存放着权限相关的信息,目标是将该字段带到前台的jsp页面以便进行一些权限控制的处理
实现方式是将该字段在密码验证的时候,放在principal的attribute属性之中

1.MyQueryDatabaseAuthenticationHandler类

对照着sql属性新增rolesSql属性(同MD5加盐中的saltSql)

修改authenticateUsernamePasswordInternal方法
在return之前新增代码

//获取权限列表
final String roles = getJdbcTemplate().queryForObject(this.rolesSql, String.class, username);
rolesMap.put("roles",roles );

修改返回值中调用的方法的第二个参数

return createHandlerResult(credential, this.principalFactory.createPrincipal(username,rolesMap), null);
2.在deployerConfigContext.xml中增加rolesSql初始值的注入
<bean id="myQueryDatabaseAuthenticationHandler" class="org.jasig.cas.adaptors.jdbc.MyQueryDatabaseAuthenticationHandler"
          p:dataSource-ref="dataSource"
          p:sql="select password from test_a where name = ?"
          p:saltSql="select salt from test_a where name = ?"
          p:rolesSql="select roles from test_a where name = ?"/>
3.PolicyBasedAuthenticationManager类中的authenticateAndResolvePrincipal方法

注释一部分代码,因为调用resolvePrincipal方法会使用配置文件中注入的attribute覆盖我们设置的attribute,注释部分:

//        if (resolver == null) {
            principal = result.getPrincipal();
//            logger.debug(
//                    "No resolver configured for {}. Falling back to handler principal {}",
//                    handler.getName(),
//                    principal);
//        } else {
//            principal = resolvePrincipal(handler.getName(), resolver, credential);
//        }
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值