whycxzp
2021-01-11 15152da2f87438747c519adcb6237093b34ae2b4
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
package com.whyc.service;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.whyc.mapper.PermissionMapper;
import com.whyc.mapper.RoleMapper;
import com.whyc.mapper.UserMapper;
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.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;
 
/**
 * 处理 shiro认证,授权,和数据库交互
 */
@Service
public class UserBridgeService {
 
    @Resource
    private UserMapper userMapper;
 
    @Resource
    private RoleMapper roleMapper;
 
    @Resource
    private PermissionMapper permissionMapper;
 
    public User findPasswordAndSlatByUserName(String userName) {
        QueryWrapper<User> queryWrapper = Wrappers.query();
        queryWrapper.select("id","password","salt").eq("name",userName);
        try{
            return userMapper.selectOne(queryWrapper);
        }catch (Exception e){
            e.printStackTrace();
            return new User(0,"用户不存在");
        }
    }
 
    @Cacheable(cacheNames = "authorizationCache",key = "#root.method")
    public AuthorizationInfo getAuthorizationInfo(User user) {
        SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
        //添加Roles和Permissions
        List<String> roles = this.findRolesByUserId(user.getId());
        List<String> perms = this.findPermissionsByUserId(user.getId());
 
        authorizationInfo.addRoles(roles);
        authorizationInfo.addStringPermissions(perms);
        return authorizationInfo;
    }
 
    private List<String> findPermissionsByUserId(int userId) {
        QueryWrapper<Permission> query = Wrappers.query();
        List<String> perms = new LinkedList<>();
        //perms.add("water:all");
        return perms;
    }
 
    private List<String> findRolesByUserId(int userId) {
        List<String> roles = new LinkedList<>();
        //roles.add("dev");
        return roles;
    }
}