LoginCheckAspect.java 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package com.chuanghai.ihotel.aop;
  2. import com.chuanghai.ihotel.common.exception.BizCodeEnume;
  3. import com.chuanghai.ihotel.common.exception.RRException;
  4. import com.chuanghai.ihotel.util.JWTUtil;
  5. import com.chuanghai.ihotel.dto.LoginUserDTO;
  6. import io.jsonwebtoken.Claims;
  7. import org.aspectj.lang.ProceedingJoinPoint;
  8. import org.aspectj.lang.annotation.Around;
  9. import org.aspectj.lang.annotation.Aspect;
  10. import org.springframework.core.annotation.Order;
  11. import org.springframework.stereotype.Component;
  12. import org.springframework.util.StringUtils;
  13. import org.springframework.web.context.request.RequestAttributes;
  14. import org.springframework.web.context.request.RequestContextHolder;
  15. import org.springframework.web.context.request.ServletRequestAttributes;
  16. import javax.servlet.http.HttpServletRequest;
  17. /**
  18. * @Author: codingliang
  19. * @Description: 登录拦截
  20. * @Date: 2021-04-29 15:57
  21. * @Version: V1.0
  22. **/
  23. @Aspect
  24. @Component
  25. @Order(0)
  26. public class LoginCheckAspect {
  27. public static ThreadLocal<LoginUserDTO> threadLocal = new ThreadLocal<>();
  28. /**
  29. * 管理员登录拦截
  30. * @param point
  31. * @return
  32. * @throws Throwable
  33. */
  34. @Around("@annotation(com.chuanghai.ihotel.anno.AdminLoginCheck)")
  35. public Object adminLoginCheck(ProceedingJoinPoint point) throws Throwable {
  36. Claims claims = validateToken("admin_token");
  37. Object adminIdStr = claims.get("admin_id");
  38. if (adminIdStr == null) {
  39. throw new RRException(BizCodeEnume.TOKEN_INVALID);
  40. }
  41. Long adminId = Long.valueOf((String) adminIdStr);
  42. LoginUserDTO loginUserVO = LoginUserDTO.builder().adminId(adminId).build();
  43. Object proceed;
  44. try {
  45. threadLocal.set(loginUserVO);
  46. proceed = point.proceed();
  47. } finally {
  48. threadLocal.remove();
  49. }
  50. return proceed;
  51. }
  52. /**
  53. * 普通用户登录拦截
  54. * @param point
  55. * @return
  56. * @throws Throwable
  57. */
  58. @Around("@annotation(com.chuanghai.ihotel.anno.UserLoginCheck)")
  59. public Object userLoginCheck(ProceedingJoinPoint point) throws Throwable {
  60. // 校验token
  61. Claims claims = validateToken("user_token");
  62. String cardNumber = (String) claims.get("card_number");
  63. String identityType = (String) claims.get("identity_type");
  64. if (!StringUtils.hasText(cardNumber)) {
  65. throw new RRException(BizCodeEnume.TOKEN_INVALID);
  66. }
  67. LoginUserDTO loginUserVO = LoginUserDTO.builder().cardNumber(cardNumber).identityType(identityType).build();
  68. Object proceed;
  69. try {
  70. threadLocal.set(loginUserVO);
  71. proceed = point.proceed();
  72. } finally {
  73. threadLocal.remove();
  74. }
  75. return proceed;
  76. }
  77. // 检验token
  78. private Claims validateToken(String headName) {
  79. HttpServletRequest request = getHttpServletRequest();
  80. String token = request.getHeader(headName);
  81. // 验证userToken
  82. if (!StringUtils.hasText(token)) {
  83. throw new RRException(BizCodeEnume.TOKEN_IS_EMPTY);
  84. }
  85. // 解析token
  86. Claims claims = JWTUtil.checkJWT(token);
  87. if (claims == null) { // 未登录
  88. throw new RRException(BizCodeEnume.TOKEN_INVALID);
  89. }
  90. return claims;
  91. }
  92. // 获取request
  93. private HttpServletRequest getHttpServletRequest() {
  94. RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
  95. ServletRequestAttributes attributes = (ServletRequestAttributes) requestAttributes;
  96. return attributes.getRequest();
  97. }
  98. }