package com.chuanghai.ihotel.aop; import com.chuanghai.ihotel.common.exception.BizCodeEnume; import com.chuanghai.ihotel.common.exception.RRException; import com.chuanghai.ihotel.util.JWTUtil; import com.chuanghai.ihotel.dto.LoginUserDTO; import io.jsonwebtoken.Claims; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component; import org.springframework.util.StringUtils; import org.springframework.web.context.request.RequestAttributes; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import javax.servlet.http.HttpServletRequest; /** * @Author: codingliang * @Description: 登录拦截 * @Date: 2021-04-29 15:57 * @Version: V1.0 **/ @Aspect @Component @Order(0) public class LoginCheckAspect { public static ThreadLocal threadLocal = new ThreadLocal<>(); /** * 管理员登录拦截 * @param point * @return * @throws Throwable */ @Around("@annotation(com.chuanghai.ihotel.anno.AdminLoginCheck)") public Object adminLoginCheck(ProceedingJoinPoint point) throws Throwable { Claims claims = validateToken("admin_token"); Object adminIdStr = claims.get("admin_id"); if (adminIdStr == null) { throw new RRException(BizCodeEnume.TOKEN_INVALID); } Long adminId = Long.valueOf((String) adminIdStr); LoginUserDTO loginUserVO = LoginUserDTO.builder().adminId(adminId).build(); Object proceed; try { threadLocal.set(loginUserVO); proceed = point.proceed(); } finally { threadLocal.remove(); } return proceed; } /** * 普通用户登录拦截 * @param point * @return * @throws Throwable */ @Around("@annotation(com.chuanghai.ihotel.anno.UserLoginCheck)") public Object userLoginCheck(ProceedingJoinPoint point) throws Throwable { // 校验token Claims claims = validateToken("user_token"); String cardNumber = (String) claims.get("card_number"); String identityType = (String) claims.get("identity_type"); if (!StringUtils.hasText(cardNumber)) { throw new RRException(BizCodeEnume.TOKEN_INVALID); } LoginUserDTO loginUserVO = LoginUserDTO.builder().cardNumber(cardNumber).identityType(identityType).build(); Object proceed; try { threadLocal.set(loginUserVO); proceed = point.proceed(); } finally { threadLocal.remove(); } return proceed; } // 检验token private Claims validateToken(String headName) { HttpServletRequest request = getHttpServletRequest(); String token = request.getHeader(headName); // 验证userToken if (!StringUtils.hasText(token)) { throw new RRException(BizCodeEnume.TOKEN_IS_EMPTY); } // 解析token Claims claims = JWTUtil.checkJWT(token); if (claims == null) { // 未登录 throw new RRException(BizCodeEnume.TOKEN_INVALID); } return claims; } // 获取request private HttpServletRequest getHttpServletRequest() { RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes(); ServletRequestAttributes attributes = (ServletRequestAttributes) requestAttributes; return attributes.getRequest(); } }