The application logs all requested url
s. This means, that it's critical not to authenticate using url parameters, because it would cause the situation in which logs are full of pairs (login=abc&password=123)
. For this reason I've configured spring-security
to read parameters from request-body
. It's done by adding the following line to the request-header
:
'Content-Type': 'application/x-www-form-urlencoded'
The body will be:
{'login':'admin', 'password':'password'}
It's fine, but the QA forces me to disable the possibility of authentication via url paramters. At the moment a POST to the following URL will also authenticate:
https://example.com/foo?login=admin&password=password
Does anyone know a trick to disable this option? With an annotation preferably.
Due to the comment I decided to add some more details to my problem. My spring-security is configured with WebSecurityConfigurerAdapter
. I have
http.usernameParameter("login")
.passwordParameter("password")
(...)
This makes Spring
searching login data in both - parameters and body. I wish to disable searching those parameters in the url.
I believe this is not possible since this behaviour is not implemented by Spring rather than JavaEE itself.
HttpServletRequest.getParameter doc states:
But you can try to alter this with filter that should look something like this:
EDIT Haim Raman proposed another solution that uses existing filter instead of introducing a new one. Only I would suggest overriding
obtainUsername()
andobtainPassword()
instead ofattemptAuthentication()
.To the best of my knowledge and intuition, like jhan had mentioned, the appropriate solution would be to use annotation
@RequestMapping(value="/login", method="RequestMethod.POST")
. Then, no matter what parameters the user may pass with the URL, both the URL and URI will always default to /login. And that is what the logger will document. Not the username and password pairs, but"http://localhost:8080/login"
, or whatever your port is.I would like to suggest an alternative which is based on spring-security rater then a workaround as suggested by chimmi.
This answer provide a solution to the issue suggested by xenteros on bres26 answer as well
Override the exiting UsernamePasswordAuthenticationFilter implementation
You need to replace your own implementation with the existing one (see doc here)
Advantages: it’s based on spring security and flexible to changes.
Disadvantage: Unfortunately I found Spring Java Config very hard to set and to read
EDIT: I accepted chimmi comment and overridden obtainUsername and obtainPassword
You can find the source code in github.
You can achieve this by modifying the
UsernamePasswordAuthenticationFilter
'sRequestMatcher
. For example:NOTE: The requirement below does not prevent a GET request from being issued (and thus leaking the credentials). It is really up to the UI to ensure this doesn't happen.