OAuth提供程序库(Java)

时间:2009-11-13 21:14:03

标签: java oauth scribe consumer

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

你知道是否有什么能让这项工作变得更容易吗?

6 个答案:

答案 0 :(得分:31)

Scribe 是一个针对Java的OAuth库,由提问者自己编写。 ; - )

注意:我在此发布此答案,以便其他googlers可以选择其他选择。 对于另一个基于库的替代方案,请参阅我的其他答案“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();

答案 1 :(得分:12)

http://oauth.net/code上提到的一个图书馆看起来很有意思(我将OAuth for Spring SecurityOAuth Signpost排除在你不想要的地方):

  

Java libraryexamples   由Praveen的John Kristian提供   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"); %> 

这看起来很接近你想要的。

答案 2 :(得分:4)

您可以使用 Jersey OAuth Signature Library

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

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;
    }
}

答案 3 :(得分:1)

Spring Security有OAuth plugin

答案 4 :(得分:0)

看起来http://oauth.googlecode.com/svn/code/java/的库有一个Subversion repo。看起来你必须签出并运行maven才能获得可执行文件。

如果你进入example / webapp / src / main / java,他们会有一些消费来自Twitter,Yahoo和&amp;的例子。其他

答案 5 :(得分:0)

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

请注意,Oracle已停止使用OpenSSO,现在是under ForgeRock as OpenAM