文章内容
AOP切面获取自定义参数注解:@Target(ElementType.PARAMETER)
1、创建自定义参数注解
1 2 3 4 5 6 7 8 9 | import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Target (ElementType.PARAMETER) @Retention (RetentionPolicy.RUNTIME) public @interface User { } |
这里@Target中的类型用的是PARAMETER,因为我这里的注解要用到参数中。比如:
1 2 3 4 | @GetMapping ( "/test" ) public R myCustomProduct( @User UserVo userVo) { return R.ok(userVo); } |
2、编写自定义注解解析器
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.MethodParameter; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Component; import org.springframework.web.bind.support.WebDataBinderFactory; import org.springframework.web.context.request.NativeWebRequest; import org.springframework.web.context.request.RequestAttributes; import org.springframework.web.method.support.HandlerMethodArgumentResolver; import org.springframework.web.method.support.ModelAndViewContainer; @Component public class UserHandlerMethodArgumentResolver implements HandlerMethodArgumentResolver { @Autowired RedisTemplate<String, Object> redisTemplate; @Override public boolean supportsParameter(MethodParameter methodParameter) { // return methodParameter.getParameterType().isAssignableFrom(UserVo.class) && methodParameter.hasParameterAnnotation(User.class); return methodParameter.hasParameterAnnotation(User. class ); } @Override public Object resolveArgument(MethodParameter methodParameter, ModelAndViewContainer modelAndViewContainer, NativeWebRequest nativeWebRequest, WebDataBinderFactory webDataBinderFactory) throws Exception { //获取用户信息 String openId = nativeWebRequest.getHeader( "uuid" ); UserVo user = (UserVo) redisTemplate.opsForValue().get(RedsKeyConstant.SESSION_USER_UUID+openId); if (user== null ||openId== null ) { return null ; } return user; } } |
3、实现WebMvcConfigurer接口,重写addArgumentResolvers方法配置解析器
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 | import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.web.method.support.HandlerMethodArgumentResolver; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import java.util.List; @Configuration public class WebMvcConfig implements WebMvcConfigurer { @Autowired private UserHandlerMethodArgumentResolver userHandlerMethodArgumentResolver; @Override public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) { argumentResolvers.add(userHandlerMethodArgumentResolver); } } |
4、创建切面
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.Signature; import org.aspectj.lang.annotation.*; import org.aspectj.lang.reflect.MethodSignature; import org.springframework.stereotype.Component;; import java.lang.annotation.Annotation; import java.lang.reflect.Method; @Aspect @Component public class LoginUserTokenAspect { @Around ( "within(com.ntan520.controller.PubController) || within(com.ntan520.controller.HomeController) " ) public Object run(ProceedingJoinPoint joinPoint) throws Throwable{ // System.out.println("aspect is run!!"); Object[] args = joinPoint.getArgs(); // 方法签名 Signature signature1 = joinPoint.getSignature(); // 获取的是代理类的method对象 Method method1 = ( (MethodSignature)signature1 ).getMethod(); // 这个方法才是目标对象上有注解的方法 Method realMethod = joinPoint.getTarget().getClass().getDeclaredMethod(signature1.getName(), method1.getParameterTypes()); // 取出对应的注解 String name = "" ; int num = - 1 ; Annotation[][] parameterAnnotations = realMethod.getParameterAnnotations(); A: for (Annotation[] annotations : parameterAnnotations) { for (Annotation annotation : annotations) { //获取注解名 name = annotation.annotationType().getSimpleName(); num = num + 1 ; if (name.equals( "User" )){ break A; } } } //获取注解 //User user = method.getAnnotation(User.class); if (name.equals( "User" )){ UserVo userVo = (UserVo)args[num]; if (userVo == null || userVo.getId() == null ){ //未登录需要告诉用户你未登录 return R.failed( "401" , "请重新获取用户信息!" ); } else { //已经登录的继续处理业务 return joinPoint.proceed(args); } } else { //没有这个注解的不验证登录,继续处理业务 return joinPoint.proceed(args); } } } |