java后台实现反向代理
背景
A平台登录,需要打通B平台
白话文:当A登录的之后,就默认B也登录了,但是不能做单点登录,因为不能修改B的代码,B
是开源的,但是有开源协议,所以B的登录不能动。B系统是嵌入在A系统的
解决思路
在A系统访问B系统的时候,在A系统添加一个反向代理配置,拦截所有访问B系统的路径,
判断系统A有没有登录,如果系统A登录了,则进行调用B系统的登录接看,然后设置cookie或者请求头信息
A系统没有登录,则直接返回未登录。
各种代理方法,参考以下网址:
https://www.codenong.com/cs106006432/
后端代码
java实现nginx配置
1、添加maven依赖
<dependency> <groupId>org.mitre.dsmiley.httpproxy</groupId> <artifactId>smiley-http-proxy-servlet</artifactId> <version>1.12.1</version> </dependency>
2:添加代理配置地址
proxy: solr: servlet_url: /* 拦截路径 target_url: http://target.com/ 代理地址
3:配置代理servlet,设置请求头等信息
package cn.jinka.gcdp.metacenter.config; import org.apache.http.HttpRequest; import org.apache.http.HttpResponse; import org.apache.http.RequestLine; import org.mitre.dsmiley.httpproxy.ProxyServlet; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.stereotype.Component; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @Component public class MyProxyServlet extends ProxyServlet { @Autowired private RedisUtils redisUtils;
@Override protected HttpResponse doExecute(HttpServletRequest servletRequest, HttpServletResponse servletResponse, HttpRequest proxyRequest) throws IOException { String tokens = redisUtils.get("tokens").toString();
//进行登录判断 if ("test22".equals(tokens)) { proxyRequest.setHeader("Authorization", ".B6AZDDtGLjnhEzbhTVS8EiVACwOXGcTvyl2eC3zxtfw"); } return super.doExecute(servletRequest, servletResponse, proxyRequest); } }
说明:
proxyRequest 标识的是代理的request,可以进行请求头等设置,其他为原有的请求servlet
4:配置代理信息

package cn.jinka.gcdp.metacenter.config; import org.mitre.dsmiley.httpproxy.ProxyServlet; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.context.properties.bind.BindResult; import org.springframework.boot.context.properties.bind.Binder; import org.springframework.boot.context.properties.source.ConfigurationPropertySources; import org.springframework.boot.web.servlet.ServletRegistrationBean; import org.springframework.context.EnvironmentAware; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.env.Environment; import java.util.Properties; @Configuration public class SolrProxyServletConfiguration implements EnvironmentAware { @Autowired MyProxyServlet myProxyServlet; @Bean public ServletRegistrationBean servletRegistrationBean() { Properties properties= (Properties) bindResult.get(); // 设置代理前缀 ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(myProxyServlet, properties.getProperty("servlet_url")); // 设置代理目标地址 servletRegistrationBean.addInitParameter(ProxyServlet.P_TARGET_URI, properties.getProperty("target_url")); // 设置是否打印日志 servletRegistrationBean.addInitParameter(ProxyServlet.P_LOG, properties.getProperty("logging_enabled", "false")); return servletRegistrationBean; } private BindResult bindResult; @Override public void setEnvironment(Environment environment) { Iterable sources = ConfigurationPropertySources.get(environment); Binder binder = new Binder(sources); BindResult bindResult = binder.bind("proxy.solr", Properties.class); this.bindResult = bindResult; } }
如果要使用多个代理可以设置多个bean,但是bean的名称必须设置不同:
servletRegistrationBean.setName(); 这样就可以配置多个bean了
ok完成以上操作之后访问所有的请求,都会被代理到了目标地址
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!