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 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 roles = findRolesByUserId(user.getId()); List perms = findPermissionsByUserId(user.getId()); authorizationInfo.addRoles(roles); authorizationInfo.addStringPermissions(perms); return authorizationInfo; } private List findPermissionsByUserId(int userId) { List perms = rolePermissionMapper.findPermissionsByUserId(userId); return perms; } private List findRolesByUserId(int userId) { List roles = userRoleMapper.findRolesByUserId(userId); //roles.add("dev"); return roles; } }