【发布时间】: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