ぺんぎんらぼ

お笑いとマンガ好きなしょぼしょぼWeb系エンジニアの日記です。たまに絵を描きます。

お笑いとマンガ好きなしょぼしょぼWeb系エンジニアの日記です

一時的にSpringSecurityによる認証を無効化したい

一時的にSpringSecurityによる認証を無効化したいときは、 スプリングブートアプリケーションのクラスに@SpringBootApplicationを付与する。

@SpringBootApplication(exclude = SecurityAutoConfiguration.class) // これ
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

ただし自分の環境では、これだけではSpringSecurityのFilterが起動してしまう、 かつ、WebSecurityConfigurerをコメントアウトすれば起動しない、 という状態だったので、WebSecurityConfigurer クラスに@Profileを付与することで無効化した。

@Profile("production") // これ
@Configuration
@EnableWebSecurity
public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter{

@Profileは環境依存ファイルを切り替えるのに使用するが、デフォルトは"default"プロファイルが使われるため、@Profile("production")を設定しておくことで、開発時にWebSecurityConfigurer クラスが使用されないようにした。