`
leobluewing
  • 浏览: 238843 次
  • 性别: Icon_minigender_1
  • 来自: 宁波
社区版块
存档分类
最新评论

CAS Shrio 配置登陆的一点心得

    博客分类:
  • sso
 
阅读更多
开始研究sso。

选了CAS,毕竟老牌开源sso工具,网上查了下据说80%的sso技术都是基于cas。

其他东西不说了,什么ssl,cas server部署什么的,网上都很多,也都写的很好。

这里仅记录一点自己实现中比较细的地方。

shiro里面一般在做 AuthenticationInfo的时候返回的info里面的principals一般都不会是简单的string(用户名),应该都是自己包装过的User对象。

我这边的demo系统也不例外。这次搭sso demo之前先弄了最简单的两个app实验,就是只有最简单的一个jsp页面,通过配置web.xml来做,一小时就搞定了,的确是很方便。

但是在shiro整合中发现在server认证通过后跳回原系统就报错。

整个CAS的认证过程已经到了第6步,认证服务器已经给client返回了用户名,所以这个错误应该和server没关系,是cilent本身的问题。

设断点跟踪后发现是token和生成的info不能匹配导致出问题。在不用CAS的情况下一般就是指输入的密码出错导致的。

顿时莫名其妙,认证都不在client进行了居然还会有这种错误。

后来发现是用的自定义的principals对象在copy代码过程中忘记改了。。。

在CasRealm标准实现中,credentials用的是ticket

return new SimpleAuthenticationInfo(principalCollection, ticket);


结果在我自己改写的过程中

return new SimpleAuthenticationInfo(new ShiroUser(user.getId(),
    					user.getUserName(), user.getRealName()),
    					[color=red]user.getPassword()[/color], getName());
copy过来的时候没改成ticket。


事后总结思考:事情虽小,但是其实解决过程也搞了两三个小时。不过最终还是理解了CAS和shiro的交互过程,最终在shiro的认证密码其实用的是返回的ticket。

在网上找的一些文章好像都没有着重强调这一点。

“纸上得来终觉浅,绝知此事要躬行”。



分享到:
评论
2 楼 leobluewing 2014-04-29  
sgq0085 写道
楼主你好,我们知道shiro登陆认证的位置是自己实现AuthorizingRealm类的 doGetAuthenticationInfo方法中。
即便是和cas整合,这里应该也是跑不了的。能麻烦楼主把你这个类的实现,或者就这个方法的代码帖上来么?



将原来的方法里的

return new SimpleAuthenticationInfo(principalCollection, ticket); 这个改写掉就行了

其他不用动。我这边直接注释掉了

// create simple authentication info
            //List<Object> principals = CollectionUtils.asList(userId, attributes);
            //PrincipalCollection principalCollection = new SimplePrincipalCollection(principals, getName());
            //return new SimpleAuthenticationInfo(principalCollection, ticket);
            
            /**
             * 替换原有的用户返回
             * */
            User user = userService.findByUserName(userId);
            if (user != null) {
    			return new SimpleAuthenticationInfo(new ShiroUser(user.getId(),
    					user.getUserName(), user.getRealName()),
    					ticket, getName());
    		} else {
    			return null;
    		}
1 楼 sgq0085 2014-04-29  
楼主你好,我们知道shiro登陆认证的位置是自己实现AuthorizingRealm类的 doGetAuthenticationInfo方法中。
即便是和cas整合,这里应该也是跑不了的。能麻烦楼主把你这个类的实现,或者就这个方法的代码帖上来么?

相关推荐

Global site tag (gtag.js) - Google Analytics