Implement Basic authentication in Java

Insanely reinventing the wheel, Sometimes I have to make my own because the wheel standards don't match.

It may be possible to use it when creating a login screen.

It is an implementation example in the micro framework Spark Framework.

First, study about Basic authentication

Behavior of basic authentication-Qiita

Hmmmm ... First, the http server is in status 401 It seems that you should return the header such as WWW-Authenticate: Basic realm = \" Secret Zone \ "\ r \ n.

Then, if you enter the authentication information on the browser side ʻAuthorization: Basic <Base64 format of'ID: PASS'>` header comes from the client If it is decoded and matches, authentication is OK, and if it is NG, it seems that 401 should be returned again.

Implemented immediately

Check out the official documentation for snippets that might work.

Documentation - Spark Framework: An expressive web framework for Kotlin and Java

It seems good to perform authentication processing with a filter that runs before processing each request.

Example of implementation

before((request, response) -> {
  String reqAuth = "" + request.headers("Authorization");
  String basicAuth =
      Base64.getEncoder().encodeToString("user1234:password1234".getBytes(StandardCharsets.UTF_8));
  if (!reqAuth.replaceAll("Basic\\s", "").equals(basicAuth)) {
    System.out.println("Basic Authorization is failed!");
    response.header("WWW-Authenticate", "Basic realm=\"Secret Zone\"\r\n");
    halt(401, "You are not welcome here");
  }
});

The standard Base64 library cannot be used unless it is Java 8. Java7 people, use the one you like ↓ BaseEncoding (Guava: Google Core Libraries for Java 16.0 API) Base64 (Apache Commons Codec 1.11 API)

Recommended Posts

Implement Basic authentication in Java
Basic Authentication with Java 11 HttpClient
Implement two-step verification in Java
Implement LTI authentication in Rails
Implement math combinations in Java
2 Implement simple parsing in Java
Implement Email Sending in Java
Implement functional quicksort in Java
Implement rm -rf in Java.
[Java] Basic terms in programming
Implement XML signature in Java
Implement Table Driven Test in Java 14
3 Implement a simple interpreter in Java
Implement reCAPTCHA v3 in Java / Spring
Implement PHP implode function in Java
Try to implement Yubaba in Java
1 Implement simple lexical analysis in Java
How to implement date calculation in Java
How to implement Kalman filter in Java
Implement API Gateway Lambda Authorizer in Java Lambda
Partization in Java
Try to implement n-ary addition in Java
Java basic grammar
Java basic grammar
Concurrency Method in Java with basic example
Changes in Java 11
Rock-paper-scissors in Java
Java basic knowledge 1
[Java] Basic structure
[Java] [Basic] Glossary
How to implement coding conventions in Java
Java basic grammar
Java basic grammar
Implement something like a stack in Java
Pi in Java
Java exercises [Basic]
FizzBuzz in Java
Implemented basic search / sort algorithm in Java
I tried to make Basic authentication with Java
[java] sort in list
Read JSON in Java
Interpreter implementation in Java
Make Blackjack in Java
Rock-paper-scissors app in Java
Constraint programming in Java
Put java8 in centos7
Combine arrays in Java
"Hello World" in Java
Callable Interface in Java
Comments in Java source
java basic knowledge memo
Azure functions in java
Format XML in Java
Simple htmlspecialchars in Java
Java basic date manipulation
Boyer-Moore implementation in Java
Hello World in Java
Use OpenCV in Java
Java basic naming conventions
webApi memorandum in java
Type determination in Java