Spring Security supports multiple configurations in the same application. For example, assume the stateless service is completely located under the URL /api/. You could use the following outline for XML based configuration:
<http pattern="/api/**" create-session="stateless">
<intercept-url pattern="/**" access="hasRole('ADMIN')" />
<http-basic />
</http>
<http>
<intercept-url pattern="/**" access="authenticated" />
<form-login login-page="/login" default-target-url="/home.htm"/>
<logout />
</http>
or the following for Java Configuration:
@EnableWebSecurity
public class MultiHttpSecurityConfig {
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) { 1
auth
.inMemoryAuthentication()
.withUser("user").password("password").roles("USER").and()
.withUser("admin").password("password").roles("USER", "ADMIN");
}
@Configuration
@Order(1) 2
public static class ApiWebSecurityConfigurationAdapter extends WebSecurityConfigurerAdapter {
protected void configure(HttpSecurity http) throws Exception {
http
.antMatcher("/api/**") 3
.authorizeRequests()
.anyRequest().hasRole("ADMIN")
.and()
.httpBasic();
}
}
@Configuration 4
public static class FormLoginWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.anyRequest().authenticated()
.and()
.formLogin();
}
}
}