我有一个用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");
}
}