自定义注解的基本实现

1、创建一个切面类:

package com.shanlin.car.annotation;

import com.shanlin.car.constant.MnnConstants;
import com.shanlin.car.exception.ShanlinCarException;
import com.shanlin.car.service.AppTokenService;
import com.shanlin.car.util.SpringContextHolder;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.Date;
import java.util.HashMap;
import java.util.Map;

@Aspect
@Component
public class TokenFilterAspect {

    private static final Logger LOGGER = LoggerFactory.getLogger(TokenFilterAspect.class);

    //定义切入点
    @Pointcut("@annotation(com.shanlin.jf.annotation.TokenFilter)")
    private void allMethod() { }

    @Around("allMethod()") //执行@接口
    public void exec(ProceedingJoinPoint joinPoint) throws Throwable {
        try {

            Object[] args = joinPoint.getArgs();
            HttpServletRequest request = null;
            for (int i = 0; i < args.length; i++) {
                if (args[i] instanceof HttpServletRequest) {
                    request = (HttpServletRequest) args[i];
                    break;
                }
            }
            if(null == request ){
                RequestAttributes ra = RequestContextHolder.getRequestAttributes();
                ServletRequestAttributes sra = (ServletRequestAttributes) ra;
                request = sra.getRequest();
            }

            String accessToken = request.getHeader("accessToken");
            String userId = request.getHeader("userId");
            LOGGER.info("TokenInterceptor----accessToken:"+accessToken+"---userId:"+userId);
            MethodSignature ms=(MethodSignature) joinPoint.getSignature();
            Method method=ms.getMethod();
            boolean flag = method.getAnnotation(TokenFilter.class).flag();
            if(flag){
                AppTokenService appTokenService = SpringContextHolder.getBean("appTokenServiceImpl");
                if(appTokenService.isValid(userId,accessToken)) {
                    joinPoint.proceed();
                }else {
                    Map<String,Object> map = new HashMap<>();
                    map.put("ret", "-1");
                    map.put("msg", "token已经失效");
                    map.put("ts", new Date().getTime()+"");
                    throw new ShanlinCarException(MnnConstants.USER_LOGIN_FAILURE.getCode(), MnnConstants.USER_LOGIN_FAILURE.getMsg(),map);
                }
            }else{
                joinPoint.proceed();
            }
        } catch (ShanlinCarException e) {
            throw e;
        }
    }
}
2、自定义注解
package com.shanlin.car.annotation;

import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;

import java.lang.annotation.*;

/**
 * Created by shanlin on 2018/3/30.
 */
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD,ElementType.TYPE})
@Documented
//最高优先级
@Order(Ordered.HIGHEST_PRECEDENCE)
public @interface TokenFilter {
    boolean flag() default false;
}

3、在配置文件中配置切面
<!-- 拦截过滤是否需要token -->
<bean id="tokenFilter" class="com.shanlin.car.annotation.TokenFilterAspect" />
<!-- Enables AspectJ -->
<aop:aspectj-autoproxy />

 


 

 


 

 

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值