java - 在同一个应用程序中实现 Stormpath 表单登录/身份验证以及 REST oAuth 身份验证时遇到问题

标签 java rest oauth stormpath

我们正在将 stormpath 与 Java 结合使用,并且还尝试在同一应用程序上将表单登录与 REST API 身份验证结合起来。

我已经按照此处所述设置了 stormpath servlet 插件 https://docs.stormpath.com/java/servlet-plugin/quickstart.html ... 这很好用。

现在,在同一个应用程序上,我们有 API,我在其中使用 stormpath 实现了 oAuth 身份验证,请参见此处 http://docs.stormpath.com/guides/api-key-management/

访问 token 的第一个请求通过发送 Basic Base64(keyId:keySecret) 工作正常在请求头和 grant_type = client_credentials在 body 里。访问 token 被很好地返回。但是,尝试使用 header Bearer <the-obtained-access-token> 对后续请求进行身份验证之前甚至没有点击应用程序 返回以下 json 错误消息...

{
    "error": "invalid_client",
    "error_description": "access_token is invalid."
}

这很令人困惑,因为我已经在整个应用程序中设置了断点,并且我非常确定在 stormpath 启动并返回此错误之前,API 请求不会到达应用程序中的任何地方。即使 stormpath 在到达 REST 接口(interface)之前以某种方式拦截了请求,这条消息对我来说也没有任何意义,因为我肯定会使用从第一次调用获取访问权限时获得的有效访问 token 进行后续 API 调用- token 。

我已经想不出为什么会发生这种情况,但我怀疑它可能与 stormpath 配置有关,尤其是与组合有关 Web View 的登录/身份验证形式和 REST 端点的 oAuth Athentication 形式。话虽如此,这就是我的 stormpath.properties 的样子。希望这可以帮助指出我可能做错的任何事情。

stormpath.application.href=https://api.stormpath.com/v1/applications/[app-id]
stormpath.web.filters.authr=com.app.security.AuthorizationFilter
stormpath.web.request.event.listener = com.app.security.AuthenticationListener

stormpath.web.uris./resources/**=anon
stormpath.web.uris./assets/**=anon
stormpath.web.uris./v1.0/**=anon
stormpath.web.uris./** = authc,authr
stormpath.web.uris./**/**=authc,authr

我们将不胜感激。

最佳答案

问题可能与错误的请求有关。

您可以在您的应用中试用这段代码吗?:

 private boolean verify(String accessToken) throws OauthAuthenticationException {
     HttpRequest request = createRequestForOauth2AuthenticatedOperation(accessToken);
     AccessTokenResult result = Applications.oauthRequestAuthenticator(application)
        .authenticate(request);
     System.out.println(result.getAccount().getEmail() + " was successfully verified, you can allow your protect operation to continue");
     return true;
 }

 private HttpRequest createRequestForOauth2AuthenticatedOperation(String token) {
     try {
         Map<String, String[]> headers = new LinkedHashMap<String, String[]>();
         headers.put("Accept", new String[]{"application/json"});
         headers.put("Authorization", new String[]{"Bearer " + token});
         HttpRequest request = HttpRequests.method(HttpMethod.GET)
             .headers(headers)
             .build();
         return request;
     } catch (Exception e) {
         e.printStackTrace();
         return null;
     }
 }

关于java - 在同一个应用程序中实现 Stormpath 表单登录/身份验证以及 REST oAuth 身份验证时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33605873/

相关文章:

java - 将类转换为字节数组时出错,反之亦然?

Java REST IE @DELETE 405 方法不允许

node.js - 如何将我的 REST API 与我的 Express 网络应用分开?

javascript - 在 JS Web 应用程序中,如何确保拥有访问 token 的恶意用户无法刷新它?

java - 正则表达式从 founded 关键字替换为 next &

java.sql.SQLException : Missing IN or OUT parameter at index, 即使我提供了正确数量的参数

java - IzPack 安装程序,找不到元素“安装”的声明

perl - 为什么 curl POST 请求会使我的 Catalyst::Controller::REST Controller 崩溃?

oauth - Paypal Oauth 返回服务器错误获取示例 PizzaApp 中的 token

oauth - Google OAuth 检查用户是否从 Google 注销