I tried to make Basic authentication with Java

Reference site

let's programming https://www.javadrive.jp/servlet/auth/

environment

eclipse2019-9 tomcat9

Motivation for writing

This is a memo to remember what you have learned. It worked almost according to the above reference site, but I changed it a little according to my environment, so I will write down the changed part.

What is Basic authentication?

A simple authentication method. When you open the website, a pop-up will appear asking for your login ID and password. Since the ID and password are sent unencrypted, it seems to be quite weak in terms of security. Once you log in, you will remain logged in until you close your browser.

File to prepare

Creating a servlet

First, let's make a servlet.

AuthTest1.java


package auth;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

//Changes with the reference site. Annotated.
@WebServlet("/AuthTest1")
public class AuthTest1 extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException{

        response.setContentType("text/html; charset=Shift_JIS");
        PrintWriter out = response.getWriter();

        out.println("<html>");
        out.println("<head>");
        out.println("<title>User authentication test</title>");
        out.println("</head>");
        out.println("<body>");

        out.println("<p>Test page 1</p>");

        //Changes with the reference site. Change the URL.
        out.println("<p><a href=\"/logintest/AuthTest2\">Go to test page 2</a></p>");

        out.println("</body>");
        out.println("</html>");
    }
}

AuthTest2.java


package auth;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

//Changes with the reference site. Annotated.
@WebServlet("/AuthTest2")
public class AuthTest2 extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException{

        response.setContentType("text/html; charset=Shift_JIS");
        PrintWriter out = response.getWriter();

        out.println("<html>");
        out.println("<head>");
        out.println("<title>User authentication test</title>");
        out.println("</head>");
        out.println("<body>");

        out.println("<p>Test page 2</p>");

        //Changes with the reference site. Change the URL.
        out.println("<p><a href=\"/logintest/AuthTest1\">Go to test page 1</a></p>");

        out.println("</body>");
        out.println("</html>");
    }
}

The location of the file is in the auth package. キャプチャ.PNG

Web.xml changes

Modify web.xml in the WEB-INF / lib directory. キャプチャ.PNG

web.xml


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
  <display-name>logintest</display-name>

<!--Changes with the reference site. Because I used annotations<servlet>When<servlet-mapping>Erase.-->
<security-constraint>
    <web-resource-collection>
      <web-resource-name>User Basic Auth</web-resource-name>
      <url-pattern>/*</url-pattern>
    </web-resource-collection>
    <auth-constraint>
      <role-name>sales</role-name>
    </auth-constraint>
  </security-constraint>

  <login-config>
    <auth-method>BASIC</auth-method>
    <realm-name>User Basic Auth</realm-name>
  </login-config>

  <security-role>
    <role-name>sales</role-name>
  </security-role>

</web-app>

Create auth.xml

For docbase = "", specify the directory where AuthTest1.java is located. The location of the file is the directory where tomcat is installed. \ Tomcat 9 \ conf \ Catalina \ localhost \

auth.xml


<Context path="/auth"
docBase="<?xml version="1.0"?>
<Context docBase="C:\Users\hoge\pleiades-2019-09-java-win-64bit-jre_20191007\pleiades\workspace\logintest\src\auth" path="/auth"> </Context>">
</Context>

Changes to tomcat-users.xml

Modify tomcat-users.xml in Severs of your Eclipse project. キャプチャ.PNG

tomcat-users.xml


<!--Changes with the reference site. All the roles that were set by default have been deleted. Because I got a mysterious error.-->
<tomcat-users>
  <role rolename="sales"/>
  <role rolename="staff"/>
  <user username="yamada" password="yamada" roles="sales"/>
  <user username="katou" password="katou" roles="staff"/>
</tomcat-users>

Try out

When the following screen appears Username: yamada Password: yamada If you can log in by entering, you will succeed! You will remain logged in until you close your browser. auth.PNG

The following cannot log in because the role is not set. Username: katou Password: katou

that's all. Thank you for your hard work.

Recommended Posts

I tried to make Basic authentication with Java
I tried to interact with Java
I tried to make an Android application with MVC now (Java)
I tried to break a block with java (1)
Basic Authentication with Java 11 HttpClient
I tried to implement TCP / IP + BIO with JAVA
[Java 11] I tried to execute Java without compiling with javac
I tried to make a login function in Java
I tried to implement Stalin sort with Java Collector
I used to make nc (netcat) with JAVA normally
I tried UDP communication with Java
I tried to summarize Java learning (1)
I tried to summarize Java 8 now
I tried to make an introduction to PHP + MySQL with Docker
I tried to create a java8 development environment with Chocolatey
I tried to modernize a Java EE application with OpenShift.
I want to make a list with kotlin and java!
I want to make a function with kotlin and java!
I tried to make Java Optional and guard clause coexist
I tried to make a client of RESAS-API in Java
I tried to summarize Java lambda expressions
I tried to get started with WebAssembly
I tried using OpenCV with Java + Tomcat
I tried to implement ModanShogi with Kinx
[Java] I tried to make a maze by the digging method ♪
I tried to make a group function (bulletin board) with Rails
Easy to make LINE BOT with Java Servlet Part 2: I tried image messages and templates
I tried to verify AdoptOpenJDK 11 (11.0.2) with Docker image
I tried to manage struts configuration with Coggle
I tried to manage login information with JMX
java I tried to break a simple block
I tried to make an automatic backup with pleasanter + PostgreSQL + SSL + docker
I did Java to make (a == 1 && a == 2 && a == 3) always true
[iOS] I tried to make a processing application like Instagram with Swift
I tried to chew C # (basic of encapsulation)
I tried to implement deep learning in Java
I want to use java8 forEach with index
I tried to make a Web API that connects to DB with Quarkus
I wanted to make (a == 1 && a == 2 && a == 3) true in Java
I tried to make a machine learning application with Dash (+ Docker) part2 ~ Basic way of writing Dash ~
I tried to output multiplication table in Java
How to write test code with Basic authentication
I tried to make a talk application in Java using AI "A3RT"
I tried to create Alexa skill in Java
Easy to make LINE BOT with Java Servlet
I tried to make a machine learning application with Dash (+ Docker) part3 ~ Practice ~
I tried to make a simple game with Javafx ① "Let's find happiness game" (unfinished)
[Java] I tried to connect using a connection pool with Servlet (tomcat) & MySQL & Java
[Android] I tried to make a material list screen with ListView + Bottom Sheet
[Small story] I tried to make the java ArrayList a little more convenient
I tried what I wanted to try with Stream softly.
I tried to implement file upload with Spring MVC
I tried to read and output CSV with Outsystems
I tried to implement Firebase push notification in Java
[Java] I tried to solve Paiza's B rank problem
I tried to operate SQS using AWS Java SDK
# 2 [Note] I tried to calculate multiplication tables in Java.
I started MySQL 5.7 with docker-compose and tried to connect
I tried to get started with Spring Data JPA
I tried to create a Clova skill in Java
I tried to make FizzBuzz that is uselessly flexible