apache shiro权限认证类

  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. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值