Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 391cc5c

Browse files
Cleaned up programmatic login example
Signed-off-by: arjantijms <arjan.tijms@gmail.com>
1 parent 2073b81 commit 391cc5c

File tree

3 files changed

+22
-39
lines changed

3 files changed

+22
-39
lines changed

‎servlet/security-programmatic/src/main/java/org/javaee7/servlet/programmatic/login/LoginServlet.java

Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
/**
1313
* @author Arun Gupta
1414
*/
15-
@WebServlet(urlPatterns = { "/LoginServlet" })
15+
@WebServlet("/LoginServlet")
1616
public class LoginServlet extends HttpServlet {
1717

1818
private static final long serialVersionUID = 1L;
@@ -25,9 +25,10 @@ public class LoginServlet extends HttpServlet {
2525
* @throws ServletException if a servlet-specific error occurs
2626
* @throws IOException if an I/O error occurs
2727
*/
28-
protectedvoidprocessRequest(HttpServletRequestrequest, HttpServletResponseresponse) throwsServletException, IOException {
29-
28+
@Override
29+
protectedvoiddoGet(HttpServletRequestrequest, HttpServletResponseresponse) throwsServletException, IOException {
3030
response.setContentType("text/html;charset=UTF-8");
31+
3132
String user = request.getParameter("user");
3233
String password = request.getParameter("password");
3334

@@ -43,34 +44,8 @@ protected void processRequest(HttpServletRequest request, HttpServletResponse re
4344
private void userDetails(PrintWriter out, HttpServletRequest request) {
4445
out.println("isUserInRole?" + request.isUserInRole("g1"));
4546
out.println("getRemoteUser?" + request.getRemoteUser());
46-
out.println("getUserPrincipal?" + request.getUserPrincipal());
47+
out.println("getUserPrincipal?" + (request.getUserPrincipal() != null? request.getUserPrincipal().getName() : null));
4748
out.println("getAuthType?" + request.getAuthType());
4849
}
4950

50-
/**
51-
* Handles the HTTP <code>GET</code> method.
52-
*
53-
* @param request servlet request
54-
* @param response servlet response
55-
* @throws ServletException if a servlet-specific error occurs
56-
* @throws IOException if an I/O error occurs
57-
*/
58-
@Override
59-
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
60-
processRequest(request, response);
61-
}
62-
63-
/**
64-
* Handles the HTTP <code>POST</code> method.
65-
*
66-
* @param request servlet request
67-
* @param response servlet response
68-
* @throws ServletException if a servlet-specific error occurs
69-
* @throws IOException if an I/O error occurs
70-
*/
71-
@Override
72-
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
73-
processRequest(request, response);
74-
}
75-
7651
}

‎servlet/security-programmatic/src/main/webapp/WEB-INF/web.xml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,6 @@
33
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
44
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
55
version="3.1">
6-
<servlet>
7-
<display-name>index</display-name>
8-
<servlet-name>index</servlet-name>
9-
<jsp-file>/index.jsp</jsp-file>
10-
</servlet>
11-
126
<security-role>
137
<role-name>g1</role-name>
148
</security-role>

‎servlet/security-programmatic/src/test/java/org/javaee7/servlet/programmatic/login/LoginServletTest.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
import org.jboss.arquillian.junit.Arquillian;
1313
import org.jboss.arquillian.test.api.ArquillianResource;
1414
import org.jboss.shrinkwrap.api.spec.WebArchive;
15+
import org.junit.After;
16+
import org.junit.Before;
1517
import org.junit.Test;
1618
import org.junit.runner.RunWith;
1719
import org.xml.sax.SAXException;
@@ -29,6 +31,8 @@ public class LoginServletTest {
2931

3032
@ArquillianResource
3133
private URL base;
34+
35+
WebClient webClient;
3236

3337
@Deployment(testable = false)
3438
public static WebArchive createDeployment() {
@@ -39,12 +43,23 @@ public static WebArchive createDeployment() {
3943
addClass(LoginServlet.class).
4044
addAsWebInfResource((new File(WEBAPP_SRC + "/WEB-INF", "web.xml")));
4145
}
46+
47+
@Before
48+
public void setup() {
49+
webClient = new WebClient();
50+
}
51+
52+
@After
53+
public void tearDown() {
54+
webClient.getCookieManager().clearCookies();
55+
webClient.close();
56+
}
4257

4358
@Test
4459
public void testUnauthenticatedRequest() throws IOException, SAXException {
45-
WebClient webClient = new WebClient();
4660
HtmlPage page = webClient.getPage(base + "/LoginServlet");
4761
String responseText = page.asText();
62+
4863
System.out.println("testUnauthenticatedRequest:\n" + responseText + "\n");
4964

5065
assertTrue(responseText.contains("isUserInRole?false"));
@@ -55,14 +70,13 @@ public void testUnauthenticatedRequest() throws IOException, SAXException {
5570

5671
@Test
5772
public void testAuthenticatedRequest() throws IOException, SAXException {
58-
WebClient webClient = new WebClient();
5973
HtmlPage page = webClient.getPage(base + "/LoginServlet?user=u1&password=p1");
6074
String responseText = page.asText();
75+
6176
System.out.println("testAuthenticatedRequest:\n" + responseText + "\n");
6277

6378
assertTrue(responseText.contains("isUserInRole?true"));
6479
assertTrue(responseText.contains("getRemoteUser?u1"));
6580
assertTrue(responseText.contains("getUserPrincipal?u1"));
66-
assertTrue(responseText.contains("getAuthType?BASIC"));
6781
}
6882
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /