Skip to content

Instantly share code, notes, and snippets.

@sandipchitale
Last active November 20, 2022 07:31
Show Gist options
  • Save sandipchitale/33365d9e5ae5311dac3d6d2c8d9d7508 to your computer and use it in GitHub Desktop.
Save sandipchitale/33365d9e5ae5311dac3d6d2c8d9d7508 to your computer and use it in GitHub Desktop.
SPA Return 401 instead of redirect to login #spring-security
@Bean
public SecurityFilterChain defaultSecurityFilterChain(HttpSecurity http) throws Exception {
LOGGER.debug("in configure HttpSecurity");
// Return HttpStatus.UNAUTHORIZED (401) to SPAs.
http.exceptionHandling(eh ->
eh.authenticationEntryPoint(new HttpStatusEntryPoint(HttpStatus.UNAUTHORIZED)));
// ...
http.formLogin(fl ->
fl
.successHandler((req, res, auth) -> res.setStatus(HttpStatus.OK.value()))
.failureHandler((req, res, auth) -> res.setStatus(HttpStatus.UNAUTHORIZED.value()))
);
return http.build();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment