Skip to content

Commit 8bdef70

Browse files
committed
FORM auth more consistent with BASIC test
Signed-off-by: arjantijms <[email protected]>
1 parent b70076f commit 8bdef70

File tree

5 files changed

+136
-72
lines changed

5 files changed

+136
-72
lines changed

servlet/security-form-based/src/main/webapp/index.jsp renamed to servlet/security-form-based/src/main/java/org/javaee7/servlet/security/form/based/ErrorServlet.java

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
<!--
21
/*
3-
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
2+
" * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
43
*
54
* Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved.
65
*
@@ -38,20 +37,47 @@
3837
* only if the new code is made subject to such option by the copyright
3938
* holder.
4039
*/
41-
-->
40+
package org.javaee7.servlet.security.form.based;
4241

43-
<%@page contentType="text/html" pageEncoding="UTF-8"%>
44-
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
45-
"http://www.w3.org/TR/html4/loose.dtd">
42+
import java.io.IOException;
4643

47-
<html>
48-
<head>
49-
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
50-
<title>Form-based Security - Success</title>
51-
</head>
52-
<body>
53-
<h1>Form-based Security - Success</h1>
44+
import javax.servlet.ServletException;
45+
import javax.servlet.annotation.WebServlet;
46+
import javax.servlet.http.HttpServlet;
47+
import javax.servlet.http.HttpServletRequest;
48+
import javax.servlet.http.HttpServletResponse;
49+
50+
/**
51+
* @author Arun Gupta
52+
* @author Arjan Tijms
53+
*/
54+
@WebServlet("/ErrorServlet")
55+
public class ErrorServlet extends HttpServlet {
56+
57+
private static final long serialVersionUID = 1L;
58+
59+
@Override
60+
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
61+
response.setContentType("text/html;charset=UTF-8");
5462

55-
If you reached this page that means form-based security credentials are correctly configured.
56-
</body>
57-
</html>
63+
response.getWriter().print(
64+
65+
"<html>" +
66+
"<head>" +
67+
"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">" +
68+
"<title>Form-Based Login Error Page</title>" +
69+
"</head>" +
70+
71+
"<body>" +
72+
"<h2>Invalid user name or password.</h2>" +
73+
"</body>" +
74+
"</html>"
75+
76+
);
77+
}
78+
79+
@Override
80+
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
81+
response.getWriter().print("my POST");
82+
}
83+
}

servlet/security-form-based/src/main/webapp/loginform.jsp renamed to servlet/security-form-based/src/main/java/org/javaee7/servlet/security/form/based/LoginServlet.java

Lines changed: 45 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
<!--
21
/*
3-
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
2+
" * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
43
*
54
* Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved.
65
*
@@ -38,26 +37,51 @@
3837
* only if the new code is made subject to such option by the copyright
3938
* holder.
4039
*/
41-
-->
40+
package org.javaee7.servlet.security.form.based;
4241

43-
<%@page contentType="text/html" pageEncoding="UTF-8"%>
44-
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
45-
"http://www.w3.org/TR/html4/loose.dtd">
42+
import java.io.IOException;
4643

47-
<html>
48-
<head>
49-
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
50-
<title>Form-Based Login Page</title>
51-
</head>
52-
<body>
53-
<h1>Form-Based Login Page</h1>
44+
import javax.servlet.ServletException;
45+
import javax.servlet.annotation.WebServlet;
46+
import javax.servlet.http.HttpServlet;
47+
import javax.servlet.http.HttpServletRequest;
48+
import javax.servlet.http.HttpServletResponse;
5449

55-
<form method="POST" action="j_security_check">
56-
Username: <input type="text" name="j_username"> <p/>
57-
Password: <input type="password" name="j_password" autocomplete="off"> <p/>
58-
<input type="submit" value="Submit" name="submitButton">
59-
<input type="reset" value="Reset">
60-
</form>
50+
/**
51+
* @author Arun Gupta
52+
* @author Arjan Tijms
53+
*/
54+
@WebServlet("/LoginServlet")
55+
public class LoginServlet extends HttpServlet {
56+
57+
private static final long serialVersionUID = 1L;
58+
59+
@Override
60+
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
61+
response.setContentType("text/html;charset=UTF-8");
62+
63+
response.getWriter().print(
64+
65+
"<html>" +
66+
"<head>" +
67+
"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">" +
68+
"<title>Form-Based Login Page</title>" +
69+
"</head>" +
70+
71+
"<body>" +
72+
"<form method=\"POST\" action=\"j_security_check\">" +
73+
"Username: <input type=\"text\" name=\"j_username\"> <p/>" +
74+
"Password: <input type=\"password\" name=\"j_password\"> <p/>" +
75+
"<input type=\"submit\" value=\"Submit\" name=\"submitButton\">" +
76+
"</form>" +
77+
"</body>" +
78+
"</html>"
79+
80+
);
81+
}
6182

62-
</body>
63-
</html>
83+
@Override
84+
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
85+
response.getWriter().print("my POST");
86+
}
87+
}

