2017-06-27 41 views
1

パスワードハッシュ暗号化を使用してbasic authenticationを有効にしようとしています。application.propertiesによるspring-securityを無効にする方法は?

@Configuration //gets picked up automatically by spring-boot 
public class WebSecurityConfig extends WebSecurityConfigurerAdapter { 
    @Override 
    protected void configure(AuthenticationManagerBuilder auth) throws Exception { 
      auth.userDetailsService(details).passwordEncoder(new BCryptPasswordEncoder()); 
    } 
} 

認証を本番環境でのみ有効にします。だから私は、最初に一般的に使用し、それを無効にしようとしている:

security.basic.enabled=false

結果:アプリケーションがまだ確保されています。私はユーザー名/パスワードの画面が表示されます。

しかし、どうすれば認証の必要性を無力化できますか?完全にあなたはSpring Profiles

は、あなたが、あなただけの本番環境でのセキュリティのコンフィグレーションBeanを必要とし使用する必要があり、異なる環境で異なることを行うには、次のプロパティ

spring.autoconfigure.exclude[0]=org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration 

答えて

1

:上記記載の通り

@Configuration 
@ConditionalOnProperty("security.basic.enabled") 
public class WebSecurityConfig extends WebSecurityConfigurerAdapter { 

} 
-1

特定のプロファイルが有効なときに条件付きでロードされるようにマークする必要があります。

@Configuration //gets picked up automatically by spring-boot 
@Profile("Production") 
public class WebSecurityConfig extends WebSecurityConfigurerAdapter { 
    @Override 
    protected void configure(AuthenticationManagerBuilder auth) throws Exception { 
      auth.userDetailsService(details).passwordEncoder(new BCryptPasswordEncoder()); 
    } 
} 

プロフィール具体的な構成はapplication-{profile}.propertiesbootstrap-{profile}.propertiesファイルを名前付きプロパティファイルで行われます。

セキュリティコンフィグレーションBeanはプロファイルProductionでマークされているので、Productionプロファイルが有効な場合にのみロードされます。

プロファイルを有効にするには、次のプロパティを設定する必要があります。

#One or more profiles can be active simultaneously 
spring.profiles.active=Production,Dev,Local 

このプロパティは、どちらかapplication.properties(必ず条件付きでロードされているapplication-Production.propertiesのようなプロファイルの特定のプロパティファイルとは異なり、ロードされた)共通プロパティファイルで編集することも、以下のように環境変数として提供することができます。

これは、プロパティを読み込むために春のサポートが100万種類も使用されます。すべてのメソッドのリストについては、このリンクを参照してください。https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html

最後に、デフォルトのSpringセキュリティ(基本)自動設定を無効にするには、次のプロパティを使用できます。

security.basic.enabled=false 
management.security.enabled=false #For actuator 

以上は、Spring Securityの自動構成を無効にするプロファイル固有のプロパティファイルにある必要があります。 ConditionalOnPropertyで解決

1

を使用し、スプリングセキュリティの自動設定を無効にするには

+0

は、私はすでに正常に動作するはずですが、 '' WebSecurityConfigurerAdapterを使用しない場合 'security.basic.enabled = false'のを、使用してみました。 – membersound

+0

これは、 'WebSecurityConfigurerAdapter'が依然としてBeanを作成しているためです。これは、応答の冒頭で述べたように、条件付きでロードする必要があります。 – 11thdimension

+0

私はWebSecurityConfigurerAdapterを2回拡張し、上記のように動作しました。魅力のように働いた。 – DOUBL3P

関連する問題