Spring 安全性 Java 配置而不强制验证

Spring Security Java Config without Forcing Auth

我很难弄清楚如何在不强制身份验证的情况下连接 spring 安全性。我的特定应用程序不需要用户进行身份验证,但用户可以根据需要进行身份验证。

我目前设置了一个 WebSecurityConfigurerAdapter,您可以在本文末尾看到 post。通过此设置,我在所有 /api/* 请求和 /j_spring_security_check 上都收到了 403。

有人可以帮我修复我现有的配置或指出一个可以完成此操作的工作示例吗?

我看到的每个例子似乎都要求用户进行身份验证,如果他们不这样做,它会抛出 403。在我的应用程序中,我只是用它来建立会话,此时所有用户都应该能够访问所有端点,无论它们是否经过身份验证。

WebSecurityConfig

@EnableWebSecurity
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    private ItAuthenticationProvider customAuthenticationProvider;


    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) {
        auth.authenticationProvider(customAuthenticationProvider);
    }

    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/resources/**");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable().authorizeRequests().antMatchers("/**").permitAll()
                .antMatchers("/j_spring_security_check").permitAll()
                .and().formLogin()
                .loginProcessingUrl("/j_spring_security_check")
                .defaultSuccessUrl("/successful.html")
                .loginPage("/#login")
                .permitAll()
                .and()
                .logout()
                .logoutSuccessUrl("/successful.html");
    }
}

如果我理解你的要求,你可以使用匿名认证。 可以找到文档 here

你可以看看this sample Spring Security application we built with Stormpath支持。在此示例中,主屏幕不需要身份验证。此外,显示的信息是根据用户是否经过身份验证动态计算的。