CAS自定义拓展Credentials

我们在使用CAS的时候,除了用户名、密码可能还需要验证码等其他信息,CAS一般使用的Credentials是org.jasig.cas.authentication.principal.UsernamePasswordCredentials,这里只包含有:

/** The username. */
@NotNull
@Size(min=1,message = "required.username")
private String username;

/** The password. */
@NotNull
@Size(min=1, message = "required.password")
private String password;

所以如果要包含有验证码等信息,需要自自定Credentials。Credentials是一个接口,我们的实现方式是继承UsernamePasswordCredentials,当然也可以直接实现Credentials接口,代码如下:

public class MyCredentials extends UsernamePasswordCredentials {
    private static final long serialVersionUID = -8343864967200862794L;

    /**
     * The authCode.
     */
    private String authCode;

    @Override
    public boolean equals(final Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        MyCredentials that = (MyCredentials) o;

        if (getPassword() != null ? !getPassword().equals(that.getPassword()) : that.getPassword() != null)
            return false;
        if (getUsername() != null ? !getUsername().equals(that.getUsername()) : that.getUsername() != null)
            return false;
        if (authCode != null ? !authCode.equals(that.authCode) : that.authCode != null) return false;

        return true;
    }

	@Override
	public int hashCode() {
	    int result = getUsername() != null ? getUsername().hashCode() : 0;
	    result = 31 * result + (getPassword() != null ? getPassword().hashCode() : 0);
	    result = 31 * result + (authCode != null ? authCode.hashCode() : 0);
	    return result;
	}
}

接下来就是修改webflow配置:



……省略其他部分

       
           
           
           
       
       
           
       
	
	
           
       


……


	
	
	

这里只执行一个方法(myViaFormAction.validateAuthCode),如果执行成功则执行原来的realSubmit,在此就不在介绍。g

接下来,看下myViaFormAction是如何实现的:

public class MyViaFormAction extends AuthenticationViaFormAction {

    private static final Logger LOGGER = LoggerFactory.getLogger(MyViaFormAction.class);

    /**
     * 校验动态码
     */
    public final String validateAuthCode(final RequestContext context, final Credentials credential, final MessageContext messageContext) {
        final HttpServletRequest request = WebUtils.getHttpServletRequest(context);
        String authCode = request.getParameter("authCode");
        String username = request.getParameter("username");
        //判空校验
        if (!StringUtils.hasText(username) || !StringUtils.hasText(authCode)) {
            messageContext.addMessage(new MessageBuilder().error().code("required.authCode").build());
            return "error";
        }
        try {
            		//验证逻辑……
                    return "success";
        } catch (Exception e) {
            messageContext.addMessage(new MessageBuilder().error().code("error.authentication.authCode.bad").build());
            return "error";
        }
        messageContext.addMessage(new MessageBuilder().error().code("error.authentication.authCode.bad").build());
        return "error";
    }
}

这样就完成了验证码验证的拓展功能!

关于webflow的配置详解,我们下一篇文章再做详述!

你可能感兴趣的:(编程)