改造CAS单点登录 --- 验证码

至于CAS原理问题这就不多介绍了,想了解的可以参考:

http://blog.chinaunix.net/uid-22816738-id-3525939.html

1.添加支持验证码jar包

在cas-server-core的pom.xml添加


           com.google.code.kaptcha 
           kaptcha 
           2.3 
           jdk15 
        

 

2.web.xml添加验证码映射


		Kaptcha
		com.google.code.kaptcha.servlet.KaptchaServlet
		
			kaptcha.border
			no
		
		
			kaptcha.textproducer.char.space
			5
		
		
			kaptcha.textproducer.char.length
			5
		
	
	
	
		Kaptcha
		/captcha.jpg
	


3.UsernamePasswordCredentials增加验证码属性

  /** The authcode. */
    @NotNull
    @Size(min=1, message = "required.authcode")
    private String authcode;
    
	public String getAuthcode() {
		return authcode;
	}

	public void setAuthcode(String authcode) {
		this.authcode = authcode;
	}

	/**
     * @return Returns the password.
     */
    public final String getPassword() {
        return this.password;
    }
 @Override
    public boolean equals(final Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        UsernamePasswordCredentials that = (UsernamePasswordCredentials) o;

        if (password != null ? !password.equals(that.password) : that.password != null) return false;
        if (username != null ? !username.equals(that.username) : that.username != null) return false;
        if (authcode != null ? !authcode.equals(that.authcode) : that.authcode != null) return false;
        return true;
    }

    @Override
    public int hashCode() {
        int result = username != null ? username.hashCode() : 0;
        result = 31 * result + (password != null ? password.hashCode() : 0);
        result = 31 * result + (authcode != null ? authcode.hashCode() : 0);
        return result;
    }



4.AuthenticationViaFormAction增加验证方法

public final String validatorCode(final RequestContext context,  final Credentials credentials, final MessageContext messageContext) throws Exception { 
    	final HttpServletRequest request = WebUtils.getHttpServletRequest(context);
        HttpSession session = request.getSession();
        String authcode = (String)session.getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);
		session.removeAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);
        
		UsernamePasswordCredentials upc = (UsernamePasswordCredentials)credentials;
		String submitAuthcode =upc.getAuthcode();
        if(!StringUtils.hasText(submitAuthcode) || !StringUtils.hasText(authcode)){
        	populateErrorsInstance(new NullAuthcodeAuthenticationException(),messageContext);
        	return "error";  
        }
        if(submitAuthcode.equals(authcode)){  
        	return "success";
        }
        populateErrorsInstance(new BadAuthcodeAuthenticationException(),messageContext);
        return "error";  
    }


NullAuthcodeAuthenticationException 、BadAuthcodeAuthenticationException为定义的异常类,取得异常编码

 

/*
 * Licensed to Jasig under one or more contributor license
 * agreements. See the NOTICE file distributed with this work
 * for additional information regarding copyright ownership.
 * Jasig licenses this file to you 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 the following location:
 *
 *   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.jasig.cas.authentication.handler;

import org.jasig.cas.ticket.TicketException;

/**
 * The exception to throw when we know the authcode is null
 * 
 * @author Scott Battaglia
 * @version $Revision$ $Date$
 * @since 3.0
 */
public class NullAuthcodeAuthenticationException extends TicketException {
	
	/** Serializable ID for unique id. */
    private static final long serialVersionUID = 5501212207531289993L;

    /** Code description. */
    public static final String CODE = "required.authcode";

    /**
     * Constructs a TicketCreationException with the default exception code.
     */
    public NullAuthcodeAuthenticationException() {
        super(CODE);
    }

    /**
     * Constructs a TicketCreationException with the default exception code and
     * the original exception that was thrown.
     * 
     * @param throwable the chained exception
     */
    public NullAuthcodeAuthenticationException(final Throwable throwable) {
        super(CODE, throwable);
    }}


 

 

/*
 * Licensed to Jasig under one or more contributor license
 * agreements. See the NOTICE file distributed with this work
 * for additional information regarding copyright ownership.
 * Jasig licenses this file to you 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 the following location:
 *
 *   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.jasig.cas.authentication.handler;

import org.jasig.cas.ticket.TicketException;

/**
 * The exception to throw when we know the authcoe is not correct
 * 
 * @author Scott Battaglia
 * @version $Revision$ $Date$
 * @since 3.0
 */
public class BadAuthcodeAuthenticationException extends TicketException {
	
	/** Serializable ID for unique id. */
    private static final long serialVersionUID = 5501212207531289993L;

    /** Code description. */
    public static final String CODE = "error.authentication.authcode.bad";

    /**
     * Constructs a TicketCreationException with the default exception code.
     */
    public BadAuthcodeAuthenticationException() {
        super(CODE);
    }

    /**
     * Constructs a TicketCreationException with the default exception code and
     * the original exception that was thrown.
     * 
     * @param throwable the chained exception
     */
    public BadAuthcodeAuthenticationException(final Throwable throwable) {
        super(CODE, throwable);
    }}


5.login_webflow.xml 修改登录验证流程


        
            
            
            
        
        
            
        
		
            
        
	

	  
          
          
          
      

 

6.增加国际化显示信息

在messages_zh_CN.properties文件中添加,其他国家语言类似添加

screen.welcome.label.authcode=\u9A8C\u8BC1\u7801:
screen.welcome.label.authcode.accesskey=a
required.authcode=\u5FC5\u987B\u5F55\u5165\u9A8C\u8BC1\u7801\u3002
error.authentication.authcode.bad=\u9A8C\u8BC1\u7801\u8F93\u5165\u6709\u8BEF\u3002

7.登录页面casLoginView.jsp添加验证码输入框

  <spring:message code=" οnclick="this.src='captcha.jpg?'+Math.random()" width="93" height="30" src="captcha.jpg">
" type="checkbox" />


ok,完成。启动看一下效果。

改造CAS单点登录 --- 验证码_第1张图片


 

你可能感兴趣的:(CAS)