ShiroRealm.java 5.0 KB
Newer Older
Y
yadong.zhang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/**
 * MIT License
 * Copyright (c) 2018 yadong.zhang
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package com.zyd.blog.core.shiro.realm;

import com.zyd.blog.business.entity.Resources;
Y
yadong.zhang 已提交
23
import com.zyd.blog.business.entity.Role;
Y
yadong.zhang 已提交
24 25
import com.zyd.blog.business.entity.User;
import com.zyd.blog.business.enums.UserStatusEnum;
智布道's avatar
智布道 已提交
26
import com.zyd.blog.business.enums.UserTypeEnum;
Y
yadong.zhang 已提交
27
import com.zyd.blog.business.service.SysResourcesService;
Y
yadong.zhang 已提交
28
import com.zyd.blog.business.service.SysRoleService;
Y
yadong.zhang 已提交
29 30 31 32 33 34 35 36 37 38 39 40
import com.zyd.blog.business.service.SysUserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
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.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
智布道's avatar
智布道 已提交
41 42
import java.util.Arrays;
import java.util.HashSet;
Y
yadong.zhang 已提交
43
import java.util.List;
智布道's avatar
智布道 已提交
44
import java.util.Set;
Y
yadong.zhang 已提交
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60

/**
 * Shiro-密码输入错误的状态下重试次数的匹配管理
 *
 * @author yadong.zhang (yadong.zhang0415(a)gmail.com)
 * @version 1.0
 * @website https://www.zhyd.me
 * @date 2018/4/24 14:37
 * @since 1.0
 */
public class ShiroRealm extends AuthorizingRealm {

    @Resource
    private SysUserService userService;
    @Resource
    private SysResourcesService resourcesService;
Y
yadong.zhang 已提交
61 62
    @Resource
    private SysRoleService roleService;
Y
yadong.zhang 已提交
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78

    /**
     * 提供账户信息返回认证信息(用户的角色信息集合)
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        //获取用户的输入的账号.
        String username = (String) token.getPrincipal();
        User user = userService.getByUserName(username);
        if (user == null) {
            throw new UnknownAccountException("账号不存在!");
        }
        if (user.getStatus() != null && UserStatusEnum.DISABLE.getCode().equals(user.getStatus())) {
            throw new LockedAccountException("帐号已被锁定,禁止登录!");
        }

Y
yadong.zhang 已提交
79 80 81
        // principal参数使用用户Id,方便动态刷新用户权限
        return new SimpleAuthenticationInfo(
                user.getId(),
Y
yadong.zhang 已提交
82 83 84 85 86 87 88 89 90 91 92 93 94
                user.getPassword(),
                ByteSource.Util.bytes(username),
                getName()
        );
    }

    /**
     * 权限认证,为当前登录的Subject授予角色和权限(角色的权限信息集合)
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        // 权限信息对象info,用来存放查出的用户的所有的角色(role)及权限(permission)
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
Y
yadong.zhang 已提交
95 96 97 98 99 100 101 102 103 104

        Long userId = (Long) SecurityUtils.getSubject().getPrincipal();

        // 赋予角色
        List<Role> roleList = roleService.listRolesByUserId(userId);
        for (Role role : roleList) {
            info.addRole(role.getName());
        }

        // 赋予权限
智布道's avatar
智布道 已提交
105 106 107 108 109 110 111 112 113 114 115 116
        List<Resources> resourcesList = null;
        User user = userService.getByPrimaryKey(userId);
        if (null == user) {
            return info;
        }
        // ROOT用户默认拥有所有权限
        if (UserTypeEnum.ROOT.toString().equalsIgnoreCase(user.getUserType())) {
            resourcesList = resourcesService.listAll();
        } else {
            resourcesList = resourcesService.listByUserId(userId);
        }

Y
yadong.zhang 已提交
117
        if (!CollectionUtils.isEmpty(resourcesList)) {
智布道's avatar
智布道 已提交
118
            Set<String> permissionSet = new HashSet<>();
Y
yadong.zhang 已提交
119
            for (Resources resources : resourcesList) {
Y
yadong.zhang 已提交
120 121
                String permission = null;
                if (!StringUtils.isEmpty(permission = resources.getPermission())) {
智布道's avatar
智布道 已提交
122
                    permissionSet.addAll(Arrays.asList(permission.trim().split(",")));
Y
yadong.zhang 已提交
123 124
                }
            }
智布道's avatar
智布道 已提交
125
            info.setStringPermissions(permissionSet);
Y
yadong.zhang 已提交
126 127 128 129 130
        }
        return info;
    }

}