package sample.basicauth;

import java.io.IOException;
import java.util.Base64;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.annotation.Priority;
import javax.ws.rs.Priorities;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.ext.Provider;

/**
 * ContainerRequestFilterを使ってBasic認証
 *
 */
@Provider
@Priority(Priorities.AUTHENTICATION)
public class BasicAuthenticationProvider implements ContainerRequestFilter {

    private final Pattern credentialsPattern = Pattern.compile("^Basic +(.+)$");

    //認証情報。本来はRDBなどから取得する。
    private String username = "backpaper0";
    private String password = "secret";

    @Override
    public void filter(ContainerRequestContext requestContext)
            throws IOException {

        String authorization = requestContext
                .getHeaderString(HttpHeaders.AUTHORIZATION);
        if (authorization != null) {
            Matcher m = credentialsPattern.matcher(authorization);
            if (m.matches()) {
                String credentials = new String(Base64.getDecoder().decode(
                        m.group(1)));
                if (credentials.equals(username + ":" + password)) {
                    return;
                }
            }
        }

        Response response = Response.status(Status.UNAUTHORIZED)
                .header(HttpHeaders.WWW_AUTHENTICATE, "Basic Realm=\"(-_-)\"")
                .type("text/plain; charset=UTF-8")
                .entity("認証しないとダメですよ！(-_-)ｼﾞﾄｯ").build();
        requestContext.abortWith(response);
    }
}

@@ -48,16 +48,58 @@
 eption;%0A
+import java.nio.charset.StandardCharsets;%0A
 import j
@@ -1116,24 +1116,168 @@
  != null) %7B%0A
+%0A            //%E6%AC%A1%E3%81%AE%E3%82%88%E3%81%86%E3%81%AA%E3%83%AA%E3%82%AF%E3%82%A8%E3%82%B9%E3%83%88%E3%83%98%E3%83%83%E3%83%80%E3%81%8C%E9%80%81%E3%82%89%E3%82%8C%E3%81%A6%E3%81%8F%E3%82%8B%E3%81%8B%E3%82%89%0A            //Base64%E3%81%A7%E3%83%87%E3%82%B3%E3%83%BC%E3%83%89%E3%81%97%E3%81%A6 %E3%83%A6%E3%83%BC%E3%82%B6%E3%83%BC%E5%90%8D:%E3%83%91%E3%82%B9%E3%83%AF%E3%83%BC%E3%83%89 %E3%82%92%E5%BE%97%E3%82%8B%E3%80%82%0A            //Authorization: Basic YmFja3BhcGVyMDpzZWNyZXQ=%0A%0A
             
@@ -1358,24 +1358,25 @@
 atches()) %7B%0A
+%0A
             
@@ -1401,88 +1401,231 @@
 ials
- = new String(Base64.getDecoder().decode(%0A                        m.group(1))
+EncodedBase64 = m.group(1);%0A                byte%5B%5D decoded = Base64.getDecoder().decode(%0A                        credentialsEncodedBase64);%0A                String credentials = new String(decoded, StandardCharsets.UTF_8
 );%0A
+%0A
     
@@ -1689,16 +1689,44 @@
 ord)) %7B%0A
+                    //%E8%AA%8D%E8%A8%BC%E6%88%90%E5%8A%9F%EF%BC%81%0A
         
@@ -1788,16 +1788,39 @@
      %7D%0A%0A
+        //%E8%AA%8D%E8%A8%BC%E5%A4%B1%E6%95%97(%C2%B4;%CF%89;%60)%0A%0A
         

コメント足したのとcredentialsはUTF-8でエンコードすることにした