开发者

Defining spring security http element in two different files?

开发者 https://www.devze.com 2023-04-09 05:54 出处:网络
Is it possible to define the security:intercept-url elements and security:custom-filter elements for a single security:http in two different Spring configuration files?

Is it possible to define the security:intercept-url elements and security:custom-filter elements for a single security:http in two different Spring configuration files?

This is so we can cleanly reuse the security:custom-filter definitions which wil开发者_开发技巧l be common across many applications with intercept rules that will not.


I can't simply duplicate the <security:http> element because I get BeanDefinitionParsingException: Configuration problem: Duplicate <http> element detected. I am well well aware of how to split a normal bean file with import


As requested in comment:

Spring Security versions prior to 3.1.x do not allow multiple http element definitions.

3.1 does however.

Here is the Jira issue for the feature.

This article on 3.1 changes might also be helpful.


You can define another context file in your web.xml:

<context-param>
  <param-name>contextConfigLocation</param-name>
    <param-value>
      /WEB-INF/spring-contexts/context1.xml
      /WEB-INF/spring-contexts/context2.xml
    </param-value>
</context-param>

Or you can define a directory where your contexts would be and name them any way you like without having to specify each context file separately:

<context-param>
  <param-name>contextConfigLocation</param-name>
    <param-value>
      /WEB-INF/spring-contexts/*
    </param-value>
</context-param>

Regarding Ayusman's answer, you actually can import your security contexts into your bean contexts:

<beans>

    <import resource="classpath*:/security-context-*.xml"/>

    <bean><!-- blah blah --></bean>

</beans>


use the import in application context file..

custom-filter.appcontext.xml
.
.
<import resource="interceptor-url-file.xml"/>

Note that both files need to have the proper spring xml schema details and MUST be valid XML files.


I have been working on this error for 5 hours. Really stupid problem.

This error is a parse error that when you comment some lines in applicationContext-security.xml, files are not generated correctly.

Let me explain on an example code.

<port-mappings>
        <port-mapping http="7001" https="7002" />
</port-mappings>

<!--    <port-mappings>
        <port-mapping http="7015" https="7515" />
    </port-mappings>
 -->

this lines are generated as,

<port-mappings>
        <port-mapping http="7001" https="7002" />
</port-mappings>

    <port-mappings>
        <port-mapping http="7015" https="7515" />
    </port-mappings>
 -->

so that, compiler tells you "duplicate element detected". Because generated file includes duplicate elements.

I hope to help you .

0

精彩评论

暂无评论...
验证码 换一张
取 消