1、切面(Aspect)
是什么
切面,指切入目标方法,扩展目标方法功能,却不修改目标方法代码,将扩展功能代码从目标方法代码中分离出来。可以针对方法级拦截,并获得方法的参数和返回值,但获取不到http请求,多配合注解使用。
应用场景
• 权限校验
• 字段加密解密
怎么用
package com.dy.aspect;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
import java.util.Date;
/**
* this is a acpect
* 切入点
* 在那些方法上起作用
* 在什么时候起作用
*
* @author duoyuan
* @create 2019-12-20 20:52
**/
@Aspect
@Component
public classTimeAspect {
@Around("execution(* com.duoyuan.controller.UserController.*(..))")
public Object handleControllerMethod(ProceedingJoinPoint proceedingJoinPoint) throwsThrowable {
System.out.println("time aspect start");
Object[] args =proceedingJoinPoint.getArgs();
for(Object arg : args) {
System.out.println(arg.getClass().getName());
System.out.println("arg is " +arg);
}
long startTime = newDate().getTime();
Object obj =proceedingJoinPoint.proceed();
System.out.println("time aspect 耗时" + (new Date().getTime() -startTime));
System.out.println("time aspect end");
returnobj;
}
}
2、过滤器(Filter)
是什么
过滤器就是一个实现了特殊接口的Java类,实现对请求资源的过滤的功能。和框架无关,在所有过滤组件的最外层,颗粒度比较大。
应用场景
• 验证token合法性、有效性
怎么用
springboot中一般有两种配置方式:
(1)直接实现java.servlet.Filter接口
packagecom.dy.webFilter;
importorg.springframework.stereotype.Component;
import javax.servlet.*;
importjava.io.IOException;
importjava.util.Date;
@Component
public class TimerFilter implementsFilter {
@Override
public void init(FilterConfig filterConfig) throwsServletException {
System.out.println("Time filter init");
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throwsIOException, ServletException {
System.out.println("Time filter start");
long startTime = newDate().getTime();
filterChain.doFilter(servletRequest, servletResponse);
System.out.println("time filter:"+(new Date().getTime()-startTime));
System.out.println("time filter finish");
}
@Override
public voiddestroy() {
System.out.println("Time filter destroy");
}
}
(2)在WebConfig中配置,这种配置方式是因为使用第三方的Filter没有@Component注解。
packagecom.dy.config;
importcom.nbkj.webFilter.TimerFilter;
importorg.springframework.boot.web.servlet.FilterRegistrationBean;
importorg.springframework.context.annotation.Bean;
importorg.springframework.context.annotation.Configuration;
importjava.util.ArrayList;
importjava.util.List;
/**
* Web配置
*
* @author duoyuan
* @Configuration 这个注解声明这个类是配置类
* @create 2019-12-20 18:00
**/@Configuration
public classWebConfig {
@Bean
publicFilterRegistrationBean timeFilter() {
FilterRegistrationBean registrationBean = newFilterRegistrationBean();
TimerFilter timerFilter = newTimerFilter();
registrationBean.setFilter(timerFilter);
List urls = new ArrayList<>();
urls.add("/*");
registrationBean.setUrlPatterns(urls);
returnregistrationBean;
}
}
3、拦截器(Intereptor)
是什么
拦截器是在面向切面编程中应用的,就是在你的service或者一个方法前调用一个方法,或者在方法后调用一个方法。是基于JAVA的反射机制。拦截器不是在web.xml,比如struts在struts.xml中配置。可以获取被拦截的controller的方法,获取不到参数。
应用场景
• 执行安全检查
• 格式化请求头和主体
• 审查或者记录日志
• 根据请求内容授权或者限制用户访问
• 根据请求频率限制用户访问
怎么用
packagecom.dy.interceptor;
importorg.springframework.stereotype.Component;
importorg.springframework.web.method.HandlerMethod;
importorg.springframework.web.servlet.HandlerInterceptor;
importorg.springframework.web.servlet.ModelAndView;
importjavax.persistence.Convert;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
importjava.util.Date;
/**
* this is spring interceptor
*
* @author duoyuan
* @create 2019-12-24 18:16
**/@Component
public class TimeInterceptor implementsHandlerInterceptor {
/**
* 控制器方法处理之前
*
* @param httpServletRequest
* @param httpServletResponse
* @param handler
* @return
* @throws Exception
*/@Override
public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object handler) throwsException {
System.out.println("preHandle");
System.out.println(((HandlerMethod) handler).getBean().getClass().getName());
System.out.println(((HandlerMethod) handler).getMethod().getName());
httpServletRequest.setAttribute("startTime", newDate().getTime());
return false;
}
/**
* 控制器方法处理之后
* 控制器方法调用不抛异常调用
*
* @param httpServletRequest
* @param httpServletResponse
* @param o
* @param modelAndView
* @throws Exception
*/@Override
public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object handler, ModelAndView modelAndView) throwsException {
System.out.println("postHandle");
Long startTime = (Long) httpServletRequest.getAttribute("startTime");
System.out.println("time interceptor 耗时" + (new Date().getTime() -startTime));
}
/**
* 控制器方法抛不抛异常都会被调用
*
* @param httpServletRequest
* @param httpServletResponse
* @param o
* @param e
* @throws Exception
*/@Override
public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throwsException {
System.out.println("afterCompletion");
Long startTime = (Long) httpServletRequest.getAttribute("startTime");
System.out.println("time interceptor 耗时" + (new Date().getTime() -startTime));
System.out.println("ex is" +e);
}
}
packagecom.dy.config;
importcom.nbkj.interceptor.TimeInterceptor;
importcom.nbkj.webFilter.TimerFilter;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.boot.web.servlet.FilterRegistrationBean;
importorg.springframework.context.annotation.Bean;
importorg.springframework.context.annotation.Configuration;
importorg.springframework.web.servlet.config.annotation.InterceptorRegistry;
importorg.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
importjava.util.ArrayList;
importjava.util.List;
/**
* Web配置
*
* @author duoyuan
* @Configuration 这个注解声明这个类是配置类
* @create 2019-12-20 18:00
**/@Configuration
public class WebConfig extendsWebMvcConfigurerAdapter {
@Autowired
privateTimeInterceptor timeInterceptor;
@Override
public voidaddInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(timeInterceptor);
}
}
4、ControllerAdvice
是什么
controller的一个增强,最常用的是和@ExceptionHandler一起用来做全局异常。
应用场景
• 配合@ExceptionHandler做全局异常处理
• 配合@InitBinder对特殊请求参数做转换
• 配合@ModelAttribute使用
怎么用
packagecom.dy.handler;
importcom.wusong.order.common.Resp;
importcom.wusong.order.common.ServiceException;
importlombok.extern.slf4j.Slf4j;
importorg.springframework.web.bind.annotation.ControllerAdvice;
importorg.springframework.web.bind.annotation.ExceptionHandler;
importorg.springframework.web.bind.annotation.ResponseBody;
/**
* @description: 全局异常处理
* @author:duoyuan
* @create: 2019-12-16 下午1:45
**/@ControllerAdvice
@Slf4j
public classGlobalExceptionHandler {
/**
* @description: 业务异常统一处理
* @param e
* @return:
* @author:
* @Date: 2019-12-16 下午12:26
*/@ExceptionHandler(value = ServiceException.class)
@ResponseBody
publicObject serviceExceptionHandle(ServiceException e) {
log.error("异常{}", e);
Object obj =Resp.error(e);
returnobj;
}
}