Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

In the UsernamePasswordAuthenticationFilter, the authRequest is added as an attribute to the HttpServletRequest. #16444

Open
amm0124 opened this issue Jan 19, 2025 · 1 comment
Assignees
Labels
in: core An issue in spring-security-core status: waiting-for-feedback We need additional information before we can continue type: enhancement A general enhancement

Comments

@amm0124
Copy link

amm0124 commented Jan 19, 2025

Expected Behavior

You expect to receive username and password in the application/json format and be able to extract them again in the unsuccessHandler.

In traditional HTML forms, the default encoding is x-www-urlencoded, but nowadays, application/json format is more commonly preferred in web development. While it is up to the developer to customize UsernamePasswordAuthenticationFilter to handle application/json format for username and password, an issue arises.

The HttpServletRequest in application/json format is a consumable stream, meaning once the data is extracted, it cannot be extracted again. This leads to a problem where, in the unsuccessHandler, you are unable to perform subsequent actions (e.g., incrementing account error counts for the username) because the stream has already been consumed.

To solve this problem, developers may find that completely customizing the filter is inefficient and means they are missing out on useful functionality provided by Spring Security.

Therefore, if authRequest is saved in the HttpServletRequest in UsernamePasswordAuthenticationFilter, it would resolve this issue.

Context

After receiving the login credentials in the application/json format for form login, you'd like to use the username and password in the unsuccessHandler for subsequent processing like error count increments.

public class CustomLoginFailureHandler implements AuthenticationFailureHandler {
	
	@Override
	public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException {
	
		UsernamePasswordAuthenticationToken authRequest = (UsernamePasswordAuthenticationToken) request.getAttribute("authRequest");
		// For example, you can extract the username using authRequest.getPrincipal() 
		// and increment the account failure count.
		
}
@amm0124 amm0124 added status: waiting-for-triage An issue we've not yet triaged type: enhancement A general enhancement labels Jan 19, 2025
@rwinch
Copy link
Member

rwinch commented Jan 21, 2025

I think that a better way to do this would be to add a new optional propertyAuthentication authenticationRequest to the AuthenticationException. Then in the AuthenticationProvider it would include the Authentication that was passed in to it in the AuthenticationException that is thrown when authentication fails. The failure would then be able to access the authenticationRequest property.

What do you think? Would you be interested in creating a pull request for this?

@rwinch rwinch added status: waiting-for-feedback We need additional information before we can continue in: core An issue in spring-security-core and removed status: waiting-for-triage An issue we've not yet triaged labels Jan 21, 2025
@rwinch rwinch self-assigned this Jan 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core An issue in spring-security-core status: waiting-for-feedback We need additional information before we can continue type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

2 participants