我应该在哪里存储令牌在客户端上

时间:2015-03-04 13:51:16

标签: javascript http authentication dropwizard

我在理解我应该存储令牌(客户端)的位置时遇到了一些问题。所以每次我在服务器上更改页面时都会发送它。

     @Override
    public User getValue(HttpContext c) {
      // This is where the credentials are extracted from the request
      final String header = c.getRequest().getHeaderValue(CUSTOM_HEADER);
      try {
        if (header != null) {
          final Optional<User> result = authenticator.authenticate(new Credentials(header,"",""));
          if (result.isPresent()) {
            return result.get();
          }
        }
      } catch (AuthenticationException e) {
        throw new WebApplicationException(Response.Status.UNAUTHORIZED);
      }

      if (required) {
        throw new WebApplicationException(Response.Status.UNAUTHORIZED);
      }

      System.out.println("NO TOKEN RETURNING NULL");

      return null;
    }
  }

这是我的身份验证在dropwizard中的样子。我需要使用HttpContext发送令牌。

所以,如果我尝试进入adress/securedpage。然后HttpContext应该有此令牌。因此服务器知道用户是否有权访问

所以从客户端成功登录后。我应该在哪里放置从服务器收到的令牌?

1 个答案:

答案 0 :(得分:1)

你有很多选择。这是理解利弊的良好环节 https://stormpath.com/blog/where-to-store-your-jwts-cookies-vs-html5-web-storage/

  1. 将其存储在本地浏览器存储中。如前一个链接所述,它存在一些风险。
  2. 将其存储在客户端的cookie中,它可以比本地浏览器存储更安全。
  3. 不要存储令牌,而是将用户名和密码存储在浏览器或客户端cookie中,然后通过静默发送凭据来检索新令牌。
  4. 使用像https://github.com/IdentityModel/oidc-token-manager这样的客户端Java库来依赖其令牌管理功能。默认情况下,它将检索到的令牌存储在localStorage中(在所有选项卡中都可用),并且可以使用silent_renew:true进行配置,以便它在使用iframe(无需显式浏览器重定向)到期之前自动刷新令牌。
相关问题