匹配模式冲突与Spring Security配置中的两个HTML部分

时间:2015-03-18 00:53:53

标签: spring spring-security

我正在尝试使用应用程序/ json 响应以及通常的基于页面的响应来实现Spring Security。我相信这意味着我的xml配置中需要两个 html 部分,每种类型一个。这是我的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://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.2.xsd">

<!-- Spring Security for JSP, HTML pages etc -->
<http auto-config="true" use-expressions="true">
    <intercept-url pattern="/ui/**" access="hasRole('ROLE_USER')" />

    <form-login
        login-page="/login"
        default-target-url="/ui/app.html"
        authentication-failure-url="/login?error"
        username-parameter="username"
        password-parameter="password" />
    <logout logout-success-url="/login?logout" />
    <csrf/>  <!-- enable csrf protection -->
</http>

<!-- Spring Security for application/json -->
<http entry-point-ref="myAuthenticationEntryPoint" use-expressions="true">
    <intercept-url pattern="/json/**" access="hasRole('ROLE_USER')"/>
</http>

<!-- Configures the authentication entry point that returns HTTP status code 401 -->
<beans:bean id="myAuthenticationEntryPoint" class="org.my.security.RestAuthenticationEntryPoint"/>

<!-- Configures in-memory implementation of the UserDetailsService implementation -->
<authentication-manager>
    <authentication-provider >
        <user-service>
            <user name="test" password="test" authorities="ROLE_USER" />
        </user-service>
    </authentication-provider>
</authentication-manager>

</beans:beans>

当我运行它时会抛出一条带有消息

的异常
  

通用匹配模式('/ **')在其他模式之前定义...

无论xml中两个 html 部分的顺序如何,它都会执行此操作,但如果我删除处理application / json的部分,它不会抛出异常

1 个答案:

答案 0 :(得分:0)

我解决了这个问题。 html 部分采用 pattern = 属性,默认为“/ **”,因此您需要提供模式= html 部分的strong>属性。我按如下方式修改了我的security-config.xml,它现在可以工作了:

<!-- Spring Security for application/json -->
<http pattern="/json/**" entry-point-ref="myAuthenticationEntryPoint" use-expressions="true">
    <intercept-url pattern="/json/**" access="hasRole('ROLE_USER')"/>
</http>

<!-- Configures the authentication entry point that returns HTTP status code 401 -->
<beans:bean id="myAuthenticationEntryPoint" class="org.my.security.RestAuthenticationEntryPoint"/>

<!-- Spring Security for JSP, HTML pages etc -->
<http auto-config="true" use-expressions="true">
    <intercept-url pattern="/ui/**" access="hasRole('ROLE_USER')" />
    ...
</http>

我没有为页面类型响应提供 pattern = ,因为这打破了登录功能,所以我不得不重新订购 html 部分。