作者 朱兆平

add: 增加查询用户是否有对应权限接口

  1 +# 分支描述
  2 +- SAAS化用户集中管理鉴权平台
@@ -122,4 +122,15 @@ public class PermssionController { @@ -122,4 +122,15 @@ public class PermssionController {
122 return new ResultJson<List<PERMISSION>>("200","success",permissionService.getUserMenuTreeByUserId(0)); 122 return new ResultJson<List<PERMISSION>>("200","success",permissionService.getUserMenuTreeByUserId(0));
123 123
124 } 124 }
  125 +
  126 + @ApiOperation(value = "根据用户ID查询用户权限", notes = "根据用户ID查询用户权限")
  127 + @GetMapping("/getPermission")
  128 + public ResultJson getPermission(
  129 + @RequestHeader("Authorization") String token,
  130 + @RequestParam(value = "url")String url,
  131 + @RequestParam(value = "name")String name){
  132 + Boolean result = permissionService.getPermission(token, url, name);
  133 + return new ResultJson("200","success",result);
  134 +
  135 + }
125 } 136 }
@@ -46,4 +46,12 @@ public interface PermissionService { @@ -46,4 +46,12 @@ public interface PermissionService {
46 * @return 46 * @return
47 */ 47 */
48 List<PERMISSION> getUserMenuTreeByUserId(Integer userId); 48 List<PERMISSION> getUserMenuTreeByUserId(Integer userId);
  49 +
  50 + /**
  51 + * 查询用户是否有对应访问权限
  52 + * @param token
  53 + * @param url
  54 + * @return 有为true 没有为false
  55 + */
  56 + Boolean getPermission(String token,String url,String name);
49 } 57 }
1 package com.tianbo.warehouse.service.imp; 1 package com.tianbo.warehouse.service.imp;
2 2
  3 +import com.alibaba.fastjson.JSONObject;
3 import com.github.pagehelper.Page; 4 import com.github.pagehelper.Page;
4 import com.github.pagehelper.PageHelper; 5 import com.github.pagehelper.PageHelper;
5 import com.github.pagehelper.PageInfo; 6 import com.github.pagehelper.PageInfo;
@@ -9,8 +10,12 @@ import com.tianbo.warehouse.annotation.cache.annotation.RedisCacheable; @@ -9,8 +10,12 @@ import com.tianbo.warehouse.annotation.cache.annotation.RedisCacheable;
9 import com.tianbo.warehouse.dao.PERMISSIONMapper; 10 import com.tianbo.warehouse.dao.PERMISSIONMapper;
10 import com.tianbo.warehouse.model.PERMISSION; 11 import com.tianbo.warehouse.model.PERMISSION;
11 import com.tianbo.warehouse.model.ROLE; 12 import com.tianbo.warehouse.model.ROLE;
  13 +import com.tianbo.warehouse.model.USERS;
12 import com.tianbo.warehouse.service.PermissionService; 14 import com.tianbo.warehouse.service.PermissionService;
  15 +import com.tianbo.warehouse.util.RedisUtils;
13 import lombok.extern.slf4j.Slf4j; 16 import lombok.extern.slf4j.Slf4j;
  17 +import org.springframework.beans.factory.annotation.Autowired;
  18 +import org.springframework.data.redis.core.RedisTemplate;
14 import org.springframework.stereotype.Service; 19 import org.springframework.stereotype.Service;
15 20
16 import javax.annotation.Resource; 21 import javax.annotation.Resource;
@@ -25,6 +30,9 @@ public class PermissionServiceImp implements PermissionService { @@ -25,6 +30,9 @@ public class PermissionServiceImp implements PermissionService {
25 @Resource 30 @Resource
26 PERMISSIONMapper permissionMapper; 31 PERMISSIONMapper permissionMapper;
27 32
  33 + @Autowired
  34 + RedisUtils redisUtils;
  35 +
28 @Override 36 @Override
29 // @RedisCacheable(cacheKey = "findAllMenus") 37 // @RedisCacheable(cacheKey = "findAllMenus")
30 public PageInfo<PERMISSION> findAll(int pageNum, int pageSize, String name) { 38 public PageInfo<PERMISSION> findAll(int pageNum, int pageSize, String name) {
@@ -226,4 +234,33 @@ public class PermissionServiceImp implements PermissionService { @@ -226,4 +234,33 @@ public class PermissionServiceImp implements PermissionService {
226 234
227 } 235 }
228 236
  237 + @Override
  238 + public Boolean getPermission(String token,String url,String name){
  239 +
  240 + try {
  241 + if(token != null && token.startsWith("Bearer ")) {
  242 + token = token.substring(7); // 7 是 "Bearer " 的长度
  243 + String userJsonStr = redisUtils.get(token);
  244 + USERS user = JSONObject.parseObject(userJsonStr, USERS.class);
  245 + PERMISSION result = user.getPermissions().stream()
  246 + .filter(permission -> "转关运抵申报申报".equals(permission.getName()) || url.equals(permission.getUrl()))
  247 + .findFirst()
  248 + .orElse(null);
  249 + // 输出查询结果
  250 + if (result != null) {
  251 + System.out.println("匹配到对应权限");
  252 + return true;
  253 + } else {
  254 + return false;
  255 + }
  256 +
  257 + } else {
  258 + // 处理未包含Bearer前缀的情况
  259 + return false;
  260 + }
  261 + }catch (Exception e){
  262 + return false;
  263 + }
  264 + }
  265 +
229 } 266 }