apache shiro权限认证类

来源:互联网 发布:淘宝windows 编辑:程序博客网 时间:2024/05/16 06:47
  1. public class ShiroDbRealm extends AuthorizingRealm {  
  2.   
  3.     @Inject  
  4.   
  5.     private UserService userService ;  
  6.   
  7.       
  8.   
  9.     /** 
  10.  
  11.  * 认证回调函数,登录时调用. 
  12.  
  13.  */  
  14.   
  15. protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authcToken)   
  16. throws AuthenticationException {  
  17.   
  18. UsernamePasswordToken token = (UsernamePasswordToken) authcToken;  
  19.   
  20. User user= userService.getUserByUserId(token.getUsername());  
  21.   
  22. if (user!= null) {    
  23.   
  24.     return new SimpleAuthenticationInfo(user.getUserId(), user.getUserId(), getName());  
  25.   
  26. else {  
  27.   
  28. return null;  
  29.   
  30. }  
  31.   
  32. }  
  33.   
  34. /** 
  35.  
  36.  * 授权查询回调函数, 进行鉴权但缓存中无用户的授权信息时调用. 
  37.  
  38.  */  
  39.   
  40. protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {  
  41.   
  42. String loginName = (String) principals.fromRealm(getName()).iterator().next();  
  43.   
  44. User user= userService.getUserByUserId(loginName);  
  45.   
  46. if (user != null) {  
  47.   
  48. SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();  
  49.   
  50. info.addStringPermission("common-user");  
  51.   
  52. return info;  
  53.   
  54. else {  
  55.   
  56. return null;  
  57.   
  58. }  
  59.   
  60. }  
  61.   
  62. }  
原创粉丝点击