【问题标题】:Custom authentication in Java REST APIJava REST API 中的自定义身份验证
【发布时间】:2014-01-31 18:41:03
【问题描述】:

我正在尝试使用在 Tomcat 7 上运行的 Jersey 框架 (v2.5.1) 在我们的 Java REST API 中进行自己的身份验证的最佳方法。

API 将通过我们的 iOS 应用程序访问。在 iOS 应用程序中,我们使用 Facebook 身份验证(使用 Facebook SDK),然后在每次调用 REST API 时使用访问令牌。

@Provider
@Priority(Priorities.AUTHENTICATION)
public class AuthenticationFilter implements ContainerRequestFilter
{
    @Override
    public void filter(ContainerRequestContext requestContext) throws IOException
    {
        // Extract the access token from the HTTP header
        // Look up in the database to see if we have a user with that token
        // If there is a user found, proceed
        // If we can't find a user, we are going to send the token to Facebook to get the user details. If the token is invalid, we throw an exception. If it is valid, we look up if we can match the Facebook details with an existing user. When we can't match, we create a new user.
    }
}

这个过滤器将在每个 API 请求中执行。

我的问题:

  • 这是一个正确的工作流程吗?
  • 我们是否应该每次都联系 Facebook 来验证令牌?这将导致大量开销。
  • 此过滤器针对每个请求执行。我们如何排除某些 url(某些资源不需要身份验证)?我正在考虑在过滤器类中保存一组 url,并查看请求的 url 是否与定义的公共 url 匹配(如果是,则不要进行身份验证)。

谢谢!

【问题讨论】:

    标签: java facebook rest authentication jersey


    【解决方案1】:

    我认为如果您可以向您的用户提供此选项可能会更好: 使用 Facebook 帐户登录/登录。 因此,除非用户选择使用他们的 FB 帐户登录,否则您不必联系 Facebook。 此外,身份验证应该是基于会话的。会话信息可以包括用户、会话密钥、有效时间范围,也可能包括源 IP。一旦用户成功登录,就会生成一个会话。那么对于每个请求,您只需检查会话密钥是否仍然有效。

    【讨论】:

      猜你喜欢
      • 2017-03-17
      • 2021-08-02
      • 2019-02-07
      • 1970-01-01
      • 2017-01-07
      • 2015-11-12
      • 1970-01-01
      • 1970-01-01
      • 2015-12-26
      相关资源
      最近更新 更多