关键词 @Aspect,@Component,@Pointcut,@Around
@Aspect
@Component
public class LoginInterceptor {
@Autowired
private ApplicationLoginKey applicationLoginKey;
@Autowired
private KeyLogin keyLogin;
@Autowired
private TestKey testKey;
/**
* 定义切入点,com.lgy.learn.demo路径下且有RequestMapping的进行拦截
*/
@Pointcut("execution(* com.lgy.learn.demo..*(..)) && @annotation(org.springframework.web.bind.annotation.RequestMapping)")
public void controllerMethodPointcut() {
}
/**
* 使用环绕方式进行执行,拦截器具体实现
*
* @param pjp
* @return
* @throws Throwable
*/
@Around("controllerMethodPointcut()")
public Object Interceptor(ProceedingJoinPoint pjp) throws Throwable {
/**
* 不同方式获取到的配置文件中的值
*/
String key = applicationLoginKey.getApplicationKeyLogin();
// String key2 = keyLogin.getApplicationKeyLogin();
// String testKey1 = testKey.getApplicationKeyLogin();
//将获取到的配置文件中的值转成List<String>
List<String> keys = Arrays.asList(key.split(","));
//获取请求信息并判断是否包含有配置文件中的值,如果没有,直接返回,否则继续执行
RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
ServletRequestAttributes request = (ServletRequestAttributes) attributes;
String resKey= request.getRequest().getParameter("key");
if(!keys.contains(resKey)){
return new ResponseResult(ResultCode.PROMISS_CODE);
}
//pjp.proceed();
return pjp.proceed();
}
}