文档中心 > API文档

1.9.1. 自定义登录认证 LoginAuthentication

登录验证器

典型应用场景如下:

  • AD/LDAP统一认证

  • 两步认证,使用手机短信、手机令牌或其他硬件进行双因素认证

  • CA认证实现

    /**
      * 认证,策略是:
      * 
      * <ol>
      *   <li>系统框架把登录界面发过来的<code>HttpServletRequest</code>完整的传递给认证实现类</li>
      *   <li>认证实现类完成自己的逻辑</li>
      *   <li>返回值约定
      *      <ol type="i">
      *        <li>当验证通过,需要给框架返回[用户名, 密码],框架将直接跳转到首页</li>
      *        <li>当不验证通过,但要终止本次登录请求,直接throw new LoginAuthenticationException(),用户将跳转到登录页</li>
      *        <li>当不验证通过,返回null,框架将调用下一个认证类认证</li>
      *      </ol>
      *   </li>
      * </ol>
      * 
      * @return 认证通过: 返回[用户名, 密码]; 验证不通过返回null,框架将调用下一个认证类认证
      * @throws LoginAuthenticationException 用户将跳转到登录页
      */
     String[] authenticate(HttpServletRequest request, HttpServletResponse response)
             throws LoginAuthenticationException;

系统缺省提供几个验证器,按顺序如下

验证器 说明
com.seeyon.ctp.portal.sso.login.SSOTicketLoginAuthentication 单点登录认证
com.seeyon.v3x.plugin.ca.CALoginAuthentication 使用CA认证
com.seeyon.ctp.login.IdentificationDogLoginAuthentication 使用身份验证狗认证
com.seeyon.apps.ldap.login.LDAPLoginAuthentication 使用LDAP认证
com.seeyon.ctp.login.auth.DefaultLoginAuthentication 使用协同用户系统认证

各验证器按顺序依次调用认证方法,只要有一个通过则该次登录认证通过。

要实现自己的登录认证,必须:

  1. 继承com.seeyon.ctp.login.AbstractLoginAuthentication,实现自己的登录验证类

    package com.seeyon.apps.login;
     
     import javax.servlet.http.HttpServletRequest;
     import javax.servlet.http.HttpServletResponse;
     
     import com.seeyon.ctp.common.constants.Constants;
     import com.seeyon.ctp.common.constants.LoginConstants;
     import com.seeyon.ctp.login.AbstractLoginAuthentication;
     import com.seeyon.ctp.login.LoginAuthenticationException;
     
     public class CustomLoginAuthentication extends AbstractLoginAuthentication {
         @Override
         public String[] authenticate(HttpServletRequest request,
                 HttpServletResponse response) throws LoginAuthenticationException {
             String username = request.getParameter(LoginConstants.USERNAME);// 用户名
             String password = request.getParameter(LoginConstants.PASSWORD);// 密码
             if (username == null || password == null) {
                 return null;
             }
             //登录方式,判断是否移动应用登陆
             String userAgentFrom = request.getParameter(Constants.LOGIN_USERAGENT_FROM);
             boolean fromMobile = Constants.login_useragent_from.mobile.name().equals(userAgentFrom) || LoginUtil.isFromM1(userAgentFrom);
     
             if (check(username, password)) {
                 return new String[] { username, password };
             }
     
             return null;
         }
     
         private boolean check(String username, String password) {
             // 登录认证逻辑,用户名和密码正确时返回true即可
             return false;
         }
     }
     
  2. 注册Spring bean

    在插件的springxml文件中,增加

        <bean class="com.seeyon.apps.login.CustomLoginAuthentication"/>