Skip to content

Commit f4106c3

Browse files
authored
Merge pull request #60 from xdev-software/develop
Release
2 parents 2a3b6c3 + b8c4c4b commit f4106c3

File tree

2 files changed

+48
-9
lines changed

2 files changed

+48
-9
lines changed

CHANGELOG.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
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)
48
* This should only affect applications with anonymous auth enabled
59
* OAuth2-OIDC
610
* Do not register ``OAuth2RefreshFilter`` twice
7-
* DeAuth
11+
* DeAuth JS-556
812
* Apply correctly
913
* Make it possible to customize application
1014

1115
# 1.0.2
1216
* Vaadin
13-
* ``XHRReloadVaadinServiceInitListener``
17+
* ``XHRReloadVaadinServiceInitListener`` #45
1418
* Improved performance by not building element every request and cloning it instead
1519
* If an error occurs while the script is added to the document the error is now logged (once at WARN; all subsequent ones at DEBUG)
1620

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)