cas登陆页定义异常提示(中英文等

cas登陆页定义异常提示(中英文等):

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

不同异常的提示,区分中英文

1, 定义好自定义异常

2,在执行异常的地方抛出

3,定义好捕捉异常的处理器

 

 

 

property属性文件:

 

authenticationFailure.AccountNotFoundException=户名或密码错误.

authenticationFailure.FailedLoginException=户名或密码错误.

authenticationFailure.AccountNotFoundExceptionEn=Invalid credentials.

authenticationFailure.FailedLoginExceptionEn=Invalid credentials.

 

 

 

 

1, 定义好自定义异常

 

 

 

package org.esteels.cas.validator;

 

import javax.security.auth.login.LoginException;

 

/**

 * Signals that user authentication failed.

 *

 * <p> This exception is thrown by LoginModules if authentication failed.

 * For example, a <code>LoginModule</code> throws this exception if

 * the user entered an incorrect password.

 *

 */

public class FailedLoginExceptionEn extends LoginException {

 

    private static final long serialVersionUID = 802556922354616286L;

 

    /**

     * Constructs a FailedLoginException with no detail message. A detail

     * message is a String that describes this particular exception.

     */

    public FailedLoginExceptionEn() {

        super();

    }

 

    /**

     * Constructs a FailedLoginException with the specified detail

     * message.  A detail message is a String that describes this particular

     * exception.

     *

     * <p>

     *

     * @param msg the detail message.

     */

    public FailedLoginExceptionEn(String msg) {

        super(msg);

    }

}

 

 

 

 

2,在执行异常的地方抛出(这里覆写后要修改配置文件引用此类的路径)

 public class QueryDatabaseAuthenticationHandler

   extends AbstractJdbcUsernamePasswordAuthenticationHandler

 {

 {

   @NotNull

   private String sql;

   

   protected final HandlerResult authenticateUsernamePasswordInternal(UsernamePasswordCredential credential)

     throws GeneralSecurityException, PreventedException

   {

        RemembermeAndCaptcha cuCredential = (RemembermeAndCaptcha) credential; 

      String username = credential.getUsername();

              int webflag= cuCredential.getWebflag();

      String encryptedPassword = getPasswordEncoder().encode(credential.getPassword());

     try {

        String dbPassword = (String)getJdbcTemplate().queryForObject(this.sql, String.class, new Object[] { username });

        if (!dbPassword.equals(encryptedPassword)) {

if(webflag==0){///中英文

throw new FailedLoginException("Password does not match value on record.");//哪种异常

}else{

throw new FailedLoginExceptionEn("Password does not match value on record."); 

}

          

       }

     } catch (IncorrectResultSizeDataAccessException e) {

        if (e.getActualSize() == 0) {

          throw new AccountNotFoundException(username + " not found with SQL query");

       }

        throw new FailedLoginException("Multiple records found for " + username);

     }

     catch (DataAccessException e) {

        throw new PreventedException("SQL exception while executing query for " + username, e);

     }

      return createHandlerResult(credential, new SimplePrincipal(username), null);

   }

 

 

 

 

 

3,定义好捕捉异常的处理器

 

 public class AuthenticationExceptionHandler

 {

   private static final String UNKNOWN = "UNKNOWN";

   private static final String DEFAULT_MESSAGE_BUNDLE_PREFIX = "authenticationFailure.";

   private static final List<Class<? extends Exception>> DEFAULT_ERROR_LIST = new ArrayList();

   

 

    private final Logger logger = LoggerFactory.getLogger(getClass());

   

   static {

      DEFAULT_ERROR_LIST.add(AccountLockedException.class);

      DEFAULT_ERROR_LIST.add(FailedLoginException.class);

      DEFAULT_ERROR_LIST.add(CredentialExpiredException.class);

     DEFAULT_ERROR_LIST.add(AccountNotFoundException.class);

     DEFAULT_ERROR_LIST.add(AccountDisabledException.class);

     DEFAULT_ERROR_LIST.add(InvalidLoginLocationException.class);

     DEFAULT_ERROR_LIST.add(InvalidLoginTimeException.class);

              DEFAULT_ERROR_LIST.add(FailedLoginExceptionEn.class);

              DEFAULT_ERROR_LIST.add(AccountNotFoundExceptionEn.class);

   }

   }

 

cas-servlet.xml:

 <bean id="authenticationExceptionHandler" class="org.esteels.cas.authentication.AuthenticationExceptionHandler" />//类中要增加比对哪些异常(复写)

 

AuthenticationExceptionHandler这个源码里面根据异常类名拼接配置属性,并获取值

 

 

 

  <action-state id="handleAuthenticationFailure">

    <evaluate expression="authenticationExceptionHandler.handle(currentEvent.attributes.error, messageContext)" />

    <transition on="AccountDisabledException" to="casAccountDisabledView"/>

    <transition on="AccountLockedException" to="casAccountLockedView"/>

    <transition on="CredentialExpiredException" to="casExpiredPassView"/>

    <transition on="InvalidLoginLocationException" to="casBadWorkstationView"/>

    <transition on="InvalidLoginTimeException" to="casBadHoursView"/>

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

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

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

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

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

  </action-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="generateServiceTicket">

        <evaluate expression="generateServiceTicketAction" />

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

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

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

<transition on="gateway" to="gatewayServicesManagementCheck" />

</action-state>

 

 

被注入的这个到那个视图

<action-state id="generateLoginTicket">

        <evaluate expression="generateLoginTicketAction.generate(flowRequestContext)" />

<transition on="generated" to="viewLoginForm" />

</action-state>

 

 

  这个视图有什么属性

 

<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>

 

posted @ 2024-03-27 17:49  疯子110  阅读(112)  评论(0编辑  收藏  举报