servlet/security-form-based/src/main/webapp/loginerror.jsp renamed to servlet/security-form-based/src/main/java/org/javaee7/servlet/security/form/based/SecureServlet.java

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
<!--
21
/*
32
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
43
*
@@ -38,29 +37,47 @@
3837
* only if the new code is made subject to such option by the copyright
3938
* holder.
4039
*/
41-
-->
40+
package org.javaee7.servlet.security.form.based;
4241

43-
<%@page contentType="text/html" pageEncoding="UTF-8"%>
44-
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
45-
"http://www.w3.org/TR/html4/loose.dtd">
42+
import java.io.IOException;
4643

47-
<html>
48-
<head>
49-
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
50-
<title>Form-Based Login Error Page</title>
51-
</head>
52-
<body>
53-
<h1>Form-Based Login Error Page</h1>
44+
import javax.servlet.ServletException;
45+
import javax.servlet.annotation.WebServlet;
46+
import javax.servlet.http.HttpServlet;
47+
import javax.servlet.http.HttpServletRequest;
48+
import javax.servlet.http.HttpServletResponse;
5449

55-
<h2>Invalid user name or password.</h2>
50+
/**
51+
* @author Arun Gupta
52+
* @author Arjan Tijms
53+
*/
54+
@WebServlet("/SecureServlet")
55+
public class SecureServlet extends HttpServlet {
5656

57-
<p>Please enter a user name or password that is authorized to access this
58-
application. For this application, make sure to create a user: <p><p>
59-
60-
For WildFly: Invoke "./bin/add-user.sh -a -u u1 -p p1 -g g1"<br>
61-
For GlassFish: Invoke "./bin/asadmin create-file-user --groups g1 u1" and use the password "p1" when prompted.<br><br>
57+
private static final long serialVersionUID = 1L;
6258

63-
Click here to <a href="index.jsp">Try Again</a></p>
59+
@Override
60+
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
61+
response.setContentType("text/html;charset=UTF-8");
62+
63+
response.getWriter().print(
64+
65+
"<html>" +
66+
"<head>" +
67+
"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">" +
68+
"<title>Form-based Security - Success</title>" +
69+
"</head>" +
70+
71+
"<body>" +
72+
"<h2>Form-based Security - Success</h2>" +
73+
"</body>" +
74+
"</html>"
75+
76+
);
77+
}
6478

65-
</body>
66-
</html>
79+
@Override
80+
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
81+
response.getWriter().print("my POST");
82+
}
83+
}

servlet/security-form-based/src/main/webapp/WEB-INF/web.xml

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,10 @@
4545
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
4646
version="3.1">
4747

48-
<servlet>
49-
<display-name>index</display-name>
50-
<servlet-name>index</servlet-name>
51-
<jsp-file>/index.jsp</jsp-file>
52-
</servlet>
53-
5448
<security-constraint>
5549
<web-resource-collection>
5650
<web-resource-name>SecurityConstraint</web-resource-name>
57-
<url-pattern>/*</url-pattern>
51+
<url-pattern>/SecureServlet</url-pattern>
5852
</web-resource-collection>
5953
<auth-constraint>
6054
<role-name>g1</role-name>
@@ -66,10 +60,9 @@
6660

6761
<login-config>
6862
<auth-method>FORM</auth-method>
69-
<realm-name>file</realm-name>
7063
<form-login-config>
71-
<form-login-page>/loginform.jsp</form-login-page>
72-
<form-error-page>/loginerror.jsp</form-error-page>
64+
<form-login-page>/LoginServlet</form-login-page>
65+
<form-error-page>/ErrorServlet</form-error-page>
7366
</form-login-config>
7467
</login-config>
7568

servlet/security-form-based/src/test/java/org/javaee7/servlet/security/form/based/FormTest.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,21 @@ public static WebArchive createDeployment() {
4141
addUsersToContainerIdentityStore();
4242

4343
return create(WebArchive.class)
44-
.addAsWebResource(new File(WEBAPP_SRC, "index.jsp"))
45-
.addAsWebResource(new File(WEBAPP_SRC, "loginerror.jsp"))
46-
.addAsWebResource(new File(WEBAPP_SRC, "loginform.jsp"))
44+
.addClasses(
45+
SecureServlet.class,
46+
LoginServlet.class,
47+
ErrorServlet.class)
48+
4749
.addAsWebInfResource(new File(WEBAPP_SRC + "/WEB-INF", "web.xml"))
4850
.addAsWebInfResource(new File(WEBAPP_SRC + "/WEB-INF", "glassfish-web.xml"));
4951
}
5052

5153
@Before
5254
public void setup() throws IOException {
55+
@SuppressWarnings("resource")
5356
WebClient webClient = new WebClient();
54-
HtmlPage page = webClient.getPage(base + "/index.jsp");
57+
HtmlPage page = webClient.getPage(base + "SecureServlet");
58+
5559
loginForm = page.getForms().get(0);
5660
}
5761

0 commit comments

Comments
 (0)