Skip to content

Commit b8c4c4b

Browse files
committed
DefaultDeAuthApplier: Use already present request/response if possible
+ Improve docs
1 parent b7d9bec commit b8c4c4b

2 files changed

Lines changed: 46 additions & 7 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# 1.0.4
2+
* OAuth2-OIDC
3+
* ``DefaultDeAuthApplier``: Use already present request/response if possible
4+
15
# 1.0.3
26
* Vaadin
37
* Fix ``VaadinOAuth2RefreshReloadCommunicator`` not always setting status code ``401`` (which causes ``xhrAdapter.js`` to ignore the response)

oauth2-oidc/src/main/java/software/xdev/sse/oauth2/filter/deauth/DefaultDeAuthApplier.java

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,12 @@
1919

2020
import jakarta.servlet.ServletRequest;
2121
import jakarta.servlet.ServletResponse;
22+
import jakarta.servlet.http.HttpServletRequest;
23+
import jakarta.servlet.http.HttpServletResponse;
2224

2325
import org.springframework.security.core.Authentication;
2426
import org.springframework.security.core.context.SecurityContextHolder;
27+
import org.springframework.security.web.authentication.logout.LogoutHandler;
2528
import org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler;
2629
import org.springframework.web.context.request.RequestContextHolder;
2730
import org.springframework.web.context.request.ServletRequestAttributes;
@@ -32,14 +35,46 @@ public class DefaultDeAuthApplier implements DeAuthApplier
3235
@Override
3336
public void deAuth(final ServletRequest request, final ServletResponse response, final Authentication auth)
3437
{
38+
// Ensure that current authentification is no longer usable
39+
// Better crash the application than allow unauthorized access
3540
SecurityContextHolder.getContext().setAuthentication(null);
3641

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();
4479
}
4580
}

0 commit comments

Comments
 (0)