MagicApiRequestInterceptor.java 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package com.xjrsoft.common.interceptor;
  2. import cn.dev33.satoken.session.SaSession;
  3. import cn.dev33.satoken.stp.StpUtil;
  4. import com.xjrsoft.common.constant.GlobalConstant;
  5. import com.xjrsoft.common.enums.ResponseCode;
  6. import com.xjrsoft.common.model.result.R;
  7. import lombok.extern.slf4j.Slf4j;
  8. import org.springframework.stereotype.Component;
  9. import org.ssssssss.magicapi.core.context.RequestEntity;
  10. import org.ssssssss.magicapi.core.interceptor.RequestInterceptor;
  11. import java.util.ArrayList;
  12. import java.util.List;
  13. /**
  14. * magic-api 接口鉴权
  15. *
  16. * @author tzx
  17. */
  18. @Component
  19. @Slf4j
  20. public class MagicApiRequestInterceptor implements RequestInterceptor {
  21. /***
  22. * 接口请求之前
  23. * @param requestEntity
  24. * @return
  25. */
  26. @Override
  27. public Object preHandle(RequestEntity requestEntity) {
  28. if (!StpUtil.isLogin()) {
  29. return R.error(ResponseCode.UN_AUTHORIZED.getCode(), ResponseCode.UN_AUTHORIZED.getMessage());
  30. }
  31. SaSession tokenSession = StpUtil.getTokenSession();
  32. List<Long> roleIds = tokenSession.get(GlobalConstant.LOGIN_USER_ROLE_ID_KEY, new ArrayList<>());
  33. if (roleIds.size() == 0) {
  34. tokenSession = StpUtil.getSessionByLoginId(StpUtil.getLoginId());
  35. roleIds = tokenSession.get(GlobalConstant.LOGIN_USER_ROLE_ID_KEY, new ArrayList<>());
  36. }
  37. //非管理员需要进行权限验证
  38. // if (!roleIds.contains(GlobalConstant.SUPER_ADMIN_ROLE_ID)) {
  39. // if (!SaStrategy.me.hasElement.apply(tokenSession.get(GlobalConstant.LOGIN_USER_INTERFACE_AUTH_CODE_KEY, new ArrayList<>()), requestEntity.getApiInfo().getId())) {
  40. // return R.error(ResponseCode.MAGIC_API_UN_AUTHORIZED.getCode(), ResponseCode.MAGIC_API_UN_AUTHORIZED.getMessage());
  41. // }
  42. // }
  43. return null;
  44. }
  45. /**
  46. * 接口执行之后
  47. *
  48. * @param requestEntity
  49. * @param returnValue
  50. * @return
  51. */
  52. @Override
  53. public Object postHandle(RequestEntity requestEntity, Object returnValue) {
  54. log.info("{} 执行完毕,返回结果:{}", requestEntity.getApiInfo().getName(), returnValue);
  55. return null;
  56. }
  57. }