49

JWT 認証に Spring Security を使用したい。ただし、デフォルトの認証が付属しています。私はそれを無効にしようとしていますが、これを行う古いアプローチ - 無効にする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.
    }
}

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

更新
Web mvc ではなく Web フラックスを使用していることを知っておくとよいかもしれません。

スクリーンショット:
基本ログインフォーム

4

14 に答える 14

19

リファレンス ドキュメント によると、 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();
    }
}
于 2017-11-14T17:44:29.740 に答える
1

application.yml でプロパティを falseに設定して春のセキュリティを無効にする場合、次のクラス@ConditionalOnPropertyをロードするために活用しましたが、それは魅力的です。SecurityConfig.javaspring.security.enabled

@ConditionalOnProperty(name = "spring.security.enabled", havingValue = "false")
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable()
            .authorizeRequests().antMatchers("/").permitAll();
    }
}
于 2021-01-28T14:38:31.073 に答える
1

を拡張する場合は、スーパー コンストラクターにWebSecurityConfigurerAdapter渡してデフォルトを無効にすることができます。 これを行う場合は、他の Bean を提供する必要がある場合があります。 true

    /**
     * Creates an instance which allows specifying if the default configuration should be
     * enabled. Disabling the default configuration should be considered more advanced
     * usage as it requires more understanding of how the framework is implemented.
     *
     * @param disableDefaults true if the default configuration should be disabled, else
     * false
     */
    protected WebSecurityConfigurerAdapter(boolean disableDefaults) {
        this.disableDefaults = disableDefaults;
    }

テスト目的でのみ無効にする場合 - 自動構成を完全に無効にするのではなく、「SecurityConfiguration」に加えて「InsecurityConfiguration」を作成し、Spring プロファイルまたはプロパティ値でアクティブ化します。

技術的にはセキュリティはまだ構成されていますが、広く開かれています。

@Configuration
@ConditionalOnProperty(prefix = "security", value = "disabled", havingValue = "true")
public class InsecurityConfiguration extends WebSecurityConfigurerAdapter {

    private final static Logger log = LoggerFactory.getLogger(InsecurityConfiguration.class);

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        log.warn("configuring insecure HttpSecurity");
        http.authorizeRequests().anyRequest().permitAll();
    }

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

}

これは、webflux ではなく、mvc 用です。Webflux の場合、SecurityWebFilterChainブライアンが言及したようなものを作成する必要があります。

これは、JWTを使用するときに、webfluxで基本認証を無効にする方法です-

    @Bean
    public SecurityWebFilterChain configure(ServerHttpSecurity http) {

        http
        .authorizeExchange().anyExchange().authenticated().and()
            .httpBasic().disable()
            .formLogin().disable()
            .logout().disable()
            .oauth2ResourceServer()
            .jwt()
            .and()
                .and().exceptionHandling().accessDeniedHandler(problemSupport);
        return http.build();
    }
于 2019-12-15T20:26:19.697 に答える