Jason
Jason

Reputation: 2076

How do I define http "security = 'none' in JavaConfig?

I want to define something similar to this XML in Spring Boot using Java Config.

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

I need a different way of securing them and cannot do form-logins. Securing them will come later. For now, I want to stop securing them with http.formLogin().

I have overridden WebSecurityConfigurerAdapter.configure() as such. I cannot find an API to say, like this:

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

Here is my configure() method now:

@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());

}

Upvotes: 7

Views: 7300

Answers (1)

Artem Bilan
Artem Bilan

Reputation: 121282

According to the 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 {
}

You can do that like this:

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

Upvotes: 20

Related Questions