web-dev-qa-db-ja.com

Spring Boot 2.0はデフォルトのセキュリティを無効にします

Spring Security for JWT認証を使用したい。ただし、デフォルトの認証が付属しています。私はそれを無効にしようとしていますが、これを行う古いアプローチ-application.propertiesで無効にする-は2.0では非推奨です。

これは私が試したものです:

@Configuration
public class StackWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.httpBasic().disable();
        // http.authorizeRequests().anyRequest().permitAll(); // Also doesn't work.
    }
}

基本的なセキュリティを単純に無効にするにはどうすればよいですか?

UPDATE
web mvcではなくweb fluxを使用していることを知ってうれしいかもしれません。

スクリーンショット:
Basic login form

25
Jan Wytze

Spring 2.0の新しい更新によると、Spring Securityがクラスパスにある場合、Spring Bootは@ EnableWebSecurity.Addを追加するので、application.propertiesにエントリを追加しても機能しません(つまり、そのようにカスタマイズできなくなります)。詳細については、公式Webサイトをご覧ください Spring Boot 2.0のセキュリティの変更

あなたの要件について正確にはわかりませんが、次のような回避策が考えられます。

@Configuration
@EnableWebSecurity
public class SecurityConfiguration  extends WebSecurityConfigurerAdapter{
    @Override
    protected void configure(HttpSecurity http) throws Exception{
        http.authorizeRequests().antMatchers("/").permitAll();
    }
}

お役に立てれば。

32
Sen

以下をアプリケーションクラスに追加/変更できます。

@SpringBootApplication(exclude = { SecurityAutoConfiguration.class })
public class MyApplication {

}
10
helmy

リファレンスドキュメントによると 、WebFluxでのすべてのリクエストを許可するためのセキュリティ構成は次のようになります。

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.web.server.ServerHttpSecurity;
import org.springframework.security.web.server.SecurityWebFilterChain;

@Configuration
public class SecurityConfig {

    @Bean
    public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
        http.authorizeExchange().anyExchange().permitAll();
        return http.build();
    }
}
10
Brian Clozel

これは私のために働いた:

@Configuration
public class SecurityConfig  extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable().authorizeRequests().anyRequest().permitAll();
    }
}
8
davo

Spring Boot 2.1以降、spring-boot-actuatorを含める場合、SecurityAutoconfigurationを除外するだけでは不十分です。ManagementWebSecurityAutoConfigurationも除外する必要があります。

@SpringBootApplication(exclude = { SecurityAutoConfiguration.class, ManagementWebSecurityAutoConfiguration.class })
7

あなたが探しているのは、BasicAuthenticationEntryPointに設定されているデフォルトの認証エントリポイントを上書きすることだと思います。

このエントリポイントは

「WWW-Authenticate」:「Basic realm = ...」

ブラウザに基本認証を使用するように指示するヘッダー。

1
Marvin

問題はorg.springframework.security.web.server.authorization.ExceptionTranslationWebFilterにあります

private ServerAuthenticationEntryPoint authenticationEntryPoint = new HttpBasicServerAuthenticationEntryPoint();

serverHttpSecurityの初期化中に修正するには、次を追加します。

http.exceptionHandling().authenticationEntryPoint(HttpStatusServerEntryPoint(HttpStatus.FORBIDDEN))

Vanilla(servlet)springはorg.springframework.security.config.annotation.web.configurers.ExceptionHandlingConfigurer#createDefaultEntryPointを使用しているように見えます

private AuthenticationEntryPoint createDefaultEntryPoint(H http) {
        if (this.defaultEntryPointMappings.isEmpty()) {
            return new Http403ForbiddenEntryPoint();
        }
        if (this.defaultEntryPointMappings.size() == 1) {
            return this.defaultEntryPointMappings.values().iterator().next();
        }
        DelegatingAuthenticationEntryPoint entryPoint = new DelegatingAuthenticationEntryPoint(
                this.defaultEntryPointMappings);
        entryPoint.setDefaultEntryPoint(this.defaultEntryPointMappings.values().iterator()
                .next());
        return entryPoint;
    }

サイドノート:ビルダースタイルBean(ExceptionTranslationWebFilterなど)の可変フィールドは、スプリングコードのデバッグを困難にします(マジックコンフィグレーションも同様)

0
ichaki5748

Springブート2では、application.propertiesファイルによる基本認証を無効にする方法はありません。しかし、唯一のことはアノテーションを使用することです

@EnableAutoConfiguration(exclude = {SecurityAutoConfiguration.class})

メインクラスで。できます

0
ngochien9x

いくつかの新鮮な答えを加えて、私はすべてアクチュエータを使用すると仮定します。そうでない場合、1つのクラスの除外で十分であると確信し、プロパティを通して無効にすることができました:

spring:
  autoconfigure:
    exclude: ${spring.autoconfigure.sac}, ${spring.autoconfigure.mwsas}
    sac: org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration
    mwsas: org.springframework.boot.actuate.autoconfigure.security.servlet.ManagementWebSecurityAutoConfiguration

プロパティを介して2つのauto-configクラスを参照して、長さをそのままにしています(これらのプレースホルダー値が何であるかが分からないため実際に正当なクラスであるため、IntelliJ Ultimateはそのように参照すると泣きます)それはあなたを悩ます)。

ただし、アプリケーションは次のように起動に失敗しません。

https://www.baeldung.com/spring-boot-security-autoconfiguration

単にSecurityAutoConfigurationを無効にする場合

それが機能した場合、自動生成されたパスワードは表示されなくなり、セキュリティはすべてを許可しますが、devが基本認証の生成されたパスワードによって混乱することはないため、受け入れられた回答よりも少し混乱が少なくなります。

メインの自動構成クラスを無効にするだけでは十分ではない理由は、次のようなものです。

@Configuration
class ManagementWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .requestMatchers(
                        EndpointRequest.to(HealthEndpoint.class, InfoEndpoint.class))
                .permitAll().anyRequest().authenticated().and().formLogin().and()
                .httpBasic();
    }

}

アクチュエータとセキュリティ設定を分割するために行われた多くの作業があり、私たち全員を混乱させましたが、今ではより簡単ですが、これらのようなアーティファクトがまだ存在しています。私が間違っている場合、春の開発者は私を修正します:-)。

0
Aubergine