java 获取加密表单,如何在Spring获取之前捕获Spring Security登录表单?

使用上面的答案作为指导,这是我创建的一个后处理器的工作示例,它允许您指定要向验证器提供的登录表单变量,以及一个示例自定义验证器,用于检查登录表单中的terms_of_service复选框值 .

在Spring配置中:

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 map;

public Details (HttpServletRequest request, String [] parameters) {

super (request);

this.map = new HashMap();

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;

}

}

这是使用它的自定义Authenticator:

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();

// see if they checked the terms_of_service checkbox

String termsOfServiceVar = details.get ("terms_of_service_accepted");

boolean termsOfServiceAccepted = (termsOfServiceVar != null && termsOfServiceVar.equals ("on"));

// ... do your custom authentication ...

return authentication; // or a new authentication object

}

@Override

public boolean supports(Class extends Object> authentication) {

return (UsernamePasswordAuthenticationToken.class.isAssignableFrom(authentication));

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值