whycxzp
2021-04-15 e4e8380597b5abdd2ede656cf5291c5d760b1149
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
package com.whyc.service;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.whyc.config.CaffeineConfig;
import com.whyc.mapper.*;
import com.whyc.pojo.Permission;
import com.whyc.pojo.User;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.caffeine.CaffeineCache;
import org.springframework.context.annotation.DependsOn;
import org.springframework.context.annotation.Lazy;
import org.springframework.core.annotation.AliasFor;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;
 
/**
 * 处理 shiro认证,授权,和数据库交互
 */
@Service
//Unified Naming
//@CacheConfig(cacheNames ={"userBridge"})
@DependsOn("caffeineCacheManager")
public class UserBridgeService {
 
    @Resource
    private UserMapper userMapper;
 
    @Resource
    private UserRoleMapper userRoleMapper;
 
    @Resource
    private RolePermissionMapper rolePermissionMapper;
 
    @Resource
    private CacheManager caffeineCacheManager;
 
    public User findPasswordAndSlatByUserName(String userName) {
        QueryWrapper<User> queryWrapper = Wrappers.query();
        queryWrapper.select("id", "name", "password", "salt").eq("name", userName);
        User user = userMapper.selectOne(queryWrapper);
        if (user == null) {
            return new User(0, "用户不存在");
        }
        return user;
    }
 
    /**
     * TODO 采用caffeineCache重写
     *
     * @param user
     * @return
     */
    //@Cacheable(key = "#root.methodName+#p0.id")
    public AuthorizationInfo getAuthorizationInfo(User user) {
        System.out.println("=========执行了UserBridgeService.getAuthorization方法==========");
        SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
        //添加Roles和Permissions
        List<String> roles = findRolesByUserId(user.getId());
        List<String> perms = findPermissionsByUserId(user.getId());
 
        authorizationInfo.addRoles(roles);
        authorizationInfo.addStringPermissions(perms);
        return authorizationInfo;
    }
 
    private List<String> findPermissionsByUserId(int userId) {
        List<String> perms = rolePermissionMapper.findPermissionsByUserId(userId);
        return perms;
    }
 
    private List<String> findRolesByUserId(int userId) {
        List<String> roles = userRoleMapper.findRolesByUserId(userId);
        //roles.add("dev");
        return roles;
    }
}