java拦截url请求参数_Java实现拦截HTTP请求的⼏种⽅式展开全部
在Java的服务端开发当中e5a48de588b662616964757a686964616f31333365656530,是很常见的业务场景,这⾥对Java开发当中⼏种常见的的实现⽅式进⾏记录和分析。案例说明基于Spring Boot环境。
⼀:实现javax.servlet.Filter接⼝(使⽤过滤器⽅式拦截请求)
import org.springframework.stereotype.Component;import javax.servlet.*;import java.io.IOException;import
java.util.Date;@Componentpublic class TimeInterceptor implements Filter {@Overridepublic void init(FilterConfig filterConfig) throws ServletException {System.out.println("time filter init");}@Overridepublic void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException
{System.out.println("time filter start");long start = new Date().getTime();filterChain.doFilter(servletRequest, servletResponse);System.out.println("time filter 耗时:"+(new Date().getTime()-start));System.out.println("time filter finish");}@Overridepublic void destroy() {System.out.println("time filter destroy");}}
如使⽤@Compent注解声明不需要加⼊其它配置即可使得⽣效,但是默认拦截/*,会拦截所有请求。
⼆:使⽤@Bean注⼊⾃定义,依然上⾯的代码,去掉@Compent注解,创建TimeWebConfig配置类:
import org.springframework.boot.web.servlet.FilterRegistrationBean;import
t.annotation.Bean;import t.annotation.Configuration;import
java.util.ArrayList;import java.util.List;@Configurationpublic class TimeWebConfig {@Beanpublic FilterRegistrationBean timeFilter(){FilterRegistrationBean registrationBean = new FilterRegistrationBean();TimeInterceptor interceptor = new TimeInterceptor();registrationBean.setFilter(interceptor);List urls = new ArrayList<>
();urls.add("/user/*");registrationBean.setUrlPatterns(urls);return registrationBean;}}
上⾯这两种拦截请求的实现是基于JavaEE提供的Filter接⼝实现的,缺点在于,该实际上是⼀个过滤器,执⾏代码的⽅法doFilter只提供了request,response等参数,当请求进⼊被过滤器拦截的时
候,我们并不知道这个请求是由哪个控制器的哪个⽅法来执⾏的。
三:使⽤springMVC提供的,实现org.springframework.web.servlet.HandlerInterceptor接⼝:
创建⾃定义的:
import org.springframework.stereotype.Component;import org.hod.HandlerMethod;import
org.springframework.web.servlet.HandlerInterceptor;import org.springframework.web.servlet.ModelAndView;import
javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.util.Date;@Componentpublic class MyInterceptor implements HandlerInterceptor {@Overridepublic boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object handler) throws Exception
{System.out.println("preHandler");System.out.println(((HandlerMethod)
handler).getBean().getClass().getName());System.out.println(((HandlerMethod)
handler).getMethod().getName());httpServletRequest.setAttribute("start", new Date().getTime());return true;}@Overridepublic void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {System.out.println("postHandler");Long start = (Long) Attribute("start");System.out.println("time interceptor 耗时:"+(new Date().getTime()-
springboot aopstart));}@Overridepublic void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {System.out.println("afterCompletion");Long start = (Long) Attribute("start");System.out.println("time interceptor 耗时:"+(new Date().getTime()-
start));System.out.println("ex is:"+e);}}
创建配置类:
import org.springframework.beans.factory.annotation.Autowired;import
t.annotation.Configuration;import
org.springframework.fig.annotation.InterceptorRegistry;import
org.springframework.fig.annotation.WebMvcConfigurerAdapter;@Configurationpublic class WebConfig extends WebMvcConfigurerAdapter {@Autowiredprivate MyInterceptor interceptor;@Overridepublic void
addInterceptors(InterceptorRegistry registry)
{registry.addInterceptor(interceptor).addPathPatterns("/user/*").excludePathPatterns("/blog/*");}}
此种⽅式的当中我们能够获取拦截的请求对应的类和⽅法的相关信息,缺点在于该handler对象⽆法获取具体执⾏⽅法的参数信息。
四:利⽤Spring的切⾯(AOP)实现:
引⼊jar包:
org.springframework.bootspring-boot-starter-aop
创建切⽚类:
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;@Aspect@Componentpublic class TimeAspect {@Around("execution(* ller.UserController.* (..))")public Object handlerControllerMethod(ProceedingJoinPoint point) throws Throwable {System.out.println("time aspect start");long start = new Date().getTime();Object[] args = Args();for (Object obj : args) {System.out.println("arg is:"+obj);}Object obj = point.proceed();//具体⽅法的返回值System.out.println("aspect 耗时:"+(new Date().getTime()-start));System.out.println("time aspect end");return obj;}}
aspectj基于AOP实现的功能⼗分强⼤,具体详解请参考spring官⽹⽹站的⽂档。
本回答由⽹友推荐
已赞过
已踩过<
你对这个回答的评价是?
评论
收起

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。