Using the above answer as a guide, this is a working example that I created from a postprocessor that allows you to specify which login form variables to provide for the authenticator, and a user authenticator example that checks the value of the terms_of_service flag in the login form.
In the Spring configuration:
<bean id="authFormDetailsPostProcessor" class="com.sefaira.authauth.AuthFormDetailsPostProcessor"> <property name="formVarNames" value="terms_of_service_accepted"/> </bean>
AuthFormDetailsPostProcessor.java:
public class AuthFormDetailsPostProcessor implements BeanPostProcessor { private String [] formVarNames; public void setFormVarNames (String formVarNames) { this.formVarNames = formVarNames.split (","); } public static class Details extends WebAuthenticationDetails { private Map<String, String> map; public Details (HttpServletRequest request, String [] parameters) { super (request); this.map = new HashMap<String, String>(); for (String parameter : parameters) { this.map.put (parameter.trim(), request.getParameter (parameter.trim())); } } public String get (String name) { return map.get(name); } } public Object postProcessAfterInitialization(Object bean, String name) { if (bean instanceof UsernamePasswordAuthenticationFilter) { ((UsernamePasswordAuthenticationFilter)bean).setAuthenticationDetailsSource( new AuthenticationDetailsSource() { public Object buildDetails(Object context) { if (formVarNames == null) { throw new RuntimeException ("AuthFormDetailsPostProcessor bean requires a formVarNames property, specifying a comma-delimited list of form vars to provide in the details object."); } return new Details ((HttpServletRequest) context, formVarNames); } }); } return bean; } public Object postProcessBeforeInitialization(Object bean, String name) { return bean; } }
This is a custom Authenticator that uses it:
public class AuthServiceAuthenticator implements AuthenticationProvider { @Override public Authentication authenticate (Authentication authentication) throws AuthenticationException { String email = (String) authentication.getPrincipal(); String password = (String) authentication.getCredentials(); AuthFormDetailsPostProcessor.Details details = (AuthFormDetailsPostProcessor.Details) authentication.getDetails();
source share