围绕使用Java Config进行Spring Security匿名访问的困惑

use*_*809 8 java spring spring-security spring-java-config

我在Spring Security中使用以下Java Config:

protected void configure(HttpSecurity http) throws Exception {
    http
        .authorizeRequests()
            .anyRequest().authenticated()
            .and()
        .httpBasic();
}
Run Code Online (Sandbox Code Playgroud)

根据此配置,所有请求都经过身份验证.当您在未经过身份验证的情况下点击控制器时,AnonymousAuthenticationFilter将为您创建一个Authentication对象username=anonymousUser, role=ROLE_ANONYMOUS.

我试图提供对特定控制器方法的匿名访问,并尝试使用以下各项:

  1. @Secured("ROLE_ANONYMOUS")
  2. @Secured("IS_AUTHENTICATED_ANONYMOUSLY")

调用控制器方法时,会给出以下响应:"HTTP状态401 - 访问此资源需要完全身份验证"

有人可以帮助我理解为什么我们收到此消息以及为什么ROLE_ANONYMOUS/ IS_AUTHENTICATED_ANONYMOUSLY似乎没有使用此配置?

谢谢,
JP

ant*_*tix 3

您的安全配置正在阻止所有未经身份验证的请求。您应该允许访问控制器

.antMatchers("/mycontroller").permitAll()
Run Code Online (Sandbox Code Playgroud)

也可以看看: