Spring Security permitAll 不允许匿名访问

新手上路,请多包涵

我有一个方法,我想允许匿名访问和经过身份验证的访问。

我正在使用基于 Java 的配置的 Spring Security 3.2.4。

覆盖的配置方法(在我的自定义配置类中扩展 WebSecurityConfigurerAdapter )具有以下 http 块:

     http
        .addFilterBefore(muiltpartFilter, ChannelProcessingFilter.class)
        .addFilterBefore(cf, ChannelProcessingFilter.class)
        .authorizeRequests()
            .anyRequest()
            .authenticated()
            .and()
        .authorizeRequests()
            .antMatchers("/ping**")
            .permitAll()
            .and()
        .formLogin()
            .loginPage("/login")
            .permitAll()
            .and()
        .logout()
            .logoutUrl("/logout")
        .logoutSuccessUrl("/login");

ping 请求处理程序和方法位于还包含登录处理程序的控制器中,它没有单独的 @PreAuthorize 或其他可能导致问题的注释。

问题是匿名访问被拒绝,用户被重定向到登录页面。

在调试级别登录,我看到来自 Spring Security 的以下反馈:

 [2014-07-11 13:18:04,483] [DEBUG] [org.springframework.security.web.access.intercept.FilterSecurityInterceptor] Secure object: FilterInvocation: URL: /ping; Attributes: [authenticated]
[2014-07-11 13:18:04,483] [DEBUG] [org.springframework.security.web.access.intercept.FilterSecurityInterceptor] Previously Authenticated: org.springframework.security.authentication.AnonymousAuthenticationToken@6faad796: Principal: anonymousUser; Credentials: [PROTECTED]; Authenticated: true; Details: org.springframework.security.web.authentication.WebAuthenticationDetails@ffffa64e: RemoteIpAddress: 192.168.2.128; SessionId: 0EF6B13BBA5F00C020FF9C35A6E3FBA9; Granted Authorities: ROLE_ANONYMOUS
[2014-07-11 13:18:04,483] [DEBUG] [org.springframework.security.access.vote.AffirmativeBased] Voter: org.springframework.security.web.access.expression.WebExpressionVoter@123f2882, returned: -1
[2014-07-11 13:18:04,483] [DEBUG] [org.springframework.security.web.access.ExceptionTranslationFilter] Access is denied (user is anonymous); redirecting to authentication entry point

我想要完成的是拥有一个可以在任何时候调用的方法,该方法将发送一个回复,指示请求是否在登录会话中。

原文由 Marceau 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.1k
1 个回答

权限顺序很重要,当我这样配置时它会起作用:

 .authorizeRequests()
        .antMatchers("/ping**")
        .permitAll()
        .and()
.authorizeRequests()
        .anyRequest()
        .authenticated()
        .and()

原文由 samsong8610 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题