2009-11-13 218 views
55

我正在寻找可帮助我构建OAuth提供程序的Java库。我必须能够接收OAuth签名的请求并确定它们是否有效(检查签名,时间戳和随机数值)。OAuth提供程序库(Java)

你知道有没有让这个任务更容易的东西?

+7

换句话说,你的正在寻找的是一个Java库,实现一个OAuth *商*,而不是*消费者*。您可能想编辑您的问题以纠正错误。 – Matthias 2010-03-21 10:59:29

+0

巴勃罗,请改变你的问题标题和你的问题,以反映意图。你想要一个OAuth提供者... – 2010-12-22 18:50:26

+2

当我注意到你是它的作者时,我只是* *要将你链接到Scribe(https://github.com/fernandezpablo85/scribe-java)!你真的最终自己写图书馆了吗? ;-) – 2010-12-23 08:26:06

回答

12

一个库看起来很有趣(我不包括OAuth for Spring SecurityOAuth Signpost这是不是你要找的内容):

一个Java libraryexamples分别是约翰克里斯蒂安,普拉文 Alavilli和Dirk Ba​​lfanz贡献的 。

OAuth for Spring Security也是 可用,由Ryan Heaton提供。 此项目不在 OAuth存储库中。

OAuth Signpost提供简单的OAuth 消息签署的Java和Apache HttpComponents(谷歌Android 准备好了!)。供稿人:Matthias Kaeppler。

我已经检查了Java library远一点,我认为这需要客户端和服务器端的代码,其提供的一切。下面blog post实际上有一个完整的例子,我在下面粘贴(一JSP)的服务器代码:

<%@ page import="net.oauth.server.*"%> 
<%@ page import="net.oauth.*"%> 

<% 
//Presumably this should actually be looked up for a given key. 
String consumerSecret="uynAeXiWTisflWX99KU1D2q5"; 

//Presumably the key is sent by the client. This is part of the URL, after all. 
String consumerKey="orkut.com:623061448914"; 

//Construct the message object. Use null for the URL and let the code construct it. 
OAuthMessage message=OAuthServlet.getMessage(request,null); 

//Construct an accessor and a consumer 
OAuthConsumer consumer=new OAuthConsumer(null, consumerKey, consumerSecret, null); 
OAuthAccessor accessor=new OAuthAccessor(consumer); 

//Now validate. Weirdly, validator has a void return type. It throws exceptions 
//if there are problems. 
SimpleOAuthValidator validator=new SimpleOAuthValidator(); 
validator.validateMessage(message,accessor); 

//Now what? Generate some JSON here for example. 
System.out.println("It must have worked"); %> 

这接近你想要什么。

+3

只是为了澄清:Pascal将这些库交叉出来,因为它们仅用于客户端OAuth。作者正在寻找什么,然而,这是一个服务器端OAuth库。 – Matthias 2010-03-21 11:00:57

+2

2013年的答案的任何更新?! :) – 2013-02-18 23:10:01

+0

看着博客文章,它似乎是省略了令牌密码(所以签名验证不应该工作)。此外,SimpleOAuthValidator实际上从未实际检查nonce值,这意味着此实现易受重放攻击的影响。它可能仍然是一个有用的起点,但是您需要从中建立起来,使其功能和安全。 – 2013-07-01 07:18:23

0

看起来像是在http://oauth.googlecode.com/svn/code/java/的图书馆有一个Subversion回购。看起来你必须结帐并运行maven才能获得可执行文件。

如果你进入example/webapp/src/main/java,他们有一些从Twitter,Yahoo,&其他的例子。

+2

我认为那些客户端只有库 – 2009-11-13 23:07:42

0

Jersey(JAX-RS的参考实现)通过名为OpenSSO Auth Filter的Jersey扩展支持OAuth。但是,这需要额外的OpenSSO服务器实例。见this document for more information

请注意,OpenSSO已被Oracle终止,现在是under ForgeRock as OpenAM

+0

嘿... hendy你有没有尝试过Scribe库? 我有点困惑如何处理该库...因为它没有javadoc文件在那里。 :( – gumuruh 2011-11-11 09:20:25

+0

抱歉,没有:) hehe – 2011-11-12 17:41:59

4

您可以使用Jersey OAuth Signature Library

对于servlet或过滤器的简单OAuth身份验证可以使用容器过滤器进行设置,容器过滤器在请求匹配并分派到根资源类之前过滤请求。容器过滤器是使用初始化参数,其指向一个用户定义的类,如下面的注册:

public class OAuthAuthenticationFilter implements ContainerRequestFilter { 
    @Override 
    public ContainerRequest filter(ContainerRequest containerRequest) { 
     // Read the OAuth parameters from the request 
     OAuthServerRequest request = new OAuthServerRequest(containerRequest); 
     OAuthParameters params = new OAuthParameters(); 
     params.readRequest(request); 

     // Set the secret(s), against which we will verify the request 
     OAuthSecrets secrets = new OAuthSecrets(); 
     // ... secret setting code ... 

     // Check that the timestamp has not expired 
     String timestampStr = params.getTimestamp(); 
     // ... timestamp checking code ... 

     // Verify the signature 
     try { 
      if(!OAuthSignature.verify(request, params, secrets)) { 
       throw new WebApplicationException(401); 
      } 
     } catch (OAuthSignatureException e) { 
      throw new WebApplicationException(e, 401); 
     } 

     // Return the request 
     return containerRequest; 
    } 
} 
+0

我们在哪里可以找到完整的工作示例(带单元测试)?谢谢。 – 2011-11-22 11:34:46

30

Scribe是一个OAuth库的Java,由提问者本人写入。 ;-)

注:我在这里发布这个答案,以便其他Google的选择可供选择。 对于另一个基于库的替代方案,请参阅我的其他答案“Jersey OAuth签名库”。

一些代码来说明用法:

OAuthService service = new ServiceBuilder() 
            .provider(TwitterApi.class) 
            .apiKey("your_api_key") 
            .apiSecret("your_api_secret") 
            .build(); 
... 
Token requestToken = service.getRequestToken(); 
String your_token = requestToken.getToken(); 
... 
Verifier verifier = new Verifier("your_previously_retrieved_verifier"); 
Token accessToken = service.getAccessToken(requestToken, verifier); 

创建要求:

OAuthRequest request = OAuthRequest(Verb.GET, "http://api.twitter.com/1/direct_messages.json"); 
service.signRequest(accessToken, request); 
Response response = request.send(); 
+0

哇!好的@Hendy – Dejell 2013-05-02 07:03:10

+0

谢谢你的信息。但请注意,此代码段不适用于Android 4及更多版本。看起来Android 4需要异步任务。 – trante 2013-07-25 18:11:07