如何在JavaConfig中定义http“security ='none'?

时间:2015-02-11 13:23:54

标签: spring spring-security spring-boot spring-java-config

我想使用Java Config在Spring Boot中定义类似于此XML的内容。

<http pattern="/webservices/**" security="none"/>

我需要一种不同的方法来保护它们,而不能进行表单登录。保护他们将在以后到来。现在,我想用http.formLogin()停止保护它们。

我已经覆盖了WebSecurityConfigurerAdapter.configure()。我无法找到一个API,如下所示:

http.securityNone().antMatchers("/webservices")

现在是我的configure()方法:

@Override
protected void configure(HttpSecurity http) throws Exception
    http.csrf().disable(); 
    http.headers().frameOptions().disable();

    http.authorizeRequests()
          .antMatchers("/resources/**", 
//"/services/**",  THIS LOGS IN AUTOMATICALLY AS 'ANONYMOUS'.  NO GOOD!
//"/remoting/**",  THIS LOGS IN AUTOMATICALLY AS 'ANONYMOUS'.  NO GOOD!
                      "/pages/login",
                      "/pages/loginFailed").permitAll()

    // Only authenticated can access these URLs and HTTP METHODs
    .antMatchers("/secured/**").authenticated()
    .antMatchers(HttpMethod.HEAD,"/**").authenticated()
    .antMatchers(HttpMethod.GET,"/**").authenticated()
    .antMatchers(HttpMethod.POST,"/**").authenticated()
    .antMatchers(HttpMethod.PUT,"/**").authenticated()
    .antMatchers(HttpMethod.DELETE,"/**").authenticated();

    // Accept FORM-based authentication
    http.formLogin()
        .failureUrl("/pages/loginFailed")
        .defaultSuccessUrl("/")
        .loginPage("/pages/login")
        .permitAll()
        .and()
        .logout()
        .logoutRequestMatcher(new AntPathRequestMatcher("/pages/logout"))
        .logoutSuccessUrl("/pages/login")
        .permitAll();

    http.formLogin().successHandler(new AppLoginSuccessHandler());

}

1 个答案:

答案 0 :(得分:14)

根据WebSecurityConfigurerAdapter JavaDocs:

/**
 * Override this method to configure {@link WebSecurity}. For
 * example, if you wish to ignore certain requests.
 */
public void configure(WebSecurity web) throws Exception {
}

你可以这样做:

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