「Spark Framework入門」Basic認証を実装サンプル
サンプルコード:
before((request, response) -> {
String requestAuth = “" + request.headers(“Authorization");
String strBasicAuth =
Base64.getEncoder().encodeToString(“basicUser:basicPwd".getBytes(StandardCharsets.UTF_8));
if (!requestAuth.replaceAll(“Basic\\s", “").equals(strBasicAuth)) {
System.out.println(“Basic Authorization is failed!");
response.header(“WWW-Authenticate", “Basic realm=\"Secret Zone\"\r\n");
halt(401, “You are not welcome here");
}
});
参考URL
http://sparkjava.com/documentation#halting