Spring MVC Security permitAll to / but denyAll to / ** not working

时间:2017-11-05 07:54:47

标签: spring spring-mvc spring-security

我有一个部署在Wildfly10上的Spring4 MVC应用程序,并使用xml进行配置。

我定义了以下控制器:

<mvc:view-controller path="/" view-name="/index" />
<mvc:view-controller path="/index" view-name="/index" />

在Spring安全性中定义访问:

<http auto-config="true" use-expressions="true">
    <intercept-url pattern="/" access="permitAll" />
    <intercept-url pattern="/index" access="permitAll" />
    ...
    <intercept-url pattern="/**" access="denyAll" />
    <form-login login-page="/login" default-target-url="/dashboard"
        always-use-default-target="true" authentication-failure-url="/loginfailed"
        authentication-failure-handler-ref="authenticationFailureHandler" />
    <logout logout-success-url="/index" />
    <access-denied-handler ref="customAccessDeniedHandler"/> 
</http>

如果我将denyAll删除到/ ** intercept-url,应用程序按预期工作,但是添加它会导致安全性将根调用重定向到登录页面而不是索引页面!

有没有办法让我可以访问我的应用程序的根目录(重定向到/索引)并仍然拒绝所有/ **因此覆盖任何未定义的内容?

1 个答案:

答案 0 :(得分:0)

通过将模式更改为<intercept-url pattern="/.+" access="denyAll" />,由Vasan评论让它正常工作。下面是一个变化的例子

<http auto-config="true" use-expressions="true">
<intercept-url pattern="/" access="permitAll" />
<intercept-url pattern="/index" access="permitAll" />
...
<intercept-url pattern="/.+" access="denyAll" />
<form-login login-page="/login" default-target-url="/dashboard"
    always-use-default-target="true" authentication-failure-url="/loginfailed"
    authentication-failure-handler-ref="authenticationFailureHandler" />
<logout logout-success-url="/index" />
<access-denied-handler ref="customAccessDeniedHandler"/>