【问题标题】:Spring MVC Security permitAll to / but denyAll to /** not workingSpring MVC Security permitAll to / 但dendenAll to /** 不起作用
【发布时间】:2018-04-17 13:55:27
【问题描述】:

我有一个 Spring4 MVC 应用程序,它部署在 Wildfly10 上并使用 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,应用程序将按预期工作,但是添加它会导致安全性将 root 调用重定向到登录页面而不是索引页面!

有没有一种方法可以让我对我的应用程序的根目录(重定向到 /index)进行 permitAll 访问,并且仍然对 /** 拒绝所有访问权限,从而覆盖未定义的任何其他内容?

【问题讨论】:

  • &lt;http&gt; 元素上设置request-matcher="regex",并尝试将/.+ 作为denyAll 行的模式。
  • 感谢@Vasan /.+ 工作 100%。我根本不需要更改request-matcher="regex",它必须默认为那个。

标签: spring spring-mvc spring-security


【解决方案1】:

通过将模式更改为 &lt;intercept-url pattern="/.+" access="denyAll" /&gt;,正如 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"/> 

【讨论】:

    猜你喜欢
    • 2016-02-16
    • 2018-01-31
    • 2017-08-20
    • 2019-09-27
    • 2020-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-14
    相关资源
    最近更新 更多