spring boot使用 * 修改请求URL域名 换 IP 访问的方法
作者:OAK社区 发布时间:2022-08-21 20:15:53
Interceptor 介绍
* (Interceptor)同 Filter 过滤器一样,它俩都是面向切面编程——AOP 的具体实现(AOP切面编程只是一种编程思想而已)。
你可以使用 Interceptor 来执行某些任务,例如在 Controller 处理请求之前编写日志,添加或更新配置…
在 Spring中,当请求发送到 Controller 时,在被Controller处理之前,它必须经过 Interceptors(0或多个)。
Spring Interceptor是一个非常类似于Servlet Filter 的概念 。
Interceptor 作用
日志记录:记录请求信息的日志,以便进行信息监控、信息统计、计算 PV(Page View)等;
权限检查:如登录检测,进入处理器检测是否登录;
性能监控:通过 * 在进入处理器之前记录开始时间,在处理完后记录结束时间,从而得到该请求的处理时间。(反向代理,如 Apache 也可以自动记录)
通用行为:读取 Cookie 得到用户信息并将用户对象放入请求,从而方便后续流程使用,还有如提取 Locale、Theme 信息等,只要是多个处理器都需要的即可使用 * 实现。
自定义 Interceptor
如果你需要自定义 Interceptor 的话必须实现 org.springframework.web.servlet.HandlerInterceptor接口或继承 org.springframework.web.servlet.handler.HandlerInterceptorAdapter类,并且需要重写下面下面 3 个方法:
preHandler(HttpServletRequest request, HttpServletResponse response, Object handler) 方法在请求处理之前被调用。该方法在 Interceptor 类中最先执行,用来进行一些前置初始化操作或是对当前请求做预处理,也可以进行一些判断来决定请求是否要继续进行下去。该方法的返回至是 Boolean 类型,当它返回 false 时,表示请求结束,后续的 Interceptor 和 Controller 都不会再执行;当它返回为 true 时会继续调用下一个 Interceptor 的 preHandle 方法,如果已经是最后一个 Interceptor 的时候就会调用当前请求的 Controller 方法。
postHandler(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) 方法在当前请求处理完成之后,也就是 Controller 方法调用之后执行,但是它会在 DispatcherServlet 进行视图返回渲染之前被调用,所以我们可以在这个方法中对 Controller 处理之后的 ModelAndView 对象进行操作。
afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handle, Exception ex) 方法需要在当前对应的 Interceptor 类的 postHandler 方法返回值为 true 时才会执行。顾名思义,该方法将在整个请求结束之后,也就是在 DispatcherServlet 渲染了对应的视图之后执行。此方法主要用来进行资源清理。
接下来结合实际代码进行学习。
案例1 :域名换IP访问
package com.config;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.http.client.support.HttpRequestWrapper;
import org.springframework.stereotype.Component;
import org.springframework.web.util.UriComponentsBuilder;
import java.io.IOException;
import java.net.URI;
@Component
public class Interceptor implements ClientHttpRequestInterceptor {
/**
* Intercept the given request, and return a response. The given {@link ClientHttpRequestExecution} allows
* the interceptor to pass on the request and response to the next entity in the chain.
*
* <p>A typical implementation of this method would follow the following pattern:
* <ol>
* <li>Examine the {@linkplain HttpRequest request} and body</li>
* <li>Optionally {@linkplain HttpRequestWrapper wrap} the request to filter HTTP attributes.</li>
* <li>Optionally modify the body of the request.</li>
* <li><strong>Either</strong>
* <ul>
* <li>execute the request using {@link ClientHttpRequestExecution#execute(HttpRequest, byte[])},</li>
* <strong>or</strong>
* <li>do not execute the request to block the execution altogether.</li>
* </ul>
* <li>Optionally wrap the response to filter HTTP attributes.</li>
* </ol>
*
* @param request the request, containing method, URI, and headers
* @param body the body of the request
* @param execution the request execution
* @return the response
* @throws IOException in case of I/O errors
*/
@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
String str = request.getURI().toString();
String str1 = str.replace("https://baidu.com", "http://39.156.66.10:8080");
URI newUri = UriComponentsBuilder.fromUri(URI.create(str)).build().toUri();
return execution.execute(new UriModifyHttpRequestWrapper(request, newUri), body);
}
private static class UriModifyHttpRequestWrapper extends HttpRequestWrapper {
private final URI uri;
public UriModifyHttpRequestWrapper(HttpRequest request, URI uri) {
super(request);
this.uri = uri;
}
@Override
public URI getURI() {
return uri;
}
}
}
案例2: erverWebExchange通过 * 修改请求url
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain)
{
ServerHttpRequest str = exchange.getRequest();
//新url
String newPath ="/system/loanOrg/list";
ServerHttpRequest newRequest = str.mutate().path(newPath).build();
exchange.getAttributes().put("path", newRequest.getURI());
return chain.filter(exchange.mutate() .request(newRequest).build());
}
案例3: 将请求路径中/idea都去掉
1.定义 *
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
@Component
public class GlobalInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
HttpServletResponseWrapper httpResponse = new HttpServletResponseWrapper((HttpServletResponse) response);
System.out.println(request.getRequestURI());
String path=request.getRequestURI();
if(path.indexOf("/idea")>-1){
path = path.replaceAll("/idea","");
request.getRequestDispatcher(path).forward(request,response);
}
return true;
}
}
2.定义WebMvcConfig
import com.GlobalInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
@Autowired
GlobalInterceptor globalInterceptor;
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(globalInterceptor).addPathPatterns("/idea/**");
}
}
案例4: SpringBoot 利用过滤器Filter修改请求url地址
要求:
代码中配置的url路径为http://127.0.0.1/api/asso
现在要求http://127.0.0.1/asso 也可以同样访问同一个conroller下面的method,并且要求参数全部跟随
代码:
package com.framework.filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
/**
* 修改请求路由,当进入url为/a/b时,将其url修改为/api/a/b
*
**/
public class UrlFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void destroy() {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest httpRequest = (HttpServletRequest)request;
HttpServletResponseWrapper httpResponse = new HttpServletResponseWrapper((HttpServletResponse) response);
System.out.println(httpRequest.getRequestURI());
String path=httpRequest.getRequestURI();
if(path.indexOf("/api/")<0){
path="/api"+path;
System.out.println(path);
httpRequest.getRequestDispatcher(path).forward(request,response);
}
else {
chain.doFilter(request,response);
}
return;
}
}
这个类必须继承Filter类,这个是Servlet的规范。有了过滤器类以后,以前的web项目可以在web.xml中进行配置,但是spring boot项目并没有web.xml这个文件,那怎么配置?在Spring boot中,我们需要FilterRegistrationBean来完成配置。
其实现过程如下:
package com.shitou.huishi.framework.filter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* Created by qhong on 2018/5/16 15:28
**/
@Configuration
public class FilterConfig {
@Bean
public FilterRegistrationBean registFilter() {
FilterRegistrationBean registration = new FilterRegistrationBean();
registration.setFilter(new UrlFilter());
registration.addUrlPatterns("/*");
registration.setName("UrlFilter");
registration.setOrder(1);
return registration;
}
}
案例5. * : WebMvcConfigurerAdapter *
拦截所有请求
@Configuration
public class CustMvcConfigurerAdapter extends WebMvcConfigurerAdapter {
@Autowired
private CustInterceptor custInterceptor;
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(custInterceptor).addPathPatterns("/**");
}
}
排除指定路径
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(custInterceptor).addPathPatterns("/**").excludePathPatterns("/select/**");
}
拦截指定路径
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(custInterceptor).addPathPatterns("/user/**");
}
CustInterceptor具体拦截类
@Component
public class CustInterceptor extends HandlerInterceptorAdapter {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
boolean needLogin = needLogin(request);
if (!needLogin) {
return true;
}
boolean isLogin = checkLogin(request, response);
return isLogin;
}
}
来源:https://libin9ioak.blog.csdn.net/article/details/126891064


猜你喜欢
- Android MediaPlayer实现音乐播放器1、布局文件<?xml version="1.0" encod
- 快速排序使用分治法(Divide and conquer)策略来把一个串行(list)分为两个子串行(sub-lists)。步骤为:1.从数
- 首先 函数指针是指向一组同类型的函数的指针;而类成员函数我们也可以相似的认为,它是指向同类中同一组类型的成员函数的指针,当然这里的成员函数更
- 在Android上,不止一个途径来侦听用户和应用程序之间交互的事件。对于用户界面里的事件,侦听方法就是从与用户交互的特定视图对象截获这些事件
- 0. Grinder – Grinder是一个开源的JVM负载测试框架,它通过很多负载注射器来为分布式测试提
- 这篇文章主要介绍了Jenkins Host key verification failed问题解决,文中通过示例代码介绍的非常详细,对大家的
- 个人觉得eclipse提示较少,所以今天就写了这篇文章。使用Intellij IDEA 2018实现Hello,World的时候我遇到了一些
- 本章先讲解Java随机数的几种产生方式,然后通过示例对其进行演示。广义上讲,Java中的随机数的有三种产生方式:(01). 通过System
- 看完了前面说的几本书
- 上一篇文章实现了微信登录的移动端功能,下面继续完善功能,实现微信登录服务端功能服务端登录功能实现在以往文章里已经实现了服务端mvc框架,而登
- 发现问题最近发现在mybatis中如果使用的字段是Oracle的关键字,会出现错误,通过查找相关的资料终于解决了,下面来一起看看详细的解决方
- 本文实例为大家分享了java制作简单验证码的具体代码,供大家参考,具体内容如下在这里我们需要用到java的画笔工具,所以我们需要导入以下包i
- 1、maven是什么,为什么存在?项目结构是什么样子,怎么定位jar官方网站说了好多,整的多复杂一样,简单说:maven是一个管理包的工具。
- 目录1、一个抽象类并不需要其中所有的方法都是抽象的。( )2、下列程序的运行结果3、在Java中,关于HashMap类的描述,以下错误的是(
- 本文实例为大家分享了Java使用poi操作excel的具体代码,供大家参考,具体内容如下依赖poi的jar包,pom.xml配置如下:<
- 本文实例为大家分享了Android实现歌词滚动效果的具体代码,供大家参考,具体内容如下自定义TextViewpublic class Ver
- FFmpeg是一个开源免费跨平台的视频和音频流方案,属于自由软件,采用LGPL或GPL许可证(依据你选择的组件)。它提供了录制、转换以及流化
- 实践仿照@EnableEurekaServer实现自动装配如果你使用过Eureka作为微服务的注册中心,那么对@EnableWebConfi
- 前言在一些日常业务中,会遇到一些琐碎文件需要统一打包到一个压缩包中上传,业务方在后台接收到压缩包后自行解压,然后解析相应文件。而且可能涉及安
- 本文实例讲述了Android编程实现等比例显示图片的方法。分享给大家供大家参考,具体如下:在android中,由于密度的影响,如果想得到图片