您当前的位置: 首页 >  spring

科技D人生

暂无认证

  • 0浏览

    0关注

    1550博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Shiro学习总结(10)——Spring集成Shiro

科技D人生 发布时间:2016-05-09 14:50:00 ,浏览量:0

1.引入Shiro的Maven依赖
[html]  view plain  copy
  1.   
  2.   
  3.       
  4.         org.apache.shiro  
  5.         shiro-core  
  6.         1.2.1  
  7.       
  8.       
  9.         org.apache.shiro  
  10.         shiro-web  
  11.         1.2.1  
  12.       
  13.       
  14.         org.apache.shiro  
  15.         shiro-ehcache  
  16.         1.2.1  
  17.       
  18.       
  19.         org.apache.shiro  
  20.         shiro-spring  
  21.         1.2.1  
  22.       
  23.       
  24.        
  25.        shiroFilter    
  26.        org.springframework.web.filter.DelegatingFilterProxy    
  27.        
  28.        
  29.        shiroFilter    
  30.        /*    
  31.       

3.    编写自己的UserRealm类继承自Realm,主要实现认证和授权的管理操作

[java]  view plain  copy
  1. package com.jay.demo.shiro;  
  2.   
  3. import java.util.HashSet;  
  4. import java.util.Iterator;  
  5. import java.util.Set;  
  6.   
  7. import org.apache.shiro.authc.AuthenticationException;  
  8. import org.apache.shiro.authc.AuthenticationInfo;  
  9. import org.apache.shiro.authc.AuthenticationToken;  
  10. import org.apache.shiro.authc.LockedAccountException;  
  11. import org.apache.shiro.authc.SimpleAuthenticationInfo;  
  12. import org.apache.shiro.authc.UnknownAccountException;  
  13. import org.apache.shiro.authz.AuthorizationInfo;  
  14. import org.apache.shiro.authz.SimpleAuthorizationInfo;  
  15. import org.apache.shiro.realm.AuthorizingRealm;  
  16. import org.apache.shiro.subject.PrincipalCollection;  
  17. import org.springframework.beans.factory.annotation.Autowired;  
  18.   
  19. import com.jay.demo.bean.Permission;  
  20. import com.jay.demo.bean.Role;  
  21. import com.jay.demo.bean.User;  
  22. import com.jay.demo.service.UserService;  
  23.   
  24. public class UserRealm extends AuthorizingRealm{  
  25.       
  26.     @Autowired  
  27.     private UserService userService;  
  28.   
  29.     /** 
  30.      * 授权操作 
  31.      */  
  32.     @Override  
  33.     protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {  
  34. //      String username = (String) getAvailablePrincipal(principals);  
  35.         String username = (String) principals.getPrimaryPrincipal();  
  36.           
  37.         Set roleSet =  userService.findUserByUsername(username).getRoleSet();  
  38.         //角色名的集合  
  39.         Set roles = new HashSet();  
  40.         //权限名的集合  
  41.         Set permissions = new HashSet();  
  42.           
  43.         Iterator it = roleSet.iterator();  
  44.         while(it.hasNext()){  
  45.             roles.add(it.next().getName());  
  46.             for(Permission per:it.next().getPermissionSet()){  
  47.                 permissions.add(per.getName());  
  48.             }  
  49.         }  
  50.   
  51.           
  52.         SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();  
  53.           
  54.         authorizationInfo.addRoles(roles);  
  55.         authorizationInfo.addStringPermissions(permissions);  
  56.           
  57.           
  58.         return authorizationInfo;  
  59.     }  
  60.   
  61.     /** 
  62.      * 身份验证操作 
  63.      */  
  64.     @Override  
  65.     protected AuthenticationInfo doGetAuthenticationInfo(  
  66.             AuthenticationToken token) throws AuthenticationException {  
  67.           
  68.         String username = (String) token.getPrincipal();  
  69.         User user = userService.findUserByUsername(username);  
  70.           
  71.         if(user==null){  
  72.             //木有找到用户  
  73.             throw new UnknownAccountException("没有找到该账号");  
  74.         }  
  75.         /* if(Boolean.TRUE.equals(user.getLocked())) {   
  76.                 throw new LockedAccountException(); //帐号锁定   
  77.             } */  
  78.           
  79.         /** 
  80.          * 交给AuthenticatingRealm使用CredentialsMatcher进行密码匹配,如果觉得人家的不好可以在此判断或自定义实现   
  81.          */  
  82.         SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(user.getUsername(), user.getPassword(),getName());  
  83.           
  84.           
  85.         return info;  
  86.     }  
  87.       
  88.     @Override  
  89.     public String getName() {  
  90.         return getClass().getName();  
  91.     }  
  92.   
  93. }  

4.在Spring的applicationContext.xml中进行Shiro的相关配置

1、添加shiroFilter定义 

Xml代码   收藏代码
  1.   
  2.   
  3.       
  4.       
  5.       
  6.       
  7.       
  8.           
  9.             /login = anon  
  10.             /user/** = authc  
  11.             /role/edit/* = perms[role:edit]  
  12.             /role/save = perms[role:edit]  
  13.             /role/list = perms[role:view]  
  14.             /** = authc  
  15.           
  16.       
  17.   
2、添加securityManager定义 
Xml代码   收藏代码
  1.   
  2.       
  3.   
3、添加realm定义 
Xml代码   收藏代码
关注
打赏
1662604032
查看更多评论
立即登录/注册

微信扫码登录

0.0553s