`
y806839048
  • 浏览: 1090185 次
  • 性别: Icon_minigender_1
  • 来自: 上海
文章分类
社区版块
存档分类
最新评论

cas login界面增加标记作用的属性(定义异常提示二)

    博客分类:
  • cas
cas 
阅读更多

cas login界面增加标记作用的属性

 

 

 

login界面增加传值字段(比如中英文标志)

 

 

<form:input cssClass="form-control"  type="hidden" path="webflag" autocomplete="false" htmlEscape="true" value="0"/>

 

 

<form:input cssClass="form-control" cssErrorClass="u_name" id="captcha" size="15" tabindex="1" type="text"

path="captcha" autocomplete="false" htmlEscape="true"  placeholder="验证码"/>

 

 

 

 

使EsteelsAuthenticationViaFormAction这个类有效及哪里使用的配置

 

cas-servlet.xml

<!--自定义验证(验证码) -->

  <bean id="authenticationViaFormAction" class="org.esteels.cas.validator.EsteelsAuthenticationViaFormAction"

        p:centralAuthenticationService-ref="centralAuthenticationService"

        p:warnCookieGenerator-ref="warnCookieGenerator"

        p:ticketRegistry-ref="ticketRegistry"/>

 

 

login-webflow.xml

 

 

 

  这个视图有什么属性,同时设置credential后增加的属性

 <!--自定义类加入Rememberme,验证码参数-->

    <var name="credential" class="org.esteels.cas.authentication.RemembermeAndCaptcha" />要增加的属性

<view-state id="viewLoginForm" view="casLoginView" model="credential">

        <binder>

            <binding property="username" />

            <binding property="password" />

            <!-- 增加验证码属性 -->

            <binding property="captcha" /> 

            <!-- 增加rememberMe属性 -->

            <binding property="rememberMe" />

            <!-- 判断中英文网站 -->

             <binding property="webflag" /> 

        </binder>

        <on-entry>

            <set name="viewScope.commandName" value="'credential'" />

        </on-entry>

<transition on="submit" bind="true" validate="true" to="EsteelsValidator"><!-- 自定义验证 -->

            <evaluate expression="authenticationViaFormAction.doBind(flowRequestContext, flowScope.credential)" />

        </transition>

</view-state>

 

  <action-state id="realSubmit">

    <evaluate expression="authenticationViaFormAction.submit(flowRequestContext, flowScope.credential, messageContext)" />

    <transition on="warn" to="warn" />

    <transition on="success" to="sendTicketGrantingTicket" />

    <transition on="successWithWarnings" to="showMessages" />

    <transition on="authenticationFailure" to="handleAuthenticationFailure" />

    <transition on="error" to="generateLoginTicket" />

  </action-state>

   <action-state id="EsteelsValidator">  

        <evaluate expression="authenticationViaFormAction.customValidator(flowRequestContext, flowScope.credential, messageContext)"></evaluate>  

        <transition on="error" to="generateLoginTicket" />  

        <transition on="success" to="realSubmit" />  

    </action-state>  

 

 

应用   (自定义异常)两种异常的处理方式不同,1,是通过配置文件指定用哪个异常类和方法 2,是通过继承AuthenticationViaFormAction类实现(见前文)

 

package org.esteels.cas.validator;

 

 

import javax.servlet.http.HttpServletRequest;  

import javax.servlet.http.HttpSession;

 

import org.esteels.cas.authentication.RemembermeAndCaptcha;

import org.jasig.cas.authentication.Credential;

import org.jasig.cas.web.flow.AuthenticationViaFormAction;  

import org.jasig.cas.web.support.WebUtils;  

import org.springframework.binding.message.MessageBuilder;  

import org.springframework.binding.message.MessageContext;  

import org.springframework.util.StringUtils;  

import org.springframework.webflow.execution.Event;  

import org.springframework.webflow.execution.RequestContext;  

import com.google.code.kaptcha.Constants;  

/**

 * 自定义验证(验证码)

 * @author  zhouwentong

 *

 */

public class EsteelsAuthenticationViaFormAction extends AuthenticationViaFormAction {  

    

public final Event customValidator(RequestContext context, Credential credential, MessageContext messageContext) {  

HttpServletRequest request =WebUtils.getHttpServletRequest(context); 

HttpSession session = request.getSession();  

 

String captcha = (String) session.getAttribute(Constants.KAPTCHA_SESSION_KEY);  

session.removeAttribute(Constants.KAPTCHA_SESSION_KEY);  

 

RemembermeAndCaptcha cuCredential = (RemembermeAndCaptcha) credential;  

String submitCaptcha= cuCredential.getCaptcha();  

String uname=cuCredential.getUsername();

String pass=cuCredential.getPassword();

int webflag=cuCredential.getWebflag();

if (!StringUtils.hasText(uname)) {

if(webflag==0){

messageContext.addMessage(new MessageBuilder().error().code("required.username").build());  

return new Event(this, ERROR);  

}else{

messageContext.addMessage(new MessageBuilder().error().code("required.username.en").build());  

return new Event(this, ERROR);  

}

}  

if (!StringUtils.hasText(pass)) {  

if(webflag==0){

messageContext.addMessage(new MessageBuilder().error().code("required.password").build());  

   return new Event(this, ERROR);  

}else{

messageContext.addMessage(new MessageBuilder().error().code("required.password.en").build());  

   return new Event(this, ERROR);

}

}  

if (!StringUtils.hasText(submitCaptcha)) {  

if(webflag==0){

messageContext.addMessage(new MessageBuilder().error().code("login.required.captcha").build());  

   return new Event(this, ERROR);  

}else{

messageContext.addMessage(new MessageBuilder().error().code("login.required.captcha.en").build());  

   return new Event(this, ERROR);  

}

}  

if (submitCaptcha.equals(captcha)) {  

   return new Event(this, SUCCESS);  

}else{

if(webflag==0){

messageContext.addMessage(new MessageBuilder().error().code("login.captcha.error").build());  

return new Event(this, ERROR);  

}else{

messageContext.addMessage(new MessageBuilder().error().code("login.captcha.error.en").build());  

return new Event(this, ERROR);  

}

 

}

}  

}  

 

 

package org.esteels.cas.authentication;

 

import org.jasig.cas.authentication.RememberMeUsernamePasswordCredential;

/**

 * 增加验证码 RememberMe字段

 * @author zhouwentong

 *

 */

public class RemembermeAndCaptcha extends RememberMeUsernamePasswordCredential{

private static final long serialVersionUID = 5034129937759981063L;  

 

   private String captcha;  

   private int webflag;//判断中英文网站

 

   public String getCaptcha() {  

       return captcha;  

   }  

 

   public void setCaptcha(String captcha) {  

       this.captcha = captcha;  

   }

 

public int getWebflag() {

return webflag;

}

 

public void setWebflag(int webflag) {

this.webflag = webflag;

}  

   

}

 

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics