2009-11-13 188 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