有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何使用SpringSecurity4.0.1使用Auth令牌实现Rest完整Web服务。释放

我正在尝试用RESTfulWebService设计一个API管理器。在Spring的新版本中,我们可以在Java代码中组合所有内容,而无需使用web.xmlsecurityconfig.xml。 根据Authtoken的概念,API管理器应该具有用于用户身份验证的Authtoken和刷新令牌。 请告诉我如何使用Spring Security实现RESTfull Web服务的示例源代码或指导

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

本教程介绍了正确的方法

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

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

我还需要把它们放到Java级别


共 (1) 个答案

  1. # 1 楼答案

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

    作为总结,您的解决方案如下所示:

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

      2.1。顺便说一下,Stormpath大大增强了这里的所有可能性。您可以通过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安全配置,这是可以在任何框架中运行的普通Java代码

    请查看here了解更多信息

    希望有帮助

    免责声明,我是一名积极的Stormpath贡献者