java - 如何在分布在多个文件上的 Spring Security 中订购多个 <http> 元素

标签 java spring spring-security

在 Spring Security 中可以指定多个 <http>导致多个 SecurityFilterChains 的配置。我使用该功能来保护不同于普通 Web 应用程序的 Rest API。 Web 应用程序和 rest api 都是在不同的模块(maven 工件)中开发的。 Spring 配置在整个类路径(classpath*:/some-common-config-path/*.xml)中通过通配符模式收集。

网络应用程序的安全配置 web-security-config.xml :

<beans:beans xmlns="http://www.springframework.org/schema/security" xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://cxf.apache.org/configuration/beans http://cxf.apache.org/schemas/configuration/cxf-beans.xsd
    http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">

  <!-- Security Config for web app -->
  <http use-expressions="true" auto-config="false" entry-point-ref="loginEntryPoint">
       ...
  </http>

  <!-- Security Config for static resources -->
  <http pattern="/static/**" security="none" />

  ...

</beans:beans>

api-security-config.xml 中 Rest API 的安全配置:

<beans:beans xmlns="http://www.springframework.org/schema/security" xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://cxf.apache.org/configuration/beans http://cxf.apache.org/schemas/configuration/cxf-beans.xsd
    http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">

  <http pattern="/api/**" use-expressions="true" create-session="stateless"
        authentication-manager-ref="apiAuthManager" entry-point-ref="restAuthenticationEntryPoint">
      <intercept-url pattern="/api/**" access="hasRole('REST_API')" />
      <http-basic />
  </http>

  ...

</beans:beans>

问题是,类路径中两个模块的顺序是不可预测的,配置文件的解析顺序也是如此。在我的特殊情况下 api-security-config.xml web-security-config.xml 之后阅读并且应用程序上下文启动失败,出现以下异常:

java.lang.IllegalArgumentException: A universal match pattern ('/**') is defined before other patterns in the filter chain, causing them to be ignored. Please check the ordering in your namespace or FilterChainProxy bean configuration

所以我要做的是以某种方式指定 <http> 的顺序元素,以便在最通用的配置之前解析特定的配置。有没有可能这样做?

最佳答案

此时没有办法打破 Spring Security 的 XML 配置并指定一个缺少确保您的 XML 以正确顺序加载的顺序。实现此目的的一种方法是执行以下操作:

创建一个/some-common-config-path/security.xml 配置,该配置由公共(public)配置加载,以正确的顺序导入两个配置(不在公共(public)配置位置):

<import resource="/not-common-config-path/api-security-config.xml"/>
<import resource="/not-common-config-path/web-security-config.xml"/>

您可以在 Spring Security 3.2 中使用 Java 配置并使用 @Order 注释来执行此操作。如图所示 in the documentation :

@Configuration
@EnableWebSecurity
public class MultiHttpSecurityConfig {
    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) { 
        auth
            .inMemoryAuthentication()
                .withUser("user").password("password").roles("USER");
    }

    @Configuration
    @Order(1)                                                        
    public static class ApiWebSecurityConfigurationAdapter extends WebSecurityConfigurerAdapter {
        protected void configure(HttpSecurity http) throws Exception {
            http
                .antMatcher("/api/**")                               
                .authorizeRequests()
                    .anyRequest().hasRole("ADMIN")
                    .and()
                .httpBasic();
        }
    }

    @Configuration                                                   
    public static class FormLoginWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {

        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http
                .authorizeRequests()
                    .anyRequest().authenticated()
                    .and()
                .formLogin();
        }
    }
}

如果您愿意,您也可以分解这些配置。

要自动获取 Java 配置,您可以轻松地将类路径扫描添加到您的设置中。例如,如果您正在使用以 XML 为中心的配置,并且您的所有 Java 配置都在包 com.example.config 中,您可以添加:

<!-- 
    enable processing of annotations such as @Autowired and @Configuration
    You may already have annotation-config
 -->
<context:annotation-config/>
<!-- Add any Java Configuration -->
<context:component-scan base-package="com.example.config"/>

有关 XML 和 Java 配置的更多详细信息,请阅读组合 Java and XML Configuration来自引用。

关于java - 如何在分布在多个文件上的 Spring Security 中订购多个 <http> 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21455596/

相关文章:

java - Apache 使用 Spring Security 重定向到 Tomcat 应用程序

java - 计算器字符串

java - XStream实体缩写解析

api - 如何只允许自己的页面访问API?

linux - Spring Boot 发送邮件

java - spring 写回属性文件

java - 将请求头转发到 Spring + Netflix + Feign 中的多个服务调用

java - Rabbitmq 连接在本地主机上突然关闭

java - servlet 容器何时会中断我的线程?

Java Spring : How to see how many entities/rows are affected by a repository.删除方法?