当前位置: 首页 > 知识库问答 >
问题:

Spring Boot:注入自定义上下文路径

皇甫聪
2023-03-14

我正在运行一个带有嵌入式Tomcat的Spring Boot1.2.3应用程序。

示例:

@Component @Order(Ordered.HIGHEST_PRECEDENCE)
public class MultiTenancyFilter implements Filter {
    private final static Pattern pattern = Pattern.compile("^/(?<contextpath>[^/]+).*$");

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        final HttpServletRequest req = (HttpServletRequest) request;
        final String requestURI = req.getRequestURI();

        Matcher matcher = pattern.matcher(requestURI);
        if(matcher.matches()) {
            chain.doFilter(new HttpServletRequestWrapper(req) {
                @Override
                public String getContextPath() {
                    return "/"+matcher.group("contextpath");
                }
            }, response);
        }
    }

    @Override public void init(FilterConfig filterConfig) throws ServletException {}
    @Override public void destroy() {}
}

但是Spring@Controller@RequestMapping和Spring Security的antMatchers(“/”)似乎并不尊重它。两者仍然可以像contextpath=“”一样工作。

共有1个答案

陈松
2023-03-14

起作用了!

Spring Security文档(http://docs.Spring.io/spring-security/site/docs/3.1.x/reference/security-filter-chain.html)说:“Spring Security只对保护应用程序中的路径感兴趣,因此忽略了contextPath。不幸的是,servlet规范没有确切定义servletPath和pathInfo的值对于特定请求URI将包含什么。[...]该策略在类AntPathRequestMatcher中实现,它使用Spring的AntPathMatcher对串联的servletPath和pathInfo执行模式的不区分大小写的匹配,忽略了QueryString。”

所以我只是覆盖了ServletPathContextPath(即使Spring Security没有使用它)。此外,我还添加了一些小的重定向,因为通常在点击http://localhost:8080/mycontext时,会被重定向到http://localhost:8080/mycontext/,而Spring Securities Ant Matcher不喜欢缺少尾随斜杠。

下面是我的MultiTenancyFilter代码:

@Component @Order(Ordered.HIGHEST_PRECEDENCE)
public class MultiTenancyFilter extends OncePerRequestFilter {
    private final static Pattern pattern = Pattern.compile("^(?<contextPath>/[^/]+)(?<servletPath>.*)$");

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        Matcher matcher = pattern.matcher(request.getServletPath());
        if(matcher.matches()) {
            final String contextPath = matcher.group("contextPath");
            final String servletPath = matcher.group("servletPath");

            if(servletPath.trim().isEmpty()) {
                response.sendRedirect(contextPath+"/");
                return;
            }

            filterChain.doFilter(new HttpServletRequestWrapper(request) {
                @Override
                public String getContextPath() {
                    return contextPath;
                }
                @Override
                public String getServletPath() {
                    return servletPath;
                }
            }, response);
        } else {
            filterChain.doFilter(request, response);
        }
    }

    @Override
    protected String getAlreadyFilteredAttributeName() {
        return "multiTenancyFilter" + OncePerRequestFilter.ALREADY_FILTERED_SUFFIX;
    }
}

它只是使用下面提到的URL模式提取contextPath和servletPath:https://theholyjava.wordpress.com/2014/03/24/HttpServletRequest-RequestUriRequestUrlContextPathServletPathPathInfoQueryString/

 类似资料:
  • 在Spring-Boot应用程序中,可以使用中的属性来选择上下文路径,但是由于我使用的是外部Tomcat8,因此没有使用该属性。 因此,我查看了tomcat-8文档,其中指出: 如果要使用与基文件名无关的上下文路径部署WAR文件或目录,则必须使用以下选项之一来防止双重部署: null null

  • 我正在尝试为AWT Graphics2D实现类似于SWT GC的xor模式绘图。使用内置XORComposite不是一个选项,因为它不像SWT那样实现xor模式绘图。 SWT xor模式绘图通过二进制异或组合源颜色和目标颜色。AWT XORComposite(可通过)使用恒定的xor颜色,该颜色通过二进制异或与源颜色组合,即目标颜色不影响结果颜色。 所以我唯一的选择就是编写我自己的Composit

  • 我通过将上下文路径设置为/myservice来运行我的springboot应用程序。这将导致附加在URL处公开的所有执行器endpoint-http://localhost:8080/myservice/actuator/,而我只想要http://localhost:8080/actuator/.有没有办法告诉springboot忽略将上下文路径附加到执行器endpoint(通过Dispatche

  • 我使用以下代码在tomcat中设置上下文路径,通过覆盖tomcat的默认路径,我可以使用直接访问应用程序。 现在我将使用WildFly-8.2.0作为运行时环境。我尝试将。war文件直接粘贴到中,我可以在等浏览器中访问我的项目。 我需要像在tomcat中那样为wildfly设置相同的配置,以便通过重写wildfly的默认欢迎页面来访问中的项目。我试图在wildfly中做同样的事情,但我被困在哪里。

  • 我是selenium的新手,我想使用selenium chrome Web驱动程序在特定的自定义文件夹中下载文件。默认情况下,该文件正在浏览器指定的下载路径中下载。任何一个建议在C#Selenium的自定义路径中下载文件的最佳解决方案。

  • 问题内容: 我是selenium的新手,我想在特定的自定义文件夹中使用selenium chrome Web驱动程序下载文件。默认情况下,文件正在浏览器指定的下载路径中下载。任何人都建议在C#Selenium中以自定义路径下载文件的最佳解决方案。 问题答案: 希望对您有帮助!