使用Spring Security Java配置时禁用基本身份验证

Kum*_*hav 13 spring-security spring-boot spring-java-config

我正在尝试使用Spring Security java配置来保护Web应用程序.

这是配置的外观: -

@Configuration
@EnableWebMvcSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    private String googleClientSecret;

    @Autowired
    private CustomUserService customUserService;

    /*
     * (non-Javadoc)
     * 
     * @see org.springframework.security.config.annotation.web.configuration.
     * WebSecurityConfigurerAdapter
     * #configure(org.springframework.security.config
     * .annotation.web.builders.HttpSecurity)
     */
    @Override
    protected void configure(HttpSecurity http) throws Exception {

        // @formatter:off
        http
            .authorizeRequests()
                .antMatchers(HttpMethod.GET, "/","/static/**", "/resources/**","/resources/public/**").permitAll()
                .anyRequest().authenticated()
            .and()
                .formLogin()
                    .and()
                .httpBasic().disable()
            .requiresChannel().anyRequest().requiresSecure();
        // @formatter:on
        super.configure(http);
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth)
            throws Exception {
        // @formatter:off
        auth
            .eraseCredentials(true)
            .userDetailsService(customUserService);
        // @formatter:on
        super.configure(auth);
    }
}
Run Code Online (Sandbox Code Playgroud)

请注意,我已使用以下方法明确禁用HTTP基本身份验证:

.httpBasic().disable()
Run Code Online (Sandbox Code Playgroud)

我在访问安全网址时仍然会收到HTTP Authenticaton提示框.为什么?

请帮我解决这个问题.我只想渲染捆绑的默认登录表单.

Spring Boot Starter版本:1.1.5 Spring Security版本:3.2.5

谢谢

kaz*_*uar 19

首先,调用super.configure(http);将覆盖您之前的整个配置.

试试这个:

http
    .authorizeRequests()
        .anyRequest().authenticated()
        .and()
    .formLogin()
        .and()
    .httpBasic().disable();
Run Code Online (Sandbox Code Playgroud)


Mar*_*ijk 6

如果您使用Spring Boot,文档说明:

要在Web应用程序中完全关闭Boot默认配置,可以使用@EnableWebSecurity添加bean

因此,如果您想完全自定义可能是一个选项.

只是为了说清楚......你只需要在主应用程序类或应用程序配置类上放置@EnableWebSecurity注释.

  • 这对我不起作用(我把@EnableWebSecurity放在我的主Spring Boot类和扩展的WebSecurityConfigurerAdapter上)."用@EnableWebSecurity添加bean"实际意味着什么? (3认同)