Grails ACEGI PLUGIN 登录扩展验证码


安装ACEGI插件以后,按照步骤一次执行了,
grails create-auth-domains User Role Requestmap
grails generate-manager
grails generate-registration

需要在登录页面扩展验证码,如下:

首先在登录页面的Form内加入如下代码:
<input type="text" name="captcha" id="captcha" size="8"/>
                            <img src="${createLink(controller:'captcha', action:'index')}" align="absmiddle"/>


然后修改plugin->acegi 0.4.1 ->src->groovy下的org.codehaus.groovy.grails.plugins.springsecurity.GrailsAuthenticationProcessingFilter的attemptAuthentication方法如下:
String inputValidationCode = httpServletRequest.getParameter( "captcha" );
        //从Session中取出验证码
        String ssnValidationCode = (String)httpServletRequest.getSession().getAttribute( "captcha" );

        if( ssnValidationCode != null && !ssnValidationCode.equals( inputValidationCode.toUpperCase() ) ){
            //用户输入的值与看到的不一致,抛出异常
            throw new ValidationCodeException( "验证码输入错误!");
        }

并且在相同的package下创建ValidationCodeException类:
package org.codehaus.groovy.grails.plugins.springsecurity
import org.springframework.security.AuthenticationException
/**
* 验证码异常
*/
public class ValidationCodeException extends AuthenticationException {
    public ValidationCodeException(String s) {
        super(s);
    }
}


再修改projects下的contoller - LoginController的authFail方法,添加如下代码:
else if(exception instanceof ValidationCodeException) {
  msg = exception.message


grails run-app,执行可看到效果。

你可能感兴趣的:(Security,grails,groovy,Acegi)