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.bi