I have done this. The solution is not complicated, but I did the trick by creating a custom security filter, mainly based on UserNamePasswordAuthenticationFilter.
In fact, you should override the tryAuthentication method. Just overriding getPassword and getUsername may not be enaugh, since you want to read the request body, and you must do this for both parameters at once (unless you are creating a multi-page HttpServletRequest shell)
The solution should be like this:
public class JsonUserNameAuthenticationFilter extends UsernamePasswordAuthenticationFilter{
Then you must configure it. I always use xml based configuration for such complex configurations,
<beans:bean id="jsonUserNamePasswordAuthenticationFilter" class="xxx.yyy.JsonUserNamePasswordAuthenticationFilter"> <beans:property name="authenticationFailureHandler> <beans:bean class="org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler"> </beans:bean> </beans:property> <beans:property name="authenticationManager" ref="mainAuthenticationManager" /> <beans:property name="authenticationSuccessHandler" > <beans:bean class="org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler"> </beans:bean> </beans:property> </beans:bean> <security:authentication-manager id="mainAuthenticationManager"> <security:authentication-provider ref="yourProvider" /> </security:authentication-manager> <security:http pattern="/login-error" security="none"/> <security:http pattern="/logout" security="none"/> <security:http pattern="/secured-pattern/**" auto-config='false' use-expressions="false" authentication-manager-ref="mainAuthenticationManager" create-session="never" entry-point-ref="serviceAccessDeniedHandler"> <security:intercept-url pattern="/secured-pattern/**" access="ROLE_REQUIRED" /> <security:custom-filter ref="jsonUserNamePasswordAuthenticationFilter" position="FORM_LOGIN_FILTER" /> <security:access-denied-handler ref="serviceAccessDeniedHandler"/> <security:csrf disabled="true"/> </security:http>
You can create some additional objects as an access denial handler, but this is the easiest part of the thing
source share