后台 - 简易开发点记录
1. 在Filter中修改请求的path
RequestFacade facade= (RequestFacade) req; Class clzz=RequestFacade.class; try { Field field=clzz.getDeclaredField("request"); field.setAccessible(true); org.apache.catalina.connector.Request request1=(org.apache.catalina.connector.Request) field.get(facade); Class requestClass=request1.getClass(); //获取封装org.apache.coyote.Request的字段 Field coyoteField=requestClass.getDeclaredField("coyoteRequest"); coyoteField.setAccessible(true); Request coyoteRequest=(Request)coyoteField.get(request1); Class requestClazz=Request.class; //获取org.apache.coyote.Request中保存路径的字段 Field uriMBField= requestClazz.getDeclaredField("uriMB"); uriMBField.setAccessible(true); MessageBytes uriMB=(MessageBytes)uriMBField.get(coyoteRequest); //这里就是改变路径的地方 String addPath = "/newPathPrew" + uriMB.getString(); uriMB.setString(addPath); chain.doFilter(facade,response); //用来打印请求路径 // System.out.println(((RequestFacade) req).getRequestURL()); } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); }
2. springboot中使用proxyservlet的 formdata传参 bug
@Bean public org.springframework.boot.web.servlet.FilterRegistrationBean registration(org.springframework.web.filter.HiddenHttpMethodFilter filter) { FilterRegistrationBean registration = new FilterRegistrationBean(filter); registration.setEnabled(false); return registration; }
3. 配置多个 proxyServletRegistrationAuth
类使用 @Configuration,方法名称不同即可
@Bean @SuppressWarnings(value={"unchecked", "rawtypes"}) public ServletRegistrationBean proxyServletRegistrationAuth(){ ServletRegistrationBean registrationBean = new ServletRegistrationBean(new ProxyServlet(),"/xxx"); registrationBean.setName("xxxServlet"); registrationBean.addInitParameter("targetUri", "http://xxx.xxx.xxx:aaaa/xxx"); registrationBean.addInitParameter(ProxyServlet.P_LOG, "true"); return registrationBean; }
4. 在 doFilter 中转发请求 使用 forward (application类上需加 @ServletComponentScan )
@WebFilter(urlPatterns = "/*") public class DMZFilter implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) req; HttpServletResponse response = (HttpServletResponse) res; String path = request.getRequestURI(); if (特定条件) { request.getRequestDispatcher("/xxx" + path).forward(request,response); return; } else { chain.doFilter(req, res); } } @Override public void destroy() { } }
相关博文地址:
https://www.cnblogs.com/java2java/p/13200636.html
https://www.jianshu.com/p/bff7ac3fa4ab
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具