IT TIP

OAuth 공급자 용 라이브러리 (Java)

itqueen 2021. 1. 8. 22:41
반응형

OAuth 공급자 용 라이브러리 (Java)


OAuth 공급자를 구축하는 데 도움이되는 Java 라이브러리를 찾고 있습니다. OAuth 서명 된 요청을 수신하고 유효한지 여부를 확인할 수 있어야합니다 (서명, 타임 스탬프 및 임시 값 확인).

이 작업을 더 쉽게 만드는 무언가가 있는지 알고 있습니까?


Scribe 는 질문자가 직접 작성한 Java 용 OAuth 라이브러리입니다. ;-)

참고 : 다른 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();

http://oauth.net/code에 언급 된 라이브러리 중 하나 가 흥미로워 보입니다 (원하는 것이 아닌 Spring SecurityOAuth Signpost위한 OAuth는 제외합니다 ).

자바 라이브러리예제는 요한 크리스티안, 프라 빈 Alavilli와 더크 Balfanz으로 기여했다.

Ryan Heaton이 제공 한 Spring Security 용 OAuth 도 사용할 수 있습니다. 이 프로젝트는 OAuth 저장소에서 호스팅되지 않습니다.

OAuth Signpost 는 Java 및 Apache HttpComponents (Google Android 지원!)에 대한 간단한 OAuth 메시지 서명을 제공합니다. Matthias Kaeppler 제공.

Java 라이브러리 를 조금 더 확인했으며 클라이언트 측 및 서버 측 코드에 필요한 모든 것을 제공한다고 생각합니다. 다음 블로그 게시물 에는 실제로 전체 예제가 있으며 아래에 서버 코드 (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"); %> 

이것은 당신이 원하는 것에 가깝게 보입니다.


Jersey OAuth 서명 라이브러리를 사용할 수 있습니다 .

서블릿 또는 필터에 대한 단순 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;
    }
}

Spring Security를위한 OAuth 플러그인 이 있습니다.


Looks like there's a Subversion repo for a library at http://oauth.googlecode.com/svn/code/java/. Looks like you'll have to checkout and run maven to get executables though.

If you go into example/webapp/src/main/java they have some examples of consuming from Twitter, Yahoo, & others.


Jersey (the reference implementation of JAX-RS) supports OAuth through a Jersey extension called OpenSSO Auth Filter. However this requires an additional OpenSSO server instance. See this document for more information.

Note that OpenSSO has been discontinued by Oracle and is now under ForgeRock as OpenAM.

ReferenceURL : https://stackoverflow.com/questions/1731966/library-for-oauth-provider-java

반응형