servlet实现简单的反向代理
项目基于Spring
须要的依赖为:
<dependency>
<groupId>org.mitre.dsmiley.httpproxy</groupId>
<artifactId>smiley-http-proxy-servlet</artifactId>
<version>1.7</version>
</dependency>
只需要添加一个java文件即可,原理有待继续研究
package *.*.*.*;
import org.mitre.dsmiley.httpproxy.ProxyServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* Created by czz on 2018/11/13.
*/
@Configuration
public class SolrProxyServletConfiguration {
@Bean
public ServletRegistrationBean servletRegistrationBean(){
ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new ProxyServlet(), "/s/*");
servletRegistrationBean.addInitParameter("targetUri", "http://www.baidu.com/s");
servletRegistrationBean.addInitParameter(ProxyServlet.P_LOG, "false");
return servletRegistrationBean;
}
}
还可以通过配置多个bean来达到代理多个服务的目的
package *.*.*.*;
import org.mitre.dsmiley.httpproxy.ProxyServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* Created by czz on 2018/11/13.
*/
@Configuration
public class SolrProxyServletConfiguration {
@Bean
public ServletRegistrationBean servletRegistrationBean(){
ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new ProxyServlet(), "/s/*");
servletRegistrationBean.setName("baidu");
servletRegistrationBean.addInitParameter("targetUri", "http://www.baidu.com/s");
servletRegistrationBean.addInitParameter(ProxyServlet.P_LOG, "false");
return servletRegistrationBean;
}
@Bean
public ServletRegistrationBean servletRegistrationBean1(){
ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new ProxyServlet(), "/bootstrap/*");
servletRegistrationBean.setName("runoob");
servletRegistrationBean.addInitParameter("targetUri", "http://www.runoob.com/bootstrap");
servletRegistrationBean.addInitParameter(ProxyServlet.P_LOG, "false");
return servletRegistrationBean;
}
@Bean
public ServletRegistrationBean servletRegistrationBean2(){
ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new ProxyServlet(), "/otheragent/*");
servletRegistrationBean.setName("***");
servletRegistrationBean.addInitParameter("targetUri", "http://***/otheragent");
servletRegistrationBean.addInitParameter(ProxyServlet.P_LOG, "false");
return servletRegistrationBean;
}
}
参考文章:Spring boot使用ProxyFilter进行服务代理
servlet实现简单的反向代理的更多相关文章
- 使用go语言实现简单的反向代理工具激活IntelliJ和PyCharm,持续更新
最近Jetbrians系列IDE更新至2017.3版本,激活检测机制也变成了动态封禁域名,导致大部分域名激活被屏蔽了,所以找了下资料,根据ilanyu的代码,改了下地址,实现了本地反向代理激活服务器. ...
- Nginx之最简单的反向代理机制分析
注:当前分析基于 Nginx之搭建反向代理实现tomcat分布式集群 的配置. 1. 用到的指令 下面介绍在上面的配置中用到的指令. upstream 指令 语法:upstream name { .. ...
- Nginx 简单配置反向代理
nginx 配置反向代理,转发请求到另一个域名 在server中加入 location /${alias} { proxy_buffer_size 128k; proxy_buffers 32 32k ...
- Nodejs实现简单的反向代理
var http = require('http'), httpProxy = require('http-proxy'); // 新建一个代理 Proxy Server 对象 var proxy = ...
- nginx简单实现反向代理和静态资源服务器
1修改hosts文件 127.0.0.1 www.test1.com 127.0.0.1 www.test2.com 127.0.0.1 static.com 2配置tomcat的server.xml ...
- nginx一个简单的反向代理设置
location /aaaaa/ { proxy_pass http://localhost:8080/aaaaa/; } 经过配置,现在访问 http://localhost/aaaaa/ 就会 ...
- IIS简单的反向代理设置
下载IIS扩展 1.URL Rewrite 地址: https://www.iis.net/downloads/microsoft/url-rewrite 2.Application Request ...
- nginx简单反向代理和负载均衡(ubuntu)
nginx简单反向代理与负载均衡 环境:三台ubuntu 12.04.5 虚拟机 均装有nginx 1.1.19 以下u1(192.168.240.129) ,u2(192.168.240.13 ...
- 腾讯云下安装 nodejs + 实现 Nginx 反向代理
本文将介绍如何给腾讯云上的 Ubuntu Server 12.04 LTS 64位主机安装 node 及 nginx,并简单配置反向代理. 笔者在整个安装过程中遇到不少麻烦(不赘述),如果你希望少踩坑 ...
随机推荐
- Manacher(hdu3068最长回文)
浅谈manacher算法 manacher算法是我在网上无意中找到的,主要是用来求某个字符串的最长回文子串. 不过网上的版本还不太成熟,我就修改了下. 不要被manacher这个名字吓倒了,其实man ...
- SpringBoot2.0 基础案例(05):多个拦截器配置和使用场景
一.拦截器简介 1.拦截器定义 拦截器,请求的接口被访问之前,进行拦截然后在之前或之后加入某些操作.拦截是AOP的一种实现策略. 拦截器主要用来按照指定规则拒绝请求. 2.拦截器中应用 Token令牌 ...
- 洛谷P2971 牛的政治Cow Politics
题目描述 Farmer John's cows are living on \(N (2 \leq N \leq 200,000)\)different pastures conveniently n ...
- Hive进阶_内置函数
Hive数学函数 round : 四舍五入 ceil : 向下取整 floor : 向上取整 ),),),),); Hive字符函数 select lower('Hello World'), uppe ...
- Linux用脚本守护进程
while true; do server=`ps -aux | grep tomcat | grep -v grep` if [ ! "$server" ]; then echo ...
- java lombok包在maven已经配置,但是注解没用
如果你是用eclipse作为开发环境,配置了maven依赖以后,还需要在eclipse/myeclipse中手动安装lombok. 其实就是加一个jar包,添加2行代码 1. 将 lombok.jar ...
- c#线程倒计时器源码
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...
- CA、公钥、私钥 概要
CA.公钥.私钥 概要 现在在开发中遇到一个需求,需要使用tls加密技术,之前并没有了解过,这里来做一个关于CA,公钥,密钥的总结,至于怎么生成这儿就不讲了,如果有机会可以再开一个单章来讲一下. 现在 ...
- 如何在CSS中解决长英文单词的页面显示问题?CSS3
简言 在页面排版中,经常遇到长英文单词溢出段落容器的情况,如何解决该问题?现编制如下对比演示程序: 演示程序 42du.cn-在线演示程序 部分html代码 <div class="b ...
- 文件操作函数及光标,tell,truncate
一丶文件基本操作 1.打开文件 open(文件名(路径),mode = "?",encoding = "字符集") 2.文件路径: 1.绝对路径,从磁盘的根目录 ...