asdtiang的博客 感謝blogjava提供的博客交流平臺

          grails acegi 登錄驗證碼的實現

          Posted on 2010-07-09 11:57 asdtiang 閱讀(930) 評論(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美元)
          主站蜘蛛池模板: 钟祥市| 宁强县| 韩城市| 丹凤县| 房山区| 抚顺市| 汽车| 美姑县| 沁水县| 洪湖市| 鹿泉市| 天峨县| 云阳县| 买车| 应城市| 太和县| 芜湖县| 洪泽县| 乐山市| 宜章县| 金堂县| 惠州市| 淳安县| 无极县| 德格县| 浙江省| 台湾省| 方城县| 文水县| 平南县| 扬州市| 克拉玛依市| 翼城县| 宿州市| 东兰县| 瑞丽市| 多伦县| 南通市| 聂拉木县| 双桥区| 昌都县|