Skip to content

Commit 245a1a4

Browse files
author
rcsoyer
committed
introduce a custom spring security event to ensure a cleaner code base, that consumes and remove logic for specific events, as well as releasing that security event as a default, expected type of object of the spring security context
1 parent b6713d8 commit 245a1a4

3 files changed

Lines changed: 28 additions & 9 deletions

File tree

src/main/java/org/acme/authorization_server/application/events/AuthenticationEventHandler.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@
22

33
import lombok.RequiredArgsConstructor;
44
import lombok.extern.slf4j.Slf4j;
5+
import org.acme.authorization_server.domain.dto.command.AppAuthenticationSuccessEvent;
56
import org.acme.authorization_server.domain.service.UserAuthenticationEventService;
67
import org.springframework.context.event.EventListener;
78
import org.springframework.security.authentication.event.AbstractAuthenticationFailureEvent;
8-
import org.springframework.security.authentication.event.AuthenticationSuccessEvent;
9-
import org.springframework.security.core.Authentication;
109
import org.springframework.security.core.context.SecurityContextHolder;
1110
import org.springframework.stereotype.Component;
1211

@@ -18,16 +17,13 @@ class AuthenticationEventHandler {
1817
private final UserAuthenticationEventService service;
1918

2019
@EventListener
21-
void onSuccess(final AuthenticationSuccessEvent loginSuccessEvent) {
22-
final Authentication authentication = loginSuccessEvent.getAuthentication();
23-
SecurityContextHolder.getContext().setAuthentication(authentication);
24-
log.debug("Account successfully authenticated. And user set to the security context. username={}",
25-
authentication.getName());
20+
void onSuccess(final AppAuthenticationSuccessEvent loginSuccessEvent) {
2621
service.createSuccessEvent(loginSuccessEvent);
2722
}
2823

2924
@EventListener
3025
void onFailure(final AbstractAuthenticationFailureEvent loginFailureEvent) {
26+
SecurityContextHolder.clearContext();
3127
service.createFailureEvent(loginFailureEvent);
3228
}
3329
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package org.acme.authorization_server.domain.dto.command;
2+
3+
import java.io.Serial;
4+
import org.springframework.security.authentication.event.AuthenticationSuccessEvent;
5+
import org.springframework.security.core.Authentication;
6+
7+
/**
8+
* Markup class, extension of {@link AuthenticationSuccessEvent}
9+
* for the events exposed explicitly, programmatically, by this application.
10+
* <br/> This simple markup has the purpose of being consumed explicitly
11+
* as an event by this application event handlers,
12+
* and by the Spring Boot context as a default {@link AuthenticationSuccessEvent}.
13+
*/
14+
public final class AppAuthenticationSuccessEvent extends AuthenticationSuccessEvent {
15+
16+
@Serial
17+
private static final long serialVersionUID = 3403943457124822906L;
18+
19+
public AppAuthenticationSuccessEvent(final Authentication authentication) {
20+
super(authentication);
21+
}
22+
}

src/main/java/org/acme/authorization_server/domain/service/UserAuthenticationEventService.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ public class UserAuthenticationEventService {
2121
private final UserRepository userRepository;
2222

2323
public void createSuccessEvent(final AuthenticationSuccessEvent event) {
24-
log.debug("Persisting User Authentication success event published by spring's security context. "
25-
+ "AuthenticationSuccessEvent={}", event);
24+
log.debug(
25+
"Persisting User Authentication success event published by spring's security context. "
26+
+ "AuthenticationSuccessEvent={}", event);
2627
final String username = event.getAuthentication().getName();
2728
userRepository
2829
.findByUsername(username)

0 commit comments

Comments
 (0)