whyclxw
2024-12-10 88ed0e2971154580635fd33f3073e3a26f52fc3a
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
package com.whyc.realm;
 
import com.whyc.anotation.Realm;
import com.whyc.pojo.db_user.UserInf;
import com.whyc.service.UserBridgeService;
import com.whyc.util.RSAUtil;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
 
/**
 * 实际处理认证授权,跟数据库交互
 */
@Realm
public class CustomRealm extends AuthorizingRealm {
 
    /**必须@Lazy注解,@Lazy与@Autowired组合,使得依赖于Service相关的Bean都是lazy-resolution proxy*/
    @Lazy
    @Autowired
    UserBridgeService userBridgeService;
 
    /**
     * 认证
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        String userName = (String) authenticationToken.getPrincipal();
        UserInf user = userBridgeService.findPasswordByUserName(userName);
        if(user.getUid()==0){
            throw new UnknownAccountException("账号不存在");
        }
        String password = RSAUtil.decrypt(user.getUsnid(),RSAUtil.getPrivateKey());
        return new SimpleAuthenticationInfo(user,password,getName());
    }
 
    /**
     * 授权
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        UserInf user = (UserInf) principalCollection.getPrimaryPrincipal();
        return userBridgeService.getAuthorizationInfo(user);
    }
 
    /**加密*//*
    @PostConstruct
    public void initCredentialsMatcher() {
        HashedCredentialsMatcher matcher = new HashedCredentialsMatcher(SuperConstant.HASH_ALGORITHM_MD5);
        matcher.setHashIterations(SuperConstant.HASH_INTEGRATIONS_ONE);
        setCredentialsMatcher(matcher);
    }*/
}