4
hao
2025-04-16 c5fb1fbcbb2bf4d511773d348f9ef625855c61fc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package com.shiro;
 
/*import com.wyait.manage.dao.UserMapper;
import com.wyait.manage.pojo.Permission;
import com.wyait.manage.pojo.Role;
import com.wyait.manage.pojo.User;
import com.wyait.manage.service.AuthService;
import com.wyait.manage.service.UserServiceImpl;*/
 
import com.system.user.dao.SysUserDao;
import com.system.user.entity.SysUser;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UsernamePasswordToken;
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.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import com.pojo.User;
 
/**
 * @项目名称:wyait-manage
 * @包名:com.wyait.manage.shiro
 * @类描述:
 * @创建人:wyait
 * @创建时间:2017-12-13 13:53
 * @version:V1.0
 */
@Service
public class ShiroRealm extends AuthorizingRealm {
 
    private static final Logger logger = LoggerFactory
            .getLogger(ShiroRealm.class);
 
    @Autowired
    private SysUserDao sysUserDao;
 
    /*@Autowired
    private UserMapper userMapper;
    @Autowired
    private AuthService authService;*/
 
    /**
     * 授予角色和权限
     * @param principalCollection
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(
            PrincipalCollection principalCollection) {
        //授权
        logger.debug("授予角色和权限");
        // 添加权限 和 角色信息
        SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
        // 获取当前登陆用户
        Subject subject = SecurityUtils.getSubject();
        SysUser user = (SysUser) subject.getPrincipal();
//        if (user.getUserMobile().equals("18516596566")) {
        if (user.getFcode().equals("PL032")) {
            // 超级管理员,添加所有角色、添加所有权限
            authorizationInfo.addRole("*");
            authorizationInfo.addStringPermission("*");
        } else {
            // 普通用户,查询用户的角色,根据角色查询权限
//            Long userId = user.getId();
            /*List<Role> roles = this.authService.getRoleByUser(userId);
            if (null != roles && roles.size() > 0) {
                for (Role role : roles) {
                    authorizationInfo.addRole(role.getCode());
                    // 角色对应的权限数据
                    List<Permission> perms = this.authService.findPermsByRoleId(role
                            .getId());
                    if (null != perms && perms.size() > 0) {
                        // 授权角色下所有权限
                        for (Permission perm : perms) {
                            authorizationInfo.addStringPermission(perm
                                    .getCode());
                        }
                    }
                }
            }*/
        }
        return authorizationInfo;
    }
 
    /**
     * 登录认证
     * @param authenticationToken
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(
            AuthenticationToken authenticationToken)
            throws AuthenticationException {
        //TODO
        //UsernamePasswordToken用于存放提交的登录信息
        UsernamePasswordToken token = (UsernamePasswordToken)authenticationToken;
        logger.info("用户登录认证:验证当前Subject时获取到token为:" + ReflectionToStringBuilder
                .toString(token, ToStringStyle.MULTI_LINE_STYLE));
//        String mobile = token.getUsername();
        String userName = token.getUsername();
        // 调用数据层
        //User user = userMapper.findUserByMobile(mobile);
        //SysUser user = new SysUser();
        SysUser user = sysUserDao.findByFcode(userName);
        SysUser userMD5 = new SysUser();
        try{
            userMD5.setFid("1");
            userMD5.setFcode(user.getFcode());
            userMD5.setFname(user.getFname());
            userMD5.setFpassword(DigestUtils.md5Hex(proPass(user.getFpassword())));
        }catch (Exception e){
            userMD5.setFpassword(DigestUtils.md5Hex("a"));
        }
//        SysUser user = sysUserDao.findByIsDelAndUserCode(0,userName);
 
        logger.debug("用户登录认证!用户信息user:" + user);
        if (user == null) {
            // 用户不存在
            return null;
        } else {
            // 密码存在
            // 第一个参数 ,登陆后,需要在session保存数据
            // 第二个参数,查询到密码(加密规则要和自定义的HashedCredentialsMatcher中的HashAlgorithmName散列算法一致)
            // 第三个参数 ,realm名字
//            return new SimpleAuthenticationInfo(user, DigestUtils.md5Hex(user.getUserPassword()),
//                    getName());
            return new SimpleAuthenticationInfo(userMD5, DigestUtils.md5Hex(userMD5.getFpassword()),
                    getName());
        }
    }
 
    /**
     * 清除所有缓存【实测无效】
     */
    public void clearCachedAuth(){
        this.clearCachedAuthorizationInfo(SecurityUtils.getSubject().getPrincipals());
    }
 
    //解密算法
    private String proPass(String src) throws Exception {
        String result = "";
        int first = new Integer(src.substring(0, 1)).intValue();
        String src_tem = src.substring(1);
        byte[] b = src_tem.getBytes("iso8859-1");
        byte[] temp = b;
        int i = 0;
        for (; i < b.length; i++) {
            temp[i] = new Integer(new Integer(temp[i]).intValue() ^ (first + 18))
                    .byteValue();
        }
        result = new String(temp);
        return result;
    }
}