项目中反向代理 集成第三方的服务接口或web监控界面,并实现与自身项目相结合的鉴权方法
2.0 版开始,代理切换到jakarta servlet-api
<!--HTTP 代理 Servlet-->
<dependency>
<groupId>org.mitre.dsmiley.httpproxy</groupId>
<artifactId>smiley-http-proxy-servlet</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>org.mitre.dsmiley.httpproxy</groupId>
<artifactId>smiley-http-proxy-servlet</artifactId>
<version>${smiley-http-proxy-servlet.version}</version>
<classifier>javax</classifier>
</dependency>
仅仅是接口代理默认官网示例使用即可,参考第二个接口代理。
如果是完整的web监控服务 会出现 静态资源 因路径 加载错误。
import org.mitre.dsmiley.httpproxy.ProxyServlet;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.filter.HiddenHttpMethodFilter;
/**
* 本地代理服务
*
* @author Smile
*/
@Configuration
public class ProxyServletConfig {
/**
* 代理Grafana 监控平台
*/
@Bean
public ServletRegistrationBean<ProxyServlet> servletRegistrationBean() {
ServletRegistrationBean<ProxyServlet> servletRegistrationBean = new ServletRegistrationBean<>(new ProxyServlet(), "/grafana/*");
servletRegistrationBean.addInitParameter(ProxyServlet.P_TARGET_URI, "http://127.0.0.1:9999");
servletRegistrationBean.addInitParameter(ProxyServlet.P_LOG, "true");
// 自动处理重定向
servletRegistrationBean.addInitParameter(ProxyServlet.P_HANDLEREDIRECTS, "false");
// 保持 COOKIES 不变
servletRegistrationBean.addInitParameter(ProxyServlet.P_PRESERVECOOKIES, "true");
// Set-Cookie 服务器响应标头中保持 cookie 路径不变
servletRegistrationBean.addInitParameter(ProxyServlet.P_PRESERVECOOKIEPATH, "true");
// 保持 HOST 参数不变
servletRegistrationBean.addInitParameter(ProxyServlet.P_PRESERVEHOST, "true");
return servletRegistrationBean;
}
/**
*接口代理
*/
@Bean
public ServletRegistrationBean<ProxyServlet> servletRegistration() {
ServletRegistrationBean<ProxyServlet> servletRegistrationBean = new ServletRegistrationBean<>(new ProxyServlet(), "/one/*","/two/*","three/*");
servletRegistrationBean.addInitParameter(ProxyServlet.P_TARGET_URI, "http://localhost:8001/api");
servletRegistrationBean.addInitParameter(ProxyServlet.P_LOG, "true");
return servletRegistrationBean;
}
/**
* 禁用springboot 自带的 HiddenHttpMethodFilter 防止post提交的form数据流被提前消费
*
* fix springboot中使用proxyservlet的 bug.
* bugs
* bugs
*
* @return
*/
@Bean
public FilterRegistrationBean<HiddenHttpMethodFilter> disableHiddenHttpMethodFilter() {
FilterRegistrationBean<HiddenHttpMethodFilter> registrationBean = new FilterRegistrationBean<>();
registrationBean.setFilter(new HiddenHttpMethodFilter());
registrationBean.setEnabled(false); // 禁用过滤器
return registrationBean;
}
}
直接访问grafana代理 springboot项目端口8088报错,静态资源 路径不正确 导致加载失败
springboot 代理的/grafana/* 到 http://127.0.0.1:9999
静态资源的访问失败 404或 错误的返回html首页,是因为路径不符合此规则导致代理是失败
proxy_pass http://127.0.0.1:8088/grafana//;
由nginx代理到 // 则问题解决 ,使 /grafana/* 代理规则生效
其他访问的服务调用 nginx的这个代理
server {
listen 8889;
server_name localhost;
# grafana websocket地址代理
location /api/live/ws {
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header X-real-ip $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://127.0.0.1:9999;
}
location / {
#add_header Access-Control-Allow-Origin *;
add_header Access-Control-Max-Age 1728000;
add_header Access-Control-Allow-Methods 'POST,GET,OPTIONS,DELETE,PUT,HEAD,PATCH';
add_header Access-Control-Allow-Headers 'satoken,DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
add_header Access-Control-Allow-Origin $http_origin;
client_max_body_size 10m;
if ($request_method = 'OPTIONS') {
return 204;
}
# grafana支持配置apikey 免登录访问
set $auth 'Bearer eyJrIjoiN1pKYlk5akFDZWNoMlVSUEN1YllXdm0yd2VYN2RzZFIiLCJuIjoiYWRtaW5rZXkiLCJpZCI6MX0=';
# apiKey设置到header grafana免密访问
proxy_set_header Authorization $auth;
proxy_pass http://127.0.0.1:8088/grafana//;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_connect_timeout 600;
proxy_read_timeout 600;
}
}
import cn.dev33.satoken.stp.StpUtil;
import jakarta.servlet.*;
import jakarta.servlet.annotation.WebFilter;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.core.annotation.Order;
import java.io.IOException;
/**
* 过滤器
*
* @author Smile
*/
@Order(1)
@WebFilter(filterName = "piceaFilter", urlPatterns = "/grafana/*")
public class ProxyServletFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
Filter.super.init(filterConfig);
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
//自行 实现条件判断即可
if (StpUtil.isLogin()) {
// 用户已登录,继续执行过滤器链
filterChain.doFilter(servletRequest, servletResponse);
} else {
// 用户未登录,可以返回错误信息或重定向到登录页面
// 例如,返回 HTTP 401 未授权状态
HttpServletResponse httpResponse = (HttpServletResponse) servletResponse;
httpResponse.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
}
}
@Override
public void destroy() {
Filter.super.destroy();
}
}
``撒花,收工~~
附:
grafana websocket需要修改custom.ini配置
# allowed_origins is a comma-separated list of origins that can establish connection with Grafana Live.
# If not set then origin will be matched over root_url. Supports wildcard symbol "*".
allowed_origins=*
或
allowed_origins = http://127.0.0.1:8889