SparkJava:如何在我的before()过滤器中验证OAuth令牌?

问题描述 投票:0回答:1

我有一个用Spark-java(不是Apache Spark)编写的非常简单的Web服务器,并且希望从初始请求中收集Auth令牌,并将其发送到辅助URL,以针对我公司的auth数据库进行身份验证。我已经看过文档,并且不确定如何做到这一点。我还需要知道如何为身份验证服务器设置请求的查询参数。下面是我的代码:

/*
 * This Java source file was generated by the Gradle 'init' task.
 */
package c2c_server;

import static spark.Spark.*;

public class HelloWorld {
   public static void main(String[] args) {

      before((before_req, before_res) -> {
         System.out.println("I'm in before");
         String token = before_req.headers("Authorization");

         // No token, reject
         if (token == null) {
            halt(401, "Unauthorized");
         }
         else if (token != null) {
            System.out.println("Token is : " + token);
            String url = "https://my-auth-url";

         }
      });

      get("/command/:command", (req, res) -> {
         System.out.println("You sent the command: " + req.params(":command") + " and the headers: " + req.headers());
         if (req.params(":command").equals("run")) {
            res.status(200);
            return "You'll run the Java app to push data to S3!";
         }
         else {
            res.status(400);
            return "I don't understand that command";
         }
      });
      System.out.println("Server listening");
   }
}
java rest spark-java
1个回答
0
投票
通过进行一些研究,看来JavaSpark项目本身没有办法做到这一点。我需要利用Apache HttpClient库来提供以下代码来处理第二个验证令牌的请求:
© www.soinside.com 2019 - 2024. All rights reserved.