Created
December 6, 2024 10:39
-
-
Save thomasdarimont/75a55e8b4b61ef3aa841b4232af93b6c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Configuration | |
@EnableWebSecurity | |
public class WebSecurityConfig extends WebSecurityConfigurerAdapter { | |
static final String LOGIN_FORM_URL = "/login"; | |
static final String TARGET_AFTER_SUCCESSFUL_LOGIN_PARAM = "target"; | |
static final String COLOUR_PARAM = "colour"; | |
private final CookieSecurityContextRepository cookieSecurityContextRepository; | |
private final LoginWithTargetUrlAuthenticationEntryPoint loginWithTargetUrlAuthenticationEntryPoint; | |
private final RedirectToOriginalUrlAuthenticationSuccessHandler redirectToOriginalUrlAuthenticationSuccessHandler; | |
private final InMemoryAuthenticationProvider inMemoryAuthenticationProvider; | |
protected WebSecurityConfig(CookieSecurityContextRepository cookieSecurityContextRepository, | |
LoginWithTargetUrlAuthenticationEntryPoint loginWithTargetUrlAuthenticationEntryPoint, | |
RedirectToOriginalUrlAuthenticationSuccessHandler redirectToOriginalUrlAuthenticationSuccessHandler, | |
InMemoryAuthenticationProvider inMemoryAuthenticationProvider) { | |
super(); | |
this.cookieSecurityContextRepository = cookieSecurityContextRepository; | |
this.loginWithTargetUrlAuthenticationEntryPoint = loginWithTargetUrlAuthenticationEntryPoint; | |
this.redirectToOriginalUrlAuthenticationSuccessHandler = redirectToOriginalUrlAuthenticationSuccessHandler; | |
this.inMemoryAuthenticationProvider = inMemoryAuthenticationProvider; | |
} | |
@Override | |
protected void configure(HttpSecurity http) throws Exception { | |
http | |
// deactivate session creation | |
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS) | |
.and().csrf().disable() | |
// store SecurityContext in Cookie / delete Cookie on logout | |
.securityContext().securityContextRepository(cookieSecurityContextRepository) | |
.and().logout().permitAll().deleteCookies(SignedUserInfoCookie.NAME) | |
// deactivate RequestCache and append originally requested URL as query parameter to login form request | |
.and().requestCache().disable() | |
.exceptionHandling().authenticationEntryPoint(loginWithTargetUrlAuthenticationEntryPoint) | |
// configure form-based login | |
.and().formLogin() | |
.loginPage(LOGIN_FORM_URL) | |
// after successful login forward user to originally requested URL | |
.successHandler(redirectToOriginalUrlAuthenticationSuccessHandler) | |
.and().authorizeRequests() | |
.antMatchers(LOGIN_FORM_URL).permitAll() | |
.antMatchers("/**").authenticated(); | |
} | |
@Override | |
protected void configure(AuthenticationManagerBuilder auth) throws Exception { | |
auth.authenticationProvider(inMemoryAuthenticationProvider); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment