asdtiang的博客 感谢blogjava提供的博客交流平台

grails acegi 登录验证码的实现

Posted on 2010-07-09 11:57 asdtiang 阅读(916) 评论(0)  编辑  收藏
闲话不多说,直接上代码吧:
第一:如何更改acegi的源码:在STS 中工程视图改为project explorer ,在工程上点击右击,有个grails tools,里面刷新下依赖,在工程下就应该出现所有插件的源码了,这样就可以改了。
找到了下类:

改为如何下代码:我也记不清怎么改的了:

/* Copyright 2006-2009 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      
http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 
*/
package org.codehaus.groovy.grails.plugins.springsecurity;

import java.io.IOException;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.security.Authentication;
import org.springframework.security.AuthenticationException;
import org.springframework.security.providers.UsernamePasswordAuthenticationToken;
import org.springframework.security.ui.webapp.AuthenticationProcessingFilter;
import org.springframework.security.util.TextUtils;
import org.springframework.util.StringUtils;

/**
 * Extends the default {
@link AuthenticationProcessingFilter} to override the
 * <code>sendRedirect()</code> logic and always send absolute redirects.
 * 
 * 
@author Tsuyoshi Yamamoto
 
*/
public class GrailsAuthenticationProcessingFilter extends
        AuthenticationProcessingFilter {

    
private String _ajaxAuthenticationFailureUrl;

    
/**
     * {
@inheritDoc}
     * 
     * 
@see org.springframework.security.ui.AbstractProcessingFilter#doFilterHttp(javax.servlet.http.HttpServletRequest,
     *      javax.servlet.http.HttpServletResponse, javax.servlet.FilterChain)
     
*/
    @Override
    
public void doFilterHttp(final HttpServletRequest request,
            
final HttpServletResponse response, final FilterChain chain)
            
throws IOException, ServletException {
        SecurityRequestHolder.set(request, response);
        
try {
            
super.doFilterHttp(request, response, chain);
        } 
finally {
            SecurityRequestHolder.reset();
        }
    }

    
/**
     * {
@inheritDoc}
     * 
     * 
@see org.springframework.security.ui.AbstractProcessingFilter#sendRedirect(javax.servlet.http.HttpServletRequest,
     *      javax.servlet.http.HttpServletResponse, java.lang.String)
     
*/
    @Override
    
protected void sendRedirect(final HttpServletRequest request,
            
final HttpServletResponse response, final String url)
            
throws IOException {
        RedirectUtils.sendRedirect(request, response, url);
    }

    
/**
     * {
@inheritDoc}
     * 
     * 
@see org.springframework.security.ui.AbstractProcessingFilter#determineFailureUrl(javax.servlet.http.HttpServletRequest,
     *      org.springframework.security.AuthenticationException)
     
*/
    @Override
    
protected String determineFailureUrl(final HttpServletRequest request,
            
final AuthenticationException failed) {
        String url 
= super.determineFailureUrl(request, failed);
        
if (getAuthenticationFailureUrl().equals(url)
                
&& AuthorizeTools.isAjax(request)) {
            url 
= StringUtils.hasLength(_ajaxAuthenticationFailureUrl) ? _ajaxAuthenticationFailureUrl
                    : getAuthenticationFailureUrl();
        }
        
return url;
    }

    
/**
     * Dependency injection for the Ajax auth fail url.
     * 
     * 
@param url
     *            the url
     
*/
    
public void setAjaxAuthenticationFailureUrl(final String url) {
        _ajaxAuthenticationFailureUrl 
= url;
    }

    
public Authentication attemptAuthentication(HttpServletRequest request)
            
throws AuthenticationException {
        String inputValidationCode 
= request.getParameter( "captcha" );
        
//从Session中取出验证码
        String ssnValidationCode = (String)request.getSession().getAttribute( "captcha" );

        
if( ssnValidationCode != null && !ssnValidationCode.equals( inputValidationCode.toUpperCase() ) ){
            
//用户输入的值与看到的不一致,抛出异常
            throw new ValidationCodeException( "验证码输入错误!");
        } 
        String username 
= obtainUsername(request);
        String password 
= obtainPassword(request);
        System.out.println(
"testestesetsetestest:"+inputValidationCode);
        
if (username == null) {
            username 
= "";
        }

        
if (password == null) {
            password 
= "";
        }

        username 
= username.trim();

        UsernamePasswordAuthenticationToken authRequest 
= new UsernamePasswordAuthenticationToken(
                username, password);

        
// Place the last username attempted into HttpSession for views
        HttpSession session = request.getSession(false);

        
if (session != null || getAllowSessionCreation()) {
            request.getSession().setAttribute(
                    SPRING_SECURITY_LAST_USERNAME_KEY,
                    TextUtils.escapeEntities(username));
        }

        
// Allow subclasses to set the "details" property
        setDetails(request, authRequest);

        
return this.getAuthenticationManager().authenticate(authRequest);
    }
}

然后再在这外包下面加一个类,其实就是抛出的一个异常类
package org.codehaus.groovy.grails.plugins.springsecurity;
import org.springframework.security.AuthenticationException;
/**
*
*/
public class ValidationCodeException extends AuthenticationException {
    
public ValidationCodeException(String s) {
        
super(s);
    }
}
这样就可以了,前台加上代码就行了。
不过,这样还不算玩,如果重做系统之类的,还得改,因为改的代码保存是在C盘。这也让我郁闷啊。这几天换机子,机子坏,改了好几遍了。
记得FCKeditor有上传图片显示不能显示的问题,今天重先发布工程时,网上下载的fckeditor,没改源码也能正常使用了,有点因祸得福的感觉。

天苍苍,野茫茫,风吹草底见牛羊

只有注册用户登录后才能发表评论。


网站导航:
 

posts - 80, comments - 24, trackbacks - 0, articles - 32

Copyright © asdtiang

asdtiang的博客 PaidMailz
点击广告网赚A(每天4个广告,每个0.0025美元,一个搜索广告0.03美元)