2014-09-03 144 views
8

我试图使用Spring Security java配置来保护Web应用程序。使用Spring Security Java配置时禁用基本身份验证

这是配置的样子: -

@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); 
    } 
} 

请注意,我有明确禁用HTTP基本身份验证使用: -

.httpBasic().disable() 

我仍然得到HTTP Authenticaton提示框,同时在访问安全网址。为什么?

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

春天引导入门版本:1.1.5 Spring Security的版本:3.2.5

感谢

+6

将'security.basic.enabled = false'添加到您的'application.properties'。你也不应该从覆盖的方法调用'super.configure'。 – 2014-09-03 17:31:18

+0

@ M.Deinum修正了它。但是为什么当我在java配置中显式禁用时它没有被禁用? – 2014-09-03 17:33:36

+0

您可以拥有多个'WebSecurityConfigurer',每个贡献配置的整体配置。这很可能是因为你的网站的其余部分受到基本身份验证和具有表单的普通网站的保护。你可以创建2个'WebSecurityConfigurer'用于休息,一个用于表单。你也可能想结账http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-security.html(Spring Boot reference,security section)。 – 2014-09-03 18:26:32

回答

6

如果你使用Spring开机后,documentation状态:

要关闭引导默认配置完全在网络中 应用程序中,您可以使用@EnableWebSecurity添加bean

所以,如果你想完全自定义自己可能是一个选项。

只是为了说清楚......您只需要在您的主应用程序类或应用程序配置类上添加@EnableWebSecurity注释。

+2

这对我不起作用(我把@EnableWebSecurity放在我的主要Spring Boot类和扩展的WebSecurityConfigurerAdapter上)。它实际上意味着'使用@EnableWebSecurity添加bean'? – Ivan 2015-03-31 16:47:03

16

首先调用super.configure(http);将在此之前覆盖整个你的配置。试试这个:

http 
    .authorizeRequests() 
     .anyRequest().authenticated() 
     .and() 
    .formLogin() 
     .and() 
    .httpBasic().disable(); 
+1

小修正..应该是.httpBasic()。disable()。 – ticktock 2014-12-12 23:28:12

-4

以下为我工作:

  http 
       .authorizeRequests() 
       .anyRequest().permitAll(); 
+5

这不会禁用基本安全性,它会简单地忽略对所有请求的安全检查。 – jkerak 2016-11-04 14:17:35

1

您可以通过HttpSecurity实例禁用formLogin如下:

http.authorizeRequests().antMatchers("/public/**").permitAll() 
     .antMatchers("/api/**").hasRole("USER") 
     .anyRequest().authenticated() 
     .and().formLogin().disable(); 

这将导致接收403 HTTP错误的时候试图访问任何安全资源

相关问题