【问题标题】:How To Implement Rest Full Web Service with Auth Token using Spring Security 4.0.1.RELEASE如何使用 Spring Security 4.0.1.RELEASE 使用 Auth Token 实现 Rest Full Web Service
【发布时间】:2015-08-14 12:17:24
【问题描述】:

我正在尝试设计一个带有 RESTful 网络服务的 API 管理器。在 Spring 的新版本中,我们可以在 Java 代码中组合所有内容,而无需使用 web.xmlsecurityconfig.xml。 根据 Authtoken 的概念,API manager 应该有 authtoken 和 refresh token 用于用户认证。 请问,谁能给我示例源代码或指导如何使用 Spring Security 实现 RESTfull webservice。

  1. 我需要知道所有配置是如何在 Java 代码中实现的。
  2. 它也应该有 Authtoken 概念。

本教程说正确的方法。

http://www.beingjavaguys.com/2014/10/spring-security-oauth2-integration.html

但 Spring 配置在 Spring.xml 文件中。

我还需要将它们放入 Java 级别。

【问题讨论】:

  • Authtoken concept 是什么意思?你的意思是OAuth2
  • 我的意思是通常情况下,当我们连接到 API 时,它们会为客户端提供令牌并刷新令牌。当令牌时间到期时,我们可以通过提供刷新令牌来获取新令牌。我需要了解它是如何在 Spring Security 中实现的。 :)
  • 也许Spring Security OAuth2 samples 可以帮助你。他们有双方客户端和服务器的样本。
  • Thanx ksokol ,但他们有 Spring MVC 应用程序。在我的 API 管理器未查看部分。在我的服务中只返回 JSON 对象。在此示例中,安全部分由 login.jsp 页面完成。在我的应用程序中没有视图(没有 html 页面)部分 :(.

标签: java spring restful-authentication


【解决方案1】:

Stormpath 的人们有一个非常简单的解决方案来实现 Oauth。请查看Using Stormpath for API Authentication

总而言之,您的解决方案将如下所示:

  1. 您将使用Stormpath Java SDK 轻松委派您的所有用户管理需求。
  2. 当用户按下登录按钮时,您的前端将通过其 REST API 将凭据安全地发送到您的后端。

    2.1。顺便说一句,Stormpath 极大地增强了这里的所有可能性。除了拥有自己的登录页面之外,您还可以通过 IDSite 将登录/注册功能完全委托给 Stormpath,或者您也可以将其委托给 Servlet Plugin。 Stormpath 还支持 Google、Facebook、LinkedIn 和 Github 登录。

  3. 然后,您的后端将尝试针对 Stormpath 后端对用户进行身份验证,结果将返回 access token

    /** This code will throw an Exception if the authentication fails */
    public void postOAuthToken(HttpServletRequest request, HttpServletResponse response) {
        Application application = client.getResource(applicationRestUrl, Application.class);
    
        //Getting the authentication result
        AccessTokenResult result = (AccessTokenResult) application.authenticateApiRequest(request);
    
        //Here you can get all the user data stored in Stormpath
        Account account = accessTokenResult.getAccount();
    
        response.setStatus(HttpServletResponse.SC_OK);
        response.setContentType("application/json");
    
        //Return the Access Token
        response.getWriter().print(token.toJson());
        response.getWriter().flush();
    }
    
  4. 然后,对于每个经过身份验证的请求,您的后端都会这样做:

    /** This is your protected API */
    public void sayHello(HttpServletRequest request, HttpServletResponse response) {
        Application application = client.getResource(applicationRestUrl, Application.class);
    
        OauthAuthenticationResult result = (OauthAuthenticationResult) application.authenticateOauthRequest(request).execute();
    
        System.out.println(result.getApiKey());
        System.out.println(result.getAccount());
    
        //At this point the authorization was successful, you can now allow the actual operation to be executed
        doSayHello();
    }
    

所有这些都不需要任何特殊的 Spring Security 配置,这是可以在任何框架中运行的纯 Java 代码。

请查看here了解更多信息。

希望有帮助!

免责声明,我是 Stormpath 的活跃贡献者。

【讨论】:

    猜你喜欢
    • 2017-05-13
    • 2019-10-14
    • 2015-01-31
    • 2015-03-23
    • 2015-07-18
    • 2016-09-10
    • 2013-02-03
    • 2017-07-12
    • 2016-03-23
    相关资源
    最近更新 更多