jaas 认证过程说明

2023-05-06  本文已影响0人  后知不觉1

1、jaas auth认证

jaas auth认证是一个固定的执行过程,我们在其中可以自定义认证过程

2、认证过程

image.png

如上图所示,JAAS 认证过程分如下几个步骤:

1. 实例化一个 javax.security.auth.login.LoginContext 对象,它负责协调认证过程。该过程可用如下代码描述:

LoginContext lc = new LoginContext(name, new CallbackHandler());

2. LoginContext 获取安全配置文件(jaas.config),实例化 Configuration 对象。也可自定义继承方法javax.security.auth.login.Configuration 重写getConfiguration 或者 getAppConfigurationEntry 方法,在新建LoginContext作为参数传入

比如hadoop 的UserInformationGroup

static void loginUserFromSubject(Subject subject) throws IOException {
    ensureInitialized();
    try {
      if (subject == null) {
        subject = new Subject();
      }
      LoginContext login = newLoginContext(authenticationMethod.getLoginAppName(),
                          subject, new HadoopConfiguration());
      login.login();
      UserGroupInformationTest realUser = new UserGroupInformationTest(subject);
      realUser.setLogin(login);
      realUser.setAuthenticationMethod(authenticationMethod);
      realUser = new UserGroupInformationTest(login.getSubject());
      // If the HADOOP_PROXY_USER environment variable or property
      // is specified, create a proxy user as the logged in user.
      String proxyUser = System.getenv(HADOOP_PROXY_USER);
      if (proxyUser == null) {
        proxyUser = System.getProperty(HADOOP_PROXY_USER);
      }
      loginUser = proxyUser == null ? realUser : createProxyUser(proxyUser, realUser);

      String fileLocation = System.getenv(HADOOP_TOKEN_FILE_LOCATION);
      if (fileLocation != null) {
        // Load the token storage file and put all of the tokens into the
        // user. Don't use the FileSystem API for reading since it has a lock
        // cycle (HADOOP-9212).
        Credentials cred = Credentials.readTokenStorageFile(
            new File(fileLocation), conf);
        loginUser.addCredentials(cred);
      }
      loginUser.spawnAutoRenewalThreadForUserCreds();
    } catch (LoginException le) {
      LOG.debug("failure to login", le);
      throw new IOException("failure to login", le);
    }
    if (LOG.isDebugEnabled()) {
      LOG.debug("UGI loginUser:"+loginUser);
    } 
  }

如下为一个安全配置文件示例:

UsernamePassword {
   javax.security.examples.auth.spi.UsernamePasswordLoginModule required debug=true;
};

它通过系统参数传入:

-Djava.security.auth.login.config=jaas.config

3. LoginContext 通过 javax.security.auth.login.Configuration 对象获取 LoginModule 实例列表。通常 Configuration 对象的实现在实例化过程中加载解析配置文件。

4. Configuration 对象返回 LoginModule 实例列表

5. 客户端代码调运 LoginContext 的 login 方法

6. LoginContext 根据 Configuration 对象返回 LoginModule 实例列表,实例化 LoginModule。

7. LoginContext 调运 LoginModule 的 initialize 方法

8. LoginContext 调运 LoginModule 的 login 方法

9. LoginContext 调运 LoginModule 的 commit 方法

10. 认证的信息被保存在 Subject 中

上一篇 下一篇

猜你喜欢

热点阅读