`
goodluck_wgw
  • 浏览: 96462 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

apache shiro整合struts2+spring+mybatis简单demo

阅读更多
先上一段shiro中获取数据源的代码,Demo见附件

public class AuthenticationRealm extends AuthorizingRealm {

    private static final Logger log = LoggerFactory.getLogger(AuthenticationRealm.class);
   
    @Autowired
private UserMapper userMapper;
   
    @Autowired
    private RoleMapper roleMapper;
   
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        UsernamePasswordToken upToken = (UsernamePasswordToken) token;
        String username = upToken.getUsername();
//        char[] password = upToken.getPassword();
//        log.debug(String.valueOf(password));
        // Null username is invalid
        if (username == null) {
            throw new AccountException("用户名不能为空!");
        }
        SimpleAuthenticationInfo info = null;
       try{
       String password =  userMapper.getPassword(username);
       if (password == null) {
           throw new UnknownAccountException("No account found for user [" + username + "]");
       }
//       if(password.length>2){
//       throw new AuthenticationException("More than one user row found for user [" + username + "]. Usernames must be unique.");
//       }
       info = new SimpleAuthenticationInfo(username, password.toCharArray(), getName());
       }  catch (RuntimeException e) {
           final String message = "There was a SQL error while authenticating user [" + username + "]";
           throw new AuthenticationException(message, e);
       }
       
           return info;
    }



@Override
protected AuthorizationInfo doGetAuthorizationInfo(
PrincipalCollection principals) {
  if (principals == null) {
            throw new AuthorizationException("PrincipalCollection method argument cannot be null.");
        }

        String username = (String) getAvailablePrincipal(principals);
        Set<String> roleNames = getRoleNameByUserName(username);
        Set<String> permissions = new HashSet<String>();
        for(String roleName : roleNames){
        Role role = roleMapper.getRole(roleName);
        for(Permission permission :role.getPermissions()){
        permissions.add(permission.getModule()+":"+permission.getPrivilege());
        }
       
        }
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo(roleNames);
        info.setStringPermissions(permissions);
        return info;
}

public Set<String> getRoleNameByUserName(String username){
Set<String> roless = new HashSet<String>();
User user = userMapper.getUser(username);
for(Role role:user.getRoles()){
roless.add(role.getName());
log.debug(role.getName());
}

return roless;

}
0
0
分享到:
评论
6 楼 鱼崽崽 2015-08-03  
群主写的不错,可以看看这个demo实例,很不错
http://43.249.81.29:8080/index.html
5 楼 liailove 2015-05-20  
代码不全,数据库没有诶
4 楼 goodluck_wgw 2014-04-25  
niweiwei 写道
哥们,你这个怎么整合的,有没有源码下载,求

下面不是有下载吗
3 楼 niweiwei 2014-04-15  
哥们,你这个怎么整合的,有没有源码下载,求
2 楼 chyzh2010 2013-10-16  
maven的pom.xml文件在我这边出错,有红叉,导致jar包没法下载完成。
1 楼 jaiiye 2012-07-20  
大哥,你这个好像没有数据库啊,方便提供下么?如果可以,请麻烦发送到我的邮箱jaiiye#hotmail.com 不胜感激。

相关推荐

Global site tag (gtag.js) - Google Analytics