Skip to content

Commit 8273772

Browse files
committed
Add PostAuthenticationEntryPoint
This is a handy implementation that allows an entry point to operate differently when there is already a known user in context. In some cases, it is not desireable to show the end user another form and ask them for their username when we already know it, for example.
1 parent b235c45 commit 8273772

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Copyright 2004-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.security.web.authentication;
18+
19+
import java.io.IOException;
20+
import java.util.Map;
21+
import java.util.function.Function;
22+
import java.util.stream.Collectors;
23+
24+
import jakarta.servlet.ServletException;
25+
import jakarta.servlet.http.HttpServletRequest;
26+
import jakarta.servlet.http.HttpServletResponse;
27+
28+
import org.springframework.security.core.Authentication;
29+
import org.springframework.security.core.AuthenticationException;
30+
import org.springframework.security.core.context.SecurityContextHolder;
31+
import org.springframework.security.core.context.SecurityContextHolderStrategy;
32+
import org.springframework.security.web.AuthenticationEntryPoint;
33+
import org.springframework.security.web.FormPostRedirectStrategy;
34+
import org.springframework.security.web.RedirectStrategy;
35+
import org.springframework.security.web.csrf.CsrfToken;
36+
import org.springframework.util.Assert;
37+
import org.springframework.web.util.UriComponentsBuilder;
38+
39+
public final class PostAuthenticationEntryPoint implements AuthenticationEntryPoint {
40+
41+
private final String entryPointUri;
42+
43+
private final Map<String, Function<Authentication, String>> params;
44+
45+
private SecurityContextHolderStrategy securityContextHolderStrategy = SecurityContextHolder
46+
.getContextHolderStrategy();
47+
48+
private RedirectStrategy redirectStrategy = new FormPostRedirectStrategy();
49+
50+
public PostAuthenticationEntryPoint(String entryPointUri, Map<String, Function<Authentication, String>> params) {
51+
this.entryPointUri = entryPointUri;
52+
this.params = params;
53+
}
54+
55+
@Override
56+
public void commence(HttpServletRequest request, HttpServletResponse response,
57+
AuthenticationException authException) throws IOException, ServletException {
58+
Authentication authentication = getAuthentication(authException);
59+
Assert.notNull(authentication, "could not find authentication in order to perform post");
60+
Map<String, String> params = this.params.entrySet()
61+
.stream()
62+
.collect(Collectors.toMap(Map.Entry::getKey, (entry) -> entry.getValue().apply(authentication)));
63+
UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(this.entryPointUri);
64+
CsrfToken csrf = (CsrfToken) request.getAttribute(CsrfToken.class.getName());
65+
if (csrf != null) {
66+
builder.queryParam(csrf.getParameterName(), csrf.getToken());
67+
}
68+
String entryPointUrl = builder.build(false).expand(params).toUriString();
69+
this.redirectStrategy.sendRedirect(request, response, entryPointUrl);
70+
}
71+
72+
private Authentication getAuthentication(AuthenticationException authException) {
73+
Authentication authentication = authException.getAuthenticationRequest();
74+
if (authentication != null && authentication.isAuthenticated()) {
75+
return authentication;
76+
}
77+
authentication = this.securityContextHolderStrategy.getContext().getAuthentication();
78+
if (authentication != null && authentication.isAuthenticated()) {
79+
return authentication;
80+
}
81+
return null;
82+
}
83+
84+
}

0 commit comments

Comments
 (0)