アノテーション型 EnableWebSecurity


  • @Retention(RUNTIME)
    @Target(TYPE)
    @Documented
    @Import({WebSecurityConfiguration.class,org.springframework.security.config.annotation.web.configuration.SpringWebMvcImportSelector.class,org.springframework.security.config.annotation.web.configuration.OAuth2ImportSelector.class,org.springframework.security.config.annotation.web.configuration.HttpSecurityConfiguration.class})
    @EnableGlobalAuthentication
    @Configuration
    public @interface EnableWebSecurity
    このアノテーションを @Configuration クラスに追加して、Spring Security 構成を任意の WebSecurityConfigurer で定義するか、WebSecurityConfigurerAdapter 基本クラスを継承し、個々のメソッドをオーバーライドすることで定義します。
     @Configuration
     @EnableWebSecurity
     public class MyWebSecurityConfiguration extends WebSecurityConfigurerAdapter {
    
            @Override
            public void configure(WebSecurity web) throws Exception {
                    web.ignoring()
                    // Spring Security should completely ignore URLs starting with /resources/
                                    .antMatchers("/resources/**");
            }
    
            @Override
            protected void configure(HttpSecurity http) throws Exception {
                    http.authorizeRequests().antMatchers("/public/**").permitAll().anyRequest()
                                    .hasRole("USER").and()
                                    // Possibly more configuration ...
                                    .formLogin() // enable form based log in
                                    // set permitAll for all URLs associated with Form Login
                                    .permitAll();
            }
    
            @Override
            protected void configure(AuthenticationManagerBuilder auth) throws Exception {
                    auth
                    // enable in memory based authentication with a user named "user" and "admin"
                    .inMemoryAuthentication().withUser("user").password("password").roles("USER")
                                    .and().withUser("admin").password("password").roles("USER", "ADMIN");
            }
    
            // Possibly more overridden methods ...
     }
     
    導入:
    3.2
    関連事項:
    WebSecurityConfigurer, WebSecurityConfigurerAdapter
    • オプション要素のサマリー

      オプション要素  
      修飾子と型 オプションの要素 説明
      booleandebug
      Spring Security のデバッグサポートを制御します。
    • 要素の詳細

      • debug

        boolean debug
        Spring Security のデバッグサポートを制御します。デフォルトは false です。
        戻り値:
        true の場合、Spring Security でデバッグサポートを有効にします
        デフォルト:
        false