Skip to content

Commit 6e65ba4

Browse files
aarmamSanderKondratjevNortal
authored andcommitted
Token parsing error currently bubbles up as an IOException. AbstractAuthenticationProcessingFilter only routes AuthenticationException through the failure handler
1 parent 7a6e2ce commit 6e65ba4

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

example/src/main/java/eu/webeid/example/security/WebEidAjaxLoginProcessingFilter.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import org.springframework.http.MediaType;
3939
import org.springframework.security.authentication.AuthenticationManager;
4040
import org.springframework.security.authentication.AuthenticationServiceException;
41+
import org.springframework.security.authentication.BadCredentialsException;
4142
import org.springframework.security.core.Authentication;
4243
import org.springframework.security.core.AuthenticationException;
4344
import org.springframework.security.core.context.SecurityContextHolder;
@@ -69,21 +70,29 @@ public WebEidAjaxLoginProcessingFilter(
6970

7071
@Override
7172
public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response)
72-
throws AuthenticationException, IOException {
73+
throws AuthenticationException {
7374
final String contentType = request.getHeader(HttpHeaders.CONTENT_TYPE);
7475
if (contentType == null || !contentType.startsWith(MediaType.APPLICATION_JSON_VALUE)) {
7576
LOG.warn("Content type not supported: {}", contentType);
7677
throw new AuthenticationServiceException("Content type not supported: " + contentType);
7778
}
7879

7980
LOG.info("attemptAuthentication(): Reading request body");
80-
final WebEidAuthToken webEidAuthToken = OBJECT_READER.readValue(request.getReader());
81+
final WebEidAuthToken webEidAuthToken = parseWebEidAuthToken(request);
8182
LOG.info("attemptAuthentication(): Creating token");
8283
final PreAuthenticatedAuthenticationToken token = new PreAuthenticatedAuthenticationToken(null, webEidAuthToken);
8384
LOG.info("attemptAuthentication(): Calling authentication manager");
8485
return getAuthenticationManager().authenticate(token);
8586
}
8687

88+
private WebEidAuthToken parseWebEidAuthToken(HttpServletRequest request) {
89+
try {
90+
return OBJECT_READER.readValue(request.getReader());
91+
} catch (IOException e) {
92+
throw new BadCredentialsException("Unable to authenticate the Web eID authentication token", e);
93+
}
94+
}
95+
8796
@Override
8897
protected void successfulAuthentication(HttpServletRequest request, HttpServletResponse response, FilterChain chain, Authentication authResult) throws IOException, ServletException {
8998
super.successfulAuthentication(request, response, chain, authResult);

0 commit comments

Comments
 (0)