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

Spring security 使用Spring Security Java配置时禁用基本身份验证,spring-security,spring-boot,spring-java-config,Spring Security,Spring Boot,Spring Java Config,我正在尝试使用SpringSecurityJava配置保护web应用程序 以下是配置的外观:- @配置 @启用WebMVC安全性 公共类安全配置扩展了WebSecurity配置适配器{ 私有字符串googleClientSecret; @自动连线 私有CustomUserService CustomUserService; /* *(非Javadoc) * *@请参阅org.springframework.security.config.annotation.web.configuration

我正在尝试使用SpringSecurityJava配置保护web应用程序

以下是配置的外观:-

@配置
@启用WebMVC安全性
公共类安全配置扩展了WebSecurity配置适配器{
私有字符串googleClientSecret;
@自动连线
私有CustomUserService CustomUserService;
/*
*(非Javadoc)
* 
*@请参阅org.springframework.security.config.annotation.web.configuration。
*Web安全配置适配器
*#配置(org.springframework.security.config
*.annotation.web.builders.HttpSecurity)
*/
@凌驾
受保护的无效配置(HttpSecurity http)引发异常{
//@formatter:off
http
.授权请求()
.antMatchers(HttpMethod.GET,“/”、“/static/**”、“/resources/**”、“/resources/public/**”).permitAll()
.anyRequest().authenticated()
.及()
.formLogin()
.及()
.httpBasic().disable()
.requireChannel().anyRequest().requirescure();
//@formatter:on
super.configure(http);
}
@凌驾
受保护的无效配置(AuthenticationManagerBuilder身份验证)
抛出异常{
//@formatter:off
认证
.删除凭据(true)
.userDetailsService(customUserService);
//@formatter:on
super.configure(auth);
}
}
请注意,我已使用以下命令显式禁用HTTP基本身份验证:-

.httpBasic().disable()
在访问安全url时,我仍然收到HTTP Authenticaton提示框。为什么?

请帮我修一下。 我只想呈现绑定的默认登录表单

弹簧启动启动器版本:1.1.5 Spring安全版本:3.2.5


谢谢如果您使用Spring Boot,状态如下:

在web中完全关闭引导默认配置 应用程序可以添加带有@EnableWebSecurity的bean

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


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

首先,调用
super.configure(http)将覆盖之前的所有配置

请尝试以下方法:

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

以下几点对我很有用:

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

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

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

这将导致在尝试访问任何对我有效的安全资源时收到403 Http错误。我的代码像

  http.csrf().disable().headers().frameOptions().sameOrigin().and().
   authorizeRequests().anyRequest().anonymous().and().httpBasic().disable();

适用于弹簧靴或使用OAuth的人

@Profile("test")
@EnableWebSecurity
static class BasicWebSecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable().authorizeRequests().anyRequest().anonymous().and().httpBasic().disable();
    }
}

如果您正在使用@EnableOAuth2Client或@EnableResourceServer,则在测试配置文件中切换到基本身份验证,然后禁用相同的身份验证。在Spring Boot中,要在web应用程序中完全关闭Spring安全默认配置,您需要添加一个带有@EnableWebSecurity的bean,将
security.basic.enabled=false
添加到
application.properties
。另外,您不应该从覆盖的方法调用
super.configure
。@M.Deinum修复了它。但是当我在java配置中显式禁用它时,为什么它没有被禁用呢?您可以有多个
websecurityconfigure
为整个配置做出贡献的配置。它很可能是,你有一个网站的休息部分,是由基本的认证和一个表单的正常网站保护。您可以创建两个
websecurityconfigure
一个用于rest,一个用于表单。您可能还需要签出(Spring Boot reference,security section)。@M.Deinum您不需要在*Configurer类中调用原始实现,它们只需使用空的主体实现接口的所有方法,因此您只需覆盖您想要使用的。小更正。。应该是.httpBasic().disable()。很抱歉,我是java新手,我们使用的jhipster具有角度前端和java后端。在哪里插入此代码?这是gateway吗?还是微软服务?应该查找什么文件?谢谢-@GelSisaed这可能是一个单独的问题。一般来说,这取决于您在何处进行身份验证,但对于jhipster,在网关中进行身份验证是非常典型的,因此我将从这里开始。发布您自己的问题(带有jhipster标记)也不是一个坏主意,比如“如何在jhipster中禁用HTTP Basic?”这对我不起作用(我将@EnableWebSecurity放在我的主Spring引导类和扩展的WebsecurityConfigureAdapter上)。“添加带有@EnableWebSecurity的bean”的实际含义是什么?这不会禁用基本安全性,它只会忽略所有请求的安全检查。