背景
A平台登录,需要打通B平台
白话文:当A登录的之后,就默认B也登录了,但是不能做单点登录,因为不能修改B的代码,B
是开源的,但是有开源协议,所以B的登录不能动。B系统是嵌入在A系统的
解决思路
在A系统访问B系统的时候,在A系统添加一个反向代理配置,拦截所有访问B系统的路径,
判断系统A有没有登录,如果系统A登录了,则进行调用B系统的登录接看,然后设置cookie或者请求头信息
A系统没有登录,则直接返回未登录。
后端代码
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://dataease.cm/ 代理地址
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", "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE2Nzc0OTY3OTYsInVzZXJJZCI6MSwidXNlcm5hbWUiOiJhZG1pbiJ9.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; } }View Code
ok完成以上操作之后访问所有的请求,都会被代理到了目标地址
标签:java,登录,springframework,反向,import,后台,org,servlet,properties From: https://www.cnblogs.com/xzlnuli/p/17166951.html