package com.my.blogcenter.aop;

import com.my.blogcenter.exception.AuthException;
import com.my.blogcenter.utils.JwtOperator;
import io.jsonwebtoken.Claims;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
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;
import java.lang.reflect.Method;
import java.util.Objects;


@Aspect
@Component
public class AuthAspect {

    @Autowired
    private JwtOperator operator;

    @Around("@annotation(com.my.blogcenter.aop.CheckLogin)")
    public Object checkLogin(ProceedingJoinPoint point) throws Throwable {
        checkToken();
        return point.proceed();

    }

    @Around("@annotation(com.my.blogcenter.aop.CheckAdminAspect)")
    public Object checkAdmin(ProceedingJoinPoint point) {
        try {
            this.checkToken();
            HttpServletRequest request = this.getRequest();
            int role = (Integer) request.getAttribute("role");
            MethodSignature signature = (MethodSignature) point.getSignature();
            Method method = signature.getMethod();
            CheckAdminAspect annotation = method.getAnnotation(CheckAdminAspect.class);
            //拿到注解的value
            int value = annotation.value();
            if (!Objects.equals(value, role)) {
                throw new AuthException("用户无权访问");
            }
            return point.proceed();
        } catch (Throwable throwable) {
//            throwable.printStackTrace();
            throw new AuthException("用户无权访问");
        }

    }

    private void checkToken() {
        try {
            HttpServletRequest request = this.getRequest();
            String token = request.getHeader("x-token");
            //校验token
            Boolean isOk = operator.validateToken(token);
            if (!isOk) {
                throw new AuthException("token不合法");
            }
            //将用户信息存入header
            Claims claimsFromToken = operator.getClaimsFromToken(token);
            request.setAttribute("userId", claimsFromToken.get("userId"));
            request.setAttribute("nickName", claimsFromToken.get("nickName"));
            request.setAttribute("role", claimsFromToken.get("role"));
        } catch (Throwable throwable) {
            throw new AuthException("token不合法");
        }

    }

    private HttpServletRequest getRequest() {
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) requestAttributes;
        HttpServletRequest request = servletRequestAttributes.getRequest();
        return request;
    }
}
最近下载更多
lt33333  LV7 2023年6月5日
lipiao161  LV15 2023年4月27日
352601466  LV2 2022年9月20日
wangdengzhe  LV7 2022年5月24日
lsqazx  LV5 2022年2月11日
luowei45  LV1 2022年1月12日
pxqtsht  LV15 2021年11月3日
lyd19931203  LV21 2021年8月4日
lgh2010101  LV17 2021年7月12日
15997331343  LV7 2021年6月16日
最近浏览更多
唯易人生  LV3 2023年7月5日
644106  LV8 2023年6月8日
lt33333  LV7 2023年6月5日
微信网友_6115804675756032  LV7 2023年5月23日
随便取个名字_哈哈  LV27 2023年5月18日
流连瓦盖法  LV7 2023年4月28日
lipiao161  LV15 2023年4月27日
haishen  LV8 2023年2月23日
秦sir3067683450  LV10 2022年12月31日
  LV6 2022年12月5日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友