I have tackled this issue with simple configuration, on Spring Security's end, and with Http Interceptor on angularjs's end:
Spring Security Configuration:
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf()
.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
.and()
.authorizeRequests()
.antMatchers("/somePage").hasRole("SOME_ROLE")
.and()
.formLogin()
.and()
.authorizeRequests()
.antMatchers("**/api/**").hasRole("SOME_ROLE")
.anyRequest().authenticated()
.and()
.httpBasic()
.and()
.logout()
.invalidateHttpSession(true)
.logoutSuccessUrl("/login")
.and()
.sessionManagement()
.maximumSessions(1);
}
@Bean
public HttpSessionEventPublisher httpSessionEventPublisher() {
return new HttpSessionEventPublisher();
}
angularjs code:
app.config(['$httpProvider',
function ($httpProvider) {
$httpProvider.interceptors.push('httpInterceptor');
...
}])
app.factory("httpInterceptor", ["$q", "$window", "$log",
function ($q, $window, $log) {
return {
responseError: function(response) {
switch (response.status) {
case 401:
case 403:
$window.location.reload();
return;
}
}
}
}]);
When combining formLogin() for pages, and httpBasic() for API calls
as well as managing sessions, you will be redirected to login page when you are accessing the site pages, and will get 401 Status Code on unauthenticated API calls, that can be caught by angular's interceptor, then you can reload the page or do whatever you need to do
Hope this helps :)