shiro的认证和授权

    
    import java.util.List;
    
    import org.apache.shiro.authc.AuthenticationException;
    import org.apache.shiro.authc.AuthenticationInfo;
    import org.apache.shiro.authc.AuthenticationToken;
    import org.apache.shiro.authc.LockedAccountException;
    import org.apache.shiro.authc.SimpleAuthenticationInfo;
    import org.apache.shiro.authc.UnknownAccountException;
    import org.apache.shiro.authz.AuthorizationInfo;
    import org.apache.shiro.authz.SimpleAuthorizationInfo;
    import org.apache.shiro.realm.AuthorizingRealm;
    import org.apache.shiro.subject.PrincipalCollection;
    import org.apache.shiro.util.ByteSource;
    import org.springframework.beans.factory.annotation.Autowired;
    
    import com.zzsxt.lee.shiro.model.Admin;
    import com.zzsxt.lee.shiro.service.AdminService;
    import com.zzsxt.lee.shiro.service.PermissionService;
    import com.zzsxt.lee.shiro.service.RoleService;
    
    public class ShiroRealm  extends AuthorizingRealm{
    		@Autowired
    	    private AdminService adminService;
    	    @Autowired
    	    private RoleService roleService;
    	    @Autowired
    	    private PermissionService permissionService;
    	//认证
    	@Override
    	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
    		SimpleAuthenticationInfo info = null;
    		//1、获取用户名
    		String adminName =(String) authenticationToken.getPrincipal();
    		//2、从数据库中查询用户信息
    		Admin admin = adminService.selectAdminByName(adminName);
    		if(admin.getId()!=0L){
    			//进来则查寻出信息
    			if(admin.getAdminLocked()==2){
    				throw new LockedAccountException("账号被锁定,请联系管理员");
    			}
    			info = new SimpleAuthenticationInfo(admin.getAdminName(), admin.getAdminPassword(), ByteSource.Util.bytes(admin.getAdminSalt()), this.getName());
    			
    		}else{
    			throw new UnknownAccountException("暂无数据");
    		}
    		return info;
    	}
    	
    	//授权
    	@Override
    	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
    		SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
    		//1、通过principalCollection对象获取从认证阶段传过来的参数(用户名或者对象)
    		String adminName = (String)principalCollection.getPrimaryPrincipal();
    		//2、通过用户名查询用户所以角色信息
    		List<String> roleList = roleService.selectRolesByAdminName(adminName);
    		if(roleList.size()>0){
    			info.addRoles(roleList);
    		}
    		//3、通过用户名查询用户所以权限信息
    		List<String> permissionList = permissionService.selectPermissionByAdminName(adminName);
    		if(permissionList.size()>0){
    			info.addStringPermissions(permissionList);
    		}
    		return info;
    	}
    
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值