1.声名注解
package com.eternalray.conf;
import java.lang.annotation.*;
/**
* 安全认证
* @author EternalRay
*/
@Target({ElementType.TYPE,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Authorized {
}
2.定义切面
package com.eternalray.conf;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.lang.annotation.Annotation;
/**
* 安全切面认证配置
* @author EternalRay
*/
@Aspect
@Order(0)
@Component
@Slf4j
public class AuthorizedAspectConf {
/**
* 请求头认证字段
*/
private static final String HEAD_AUTHORIZATION="Authorization";
/**
* 请求切点方法(已提供@RequestMapping,@GetMapping,@PostMapping注解,需要其它请增加)
*/
@Pointcut(" @annotation(org.springframework.web.bind.annotation.RequestMapping) || " +
" @annotation(org.springframework.web.bind.ann

本文介绍了如何在SpringBoot应用中使用AOP实现认证token的处理,包括声明注解、定义切面、控制器层的设置,以及POM配置,并提供了全局异常处理的类代码链接,部分内容参考自相关博客链接。
最低0.47元/天 解锁文章
860

被折叠的 条评论
为什么被折叠?



