package com.whyc.service;
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
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.cache.annotation.CacheConfig;
|
import org.springframework.cache.annotation.Cacheable;
|
import org.springframework.context.annotation.Lazy;
|
import org.springframework.stereotype.Service;
|
|
import javax.annotation.Resource;
|
import java.util.LinkedList;
|
import java.util.List;
|
|
/**
|
* 处理 shiro认证,授权,和数据库交互
|
*/
|
@Service
|
//Unified Naming
|
@CacheConfig(cacheNames ={"userBridge"})
|
public class UserBridgeService {
|
|
@Resource
|
private UserMapper userMapper;
|
|
@Resource
|
private UserRoleMapper userRoleMapper;
|
|
@Resource
|
private RolePermissionMapper rolePermissionMapper;
|
|
public User findPasswordAndSlatByUserName(String userName) {
|
QueryWrapper<User> queryWrapper = Wrappers.query();
|
queryWrapper.select("id","name","password","salt").eq("name",userName);
|
try{
|
return userMapper.selectOne(queryWrapper);
|
}catch (Exception e){
|
e.printStackTrace();
|
return new User(0,"用户不存在");
|
}
|
}
|
|
@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;
|
}
|
|
//@Cacheable(key = "#root.methodName+#userId")
|
private List<String> findPermissionsByUserId(int userId) {
|
List<String> perms = rolePermissionMapper.findPermissionsByUserId(userId);
|
return perms;
|
}
|
|
//@Cacheable(key="#root.methodName+#userId")
|
private List<String> findRolesByUserId(int userId) {
|
List<String> roles =userRoleMapper.findRolesByUserId(userId);
|
//roles.add("dev");
|
return roles;
|
}
|
}
|