19
19
20
20
import jakarta .servlet .ServletRequest ;
21
21
import jakarta .servlet .ServletResponse ;
22
+ import jakarta .servlet .http .HttpServletRequest ;
23
+ import jakarta .servlet .http .HttpServletResponse ;
22
24
23
25
import org .springframework .security .core .Authentication ;
24
26
import org .springframework .security .core .context .SecurityContextHolder ;
27
+ import org .springframework .security .web .authentication .logout .LogoutHandler ;
25
28
import org .springframework .security .web .authentication .logout .SecurityContextLogoutHandler ;
26
29
import org .springframework .web .context .request .RequestContextHolder ;
27
30
import org .springframework .web .context .request .ServletRequestAttributes ;
@@ -32,14 +35,46 @@ public class DefaultDeAuthApplier implements DeAuthApplier
32
35
@ Override
33
36
public void deAuth (final ServletRequest request , final ServletResponse response , final Authentication auth )
34
37
{
38
+ // Ensure that current authentification is no longer usable
39
+ // Better crash the application than allow unauthorized access
35
40
SecurityContextHolder .getContext ().setAuthentication (null );
36
41
37
- Optional .ofNullable (RequestContextHolder .getRequestAttributes ())
38
- .filter (ServletRequestAttributes .class ::isInstance )
39
- .map (ServletRequestAttributes .class ::cast )
40
- .ifPresent (a -> new SecurityContextLogoutHandler ().logout (
41
- a .getRequest (),
42
- a .getResponse (),
43
- auth ));
42
+ // Find corresponding request and response
43
+ HttpServletRequest httpServletRequest = request instanceof final HttpServletRequest r ? r : null ;
44
+ HttpServletResponse httpServletResponse = response instanceof final HttpServletResponse r ? r : null ;
45
+
46
+ if (httpServletRequest == null || httpServletResponse == null )
47
+ {
48
+ // Fallback: Use RequestContextHolder
49
+ final Optional <ServletRequestAttributes > optServletRequestAttributes =
50
+ Optional .ofNullable (RequestContextHolder .getRequestAttributes ())
51
+ .filter (ServletRequestAttributes .class ::isInstance )
52
+ .map (ServletRequestAttributes .class ::cast );
53
+ if (optServletRequestAttributes .isPresent ())
54
+ {
55
+ final ServletRequestAttributes servletRequestAttributes = optServletRequestAttributes .get ();
56
+ if (httpServletRequest == null )
57
+ {
58
+ httpServletRequest = servletRequestAttributes .getRequest ();
59
+ }
60
+ if (httpServletResponse == null )
61
+ {
62
+ httpServletResponse = servletRequestAttributes .getResponse ();
63
+ }
64
+ }
65
+ }
66
+
67
+ // Execute logout
68
+ // https://docs.spring.io/spring-security/reference/servlet/authentication/logout.html#creating-custom-logout-endpoint
69
+ // This will invalidate the session and definitely kill the authentication
70
+ if (httpServletRequest != null )
71
+ {
72
+ this .getLogoutHandler ().logout (httpServletRequest , httpServletResponse , auth );
73
+ }
74
+ }
75
+
76
+ protected LogoutHandler getLogoutHandler ()
77
+ {
78
+ return new SecurityContextLogoutHandler ();
44
79
}
45
80
}
0 commit comments