I've an application in spring/spring-mvc that totally uses JSON communications. Now I need to authenticate my application with spring security 3 (that uses LdapAuthenticationProvider) via JSON.
The default spring seurity submit form requires a POST like this:
POST /myapp/j_spring_security_check HTTP/1.1
Accept-Encoding: gzip,deflate
Content-Type: application/x-www-form-urlencoded
Content-Length: 32
Host: 127.0.0.1:8080
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.1.1 (java 1.5)
j_username=myUsername&j_password=myPass
But I want to pass a JSON object like this:
{"j_username":"myUsername","j_password":"myPass"}
I read many post like this, this other or this one without luck, in all ajax cases is done a POST like above.
Any Ideas?
Look at this example: https://github.com/fuhaiwei/springboot_security_restful_api
If you want just different request body parser for login request just extend
UsernamePasswordAuthenticationFilter
and overrideattemptAuthentication
method. By defaultUsernamePasswordAuthenticationFilter
will parse url encoded data and createUsernamePasswordAuthenticationToken
from it. Now you just need to make parser that will parse whatever you send to application.Here is example that will parse
{"username": "someusername", "password": "somepassword"}
In snippet request body is extracted to string and mapped to object
AuthReq
(@Data
annotation is from lombok lib, it will generate seters and getters). Than you can makeUsernamePasswordAuthenticationToken
that will be passed to defaultAuthenticationProvider
.Now you can extend
WebSecurityConfigurerAdapter
and override cnofigure method to replace old filter.With
addFilterAt
method you replace defaultUsernamePasswordAuthenticationFilter
. Dont forget to use@EnableWebSecurity
annotation.You can write your own security filter that will parse your JSON.
http://docs.spring.io/spring-security/site/docs/3.0.x/reference/core-web-filters.html
You can use the BasicAuthenticationFilter as a reference:
http://docs.spring.io/spring-security/site/docs/3.0.x/apidocs/org/springframework/security/web/authentication/www/BasicAuthenticationFilter.html
According with Kevin suggestions,
and after reading this posts: 1, 2, documentation 3, and thanks to this blog post,
I wrote my own FORM_LOGIN_FILTER to directly manage JSON before authentication.
I paste my code for the community.
The goal is to grant both the classical browser form POST authentication with JSON based authentication. Also in JSON authentication I want to avoid the redirect to loginSuccesful.htm
In context:
CustomUsernamePasswordAuthenticationFilter class:
CustomAuthenticationSuccessHandler class:
I applied the answers from fl4l and oe.elvik for login with JSON credentials in a Spring Boot application. I am working with annotation-based bean configuration.
In the referenced answers, a custom filter is created in which the authentication manager is injected. To do this, the authentication manager must be present as a Spring Bean. Here is a link on how to do that: https://stackoverflow.com/a/21639553/3950535.