refactor(auth): add comments, clearContext on logout, explain session auth
- Add comment to SecurityConfig explaining why CSRF is disabled - Add SecurityContextHolder.clearContext() to logout for clean thread state - Add Javadoc on authenticateInSession() explaining manual session setup Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -50,6 +50,11 @@ public class AuthController {
|
|||||||
return ResponseEntity.ok(ApiResponse.success(user));
|
return ResponseEntity.ok(ApiResponse.success(user));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates an authenticated Spring Security context and stores it in the HTTP session
|
||||||
|
* so that subsequent requests from the same session are recognised as authenticated.
|
||||||
|
* We do this manually because we are not using Spring Security's built-in form login.
|
||||||
|
*/
|
||||||
private void authenticateInSession(String email, String role, HttpServletRequest request) {
|
private void authenticateInSession(String email, String role, HttpServletRequest request) {
|
||||||
var auth = UsernamePasswordAuthenticationToken.authenticated(
|
var auth = UsernamePasswordAuthenticationToken.authenticated(
|
||||||
email, null, List.of(new SimpleGrantedAuthority("ROLE_" + role.toUpperCase())));
|
email, null, List.of(new SimpleGrantedAuthority("ROLE_" + role.toUpperCase())));
|
||||||
@@ -66,6 +71,7 @@ public class AuthController {
|
|||||||
if (session != null) {
|
if (session != null) {
|
||||||
session.invalidate();
|
session.invalidate();
|
||||||
}
|
}
|
||||||
|
SecurityContextHolder.clearContext();
|
||||||
return ResponseEntity.noContent().build();
|
return ResponseEntity.noContent().build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -18,6 +18,8 @@ public class SecurityConfig {
|
|||||||
@Bean
|
@Bean
|
||||||
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
|
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
|
||||||
http
|
http
|
||||||
|
// CSRF is disabled: SvelteKit is the only client and submits form actions
|
||||||
|
// server-side, so the browser never calls the backend directly.
|
||||||
.csrf(csrf -> csrf.disable())
|
.csrf(csrf -> csrf.disable())
|
||||||
.authorizeHttpRequests(auth -> auth
|
.authorizeHttpRequests(auth -> auth
|
||||||
.requestMatchers("/v1/auth/signup", "/v1/auth/login").permitAll()
|
.requestMatchers("/v1/auth/signup", "/v1/auth/login").permitAll()
|
||||||
|
|||||||
Reference in New Issue
Block a user