【问题标题】:Spring <http> namespace seems to cause all non-role based pages to be allowedSpring <http> 命名空间似乎导致所有非基于角色的页面都被允许
【发布时间】:2026-01-19 11:05:02
【问题描述】:

我正在尝试将 spring MVC 配置为不对任何没有身份验证的页面进行身份验证(启用 ROLE_ANONYMOUS 的使用,因为所有页面都明确要求)。

但我在调试日志中收到此消息:

o.s.s.w.a.i.FilterSecurityInterceptor  - Public object - authentication not attempted

FilterSecurityInterceptor 由命名空间添加。我认为我需要在过滤器上setRejectPublicInvocations 来禁用它。

但我看不到任何通过 http 命名空间执行此操作的方法。我是否必须完全放弃使用 http 命名空间才能完成此操作?

【问题讨论】:

  • 看看你的 applicationContext-security.xml 会很有用

标签: java spring spring-mvc spring-security


【解决方案1】:

就我而言,我基本上是这样做的。

它适用于匿名用户。

    <http auto-config="true" use-expressions="true">
    <intercept-url pattern="/css/**" filters="none" />
    <intercept-url pattern="/js/**" filters="none" />
    <intercept-url pattern="/img/**" filters="none" />
    <intercept-url pattern="/loginform.*" filters="none" />
    <intercept-url pattern="/topic/addtopic**"
        access="hasAnyRole('USER_ROLE','ADMIN_ROLE','OPER_ROLE')" />
    <intercept-url pattern="/user/**"
        access="hasAnyRole('USER_ROLE','ADMIN_ROLE','OPER_ROLE')" />
    <intercept-url pattern="/admin/**" access="hasRole('ADMIN_ROLE')" />
    <intercept-url pattern="/cadastro.*" filters="none" />
    <form-login login-page="/loginform.html"
        authentication-failure-url="/loginform.html?error=invalido" />
</http>

【讨论】:

  • 问题是,如果您有一个页面 /foobar.html 并且您没有明确授予角色,它将被用作“公共”页面/对象。我不想那样。我想确保所有页面都在权限方面明确定义(例如,页面必须被授予 ROLE_ANONYMOUS 权限,否则会被拒绝)。