let's programming https://www.javadrive.jp/servlet/auth/
eclipse2019-9 tomcat9
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.
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.
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.
Modify web.xml in the WEB-INF / lib directory.
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>
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>
Modify tomcat-users.xml in Severs of your Eclipse project.
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>
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.
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