error at ::0 formal unbound in pointcut 以及切面返回JSON数据

使用Spring AOP 报错

在使用切面的时候,启动报错:error at ::0 formal unbound in pointcut 

原因在于

@Before(value = "pointCut()")
	@ResponseBody
	public void doBefore(JoinPoint joinPoint,Exception e) {
		handleLog(joinPoint,e);
	}

在上述代码中,@Before注解不是针对异常处理的,所以需要去除Exception e参数。重启成功解决

修改后的代码:

package com.pinyu.miniprogram.global.aspect;

import java.io.IOException;
import java.lang.reflect.Method;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpOutputMessage;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageNotWritableException;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.http.server.ServletServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pinyu.miniprogram.config.SpringBeanTool;
import com.pinyu.miniprogram.config.redis.RedisUtils;
import com.pinyu.miniprogram.global.ann.RequireToken;
import com.pinyu.miniprogram.utils.RequestUtils;
import com.pinyu.miniprogram.utils.Token;
import com.pinyu.miniprogram.utils.result.JsonMsg;

@Aspect
@Component("tokenAspect")
public class TokenAspect {
	
	@Resource
    private MappingJackson2HttpMessageConverter converter;
	@Autowired
	private SpringBeanTool springBeanTool;
	@Autowired
	private RedisUtils redisUtils;

	private static final Logger log = LoggerFactory.getLogger(TokenAspect.class);

	// 配置织入点
	@Pointcut("@annotation(com.pinyu.miniprogram.global.ann.RequireToken)")
	public void pointCut() {
	}

	/**
	 * 拦截token
	 * @param joinPoint
	 * @param e
	 */
	@Before(value = "pointCut()")
	@ResponseBody
	public void doBefore(JoinPoint joinPoint) {
		handleLog(joinPoint);
	}

	private void handleLog(JoinPoint joinPoint) {
		    // 获得注解
//			RequireToken tokenAnn = getAnnotationLog(joinPoint);
//		    if (tokenAnn == null) {
//		    	return;
//		    }
		    HttpServletRequest request = springBeanTool.getRequest();
		    String token = RequestUtils.getHeader(request, "token");
		    Double score = redisUtils.score(Token.MEMBER_TOKEN,token);
		    if(score==null){
		        HttpServletResponse response = springBeanTool.getResponse();
		        HttpOutputMessage outputMessage = new ServletServerHttpResponse(response);
		        try {
					converter.write(JsonMsg.Error("token失效,需要重新登录"), MediaType.APPLICATION_JSON, outputMessage);
					shutdownResponse(response);
				} catch (Exception e) {
					e.printStackTrace();
				}
		    }
	}
	/**
	 * 是否存在注解,如果存在就获取
	 */
	private static RequireToken getAnnotationLog(JoinPoint joinPoint) {
		Signature signature = joinPoint.getSignature();
		MethodSignature methodSignature = (MethodSignature) signature;
		Method method = methodSignature.getMethod();
		if (method != null) {
			return method.getAnnotation(RequireToken.class);
		}
		return null;
	}
	
	private void shutdownResponse(HttpServletResponse response) throws IOException{
        response.getOutputStream().close();
    }

}

 

你可能感兴趣的:(java,spring,Spring,Boot)