【问题标题】:OAuth - JAX-RS - Setting up secure webpageOAuth - JAX-RS - 设置安全网页
【发布时间】:2016-05-26 14:49:46
【问题描述】:

我有一个 JAX-RS 项目,我需要使用 OAuth 保护 1 个特定页面,如果可能的话,我希望将所有内容都放在 1 个类中。

我搜索的内容似乎没有合适的指南或教程。

这是我迄今为止尝试过的:

原课:

 @Path("/topsecret")
 @Produces(MediaType.TEXT_PLAIN)
 public class TopSecretRestService extends AbstractRestService {

  @GET
  @Path("/")
   public Response getSecret() {
       String output = "This is TOP secret: " + configuration.getValue(Configuration.Key.TOPSECRET);
       return Response.status(200).entity(output).build();

   }
}

Steeplesoft 的解决方案:(不断报错)

@Path("/topsecret")
@Produces(MediaType.TEXT_PLAIN)
public class TopSecretRestService extends AbstractRestService {

    @Path("/")
    @GET
    public Response authorize(@Context HttpServletRequest request)
            throws URISyntaxException, OAuthSystemException {
        try {
            OAuthAuthzRequest oauthRequest =
                new OAuthAuthzRequest(request);
            OAuthIssuerImpl oauthIssuerImpl =
                new OAuthIssuerImpl(new MD5Generator());

            //build response according to response_type
            String responseType =
                oauthRequest.getParam(OAuth.OAUTH_RESPONSE_TYPE);

            OAuthASResponse.OAuthAuthorizationResponseBuilder builder =
                    OAuthASResponse.authorizationResponse(request,
                        HttpServletResponse.SC_FOUND);

            // 1
            if (responseType.equals(ResponseType.CODE.toString())) {
                final String authorizationCode =
                    oauthIssuerImpl.authorizationCode();
                database.addAuthCode(authorizationCode);
                builder.setCode(authorizationCode);
            }

            String redirectURI =
                oauthRequest.getParam(OAuth.OAUTH_REDIRECT_URI);
            final OAuthResponse response = builder
                .location(redirectURI)
                .buildQueryMessage();
            URI url = new URI(response.getLocationUri());
            return Response.status(response.getResponseStatus())
                .location(url)
                .build();

            String output = "This is TOP secret: " + configuration.getValue(Configuration.Key.TOPSECRET);
            return Response.status(200).entity(output).build();


        } catch (OAuthProblemException e) {
            // ...
        }
}

}

谷歌的解决方案(看似最简单但找不到合适的罐子)

@GET
@Path("/")
public Response getSecret() {

    OAuthService oauth = OAuthServiceFactory.getOAuthService();
    String scope = "https://www.googleapis.com/auth/userinfo.email";
    Set<String> allowedClients = new HashSet<>();
    allowedClients.add("407408718192.apps.googleusercontent.com"); // list your client ids here

    try {
      User user = oauth.getCurrentUser(scope);
      String tokenAudience = oauth.getClientId(scope);
      if (!allowedClients.contains(tokenAudience)) {
        throw new OAuthRequestException("audience of token '" + tokenAudience
            + "' is not in allowed list " + allowedClients);
      }
      // proceed with authenticated user
        String output = "This is TOP secret: " + configuration.getValue(Configuration.Key.TOPSECRET);
        return Response.status(200).entity(output).build();
    } catch (OAuthRequestException ex) {
      // handle auth error
      // ...
    } catch (OAuthServiceFailureException ex) {
      // optionally, handle an oauth service failure
      // ...
    }

}

网站和其他问题调查​​:

Securing jax-rs with OAuth -- 提问者提供的答案,很短,没有细节

Jax RS REST API - OAuth 2.0 and Control Origin -- 提问者提供的答案,不是同一个问题

http://cxf.apache.org/docs/jax-rs-oauth2.html 使用 oauth2 的 jax-rs 教程

注意:我对 OAuth 和 jax-rs 都很陌生

【问题讨论】:

    标签: java web-services rest oauth jax-rs


    【解决方案1】:

    使用 JAX-RS 编写的最简单的工作示例是 java-oauth-server。它是一个授权服务器实现,不仅支持 OAuth 2.0(RFC 6749 等),还支持OpenID Connect

    如果您寻找的不是授权服务器实现而是资源服务器实现,请参阅java-resource-server

    授权服务器是颁发访问令牌的服务器。 资源服务器是引用访问令牌并返回请求数据的服务器。这两个服务器在逻辑上是不同的东西,但如果您愿意,它们可以在一台服务器上实现。我不知道您要实现哪个服务器。

    回答者是java-oauth-server和java-resource-server的作者。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-05-06
      • 1970-01-01
      • 2013-01-18
      • 1970-01-01
      • 2015-12-18
      • 2011-10-11
      • 1970-01-01
      • 2013-08-27
      相关资源
      最近更新 更多