1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.jboss.as.quickstarts.servlet_security;
18
19 import java.io.IOException;
20 import java.io.PrintWriter;
21 import java.security.Principal;
22
23 import javax.servlet.ServletException;
24 import javax.servlet.annotation.HttpConstraint;
25 import javax.servlet.annotation.ServletSecurity;
26 import javax.servlet.annotation.WebServlet;
27 import javax.servlet.http.HttpServlet;
28 import javax.servlet.http.HttpServletRequest;
29 import javax.servlet.http.HttpServletResponse;
30
31
32
33
34
35
36
37
38 @SuppressWarnings("serial")
39 @WebServlet("/SecuredServlet")
40 @ServletSecurity(@HttpConstraint(rolesAllowed = { "quickstarts" }))
41 public class SecuredServlet extends HttpServlet {
42
43 private static String PAGE_HEADER = "<html><head><title>servlet-security</title></head><body>";
44
45 private static String PAGE_FOOTER = "</body></html>";
46
47 @Override
48 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
49 PrintWriter writer = resp.getWriter();
50 Principal principal = null;
51 String authType = null;
52 String remoteUser = null;
53
54
55 principal = req.getUserPrincipal();
56
57 remoteUser = req.getRemoteUser();
58
59 authType = req.getAuthType();
60
61 writer.println(PAGE_HEADER);
62 writer.println("<h1>" + "Successfully called Secured Servlet " + "</h1>");
63 writer.println("<p>" + "Principal : " + principal.getName() + "</p>");
64 writer.println("<p>" + "Remote User : " + remoteUser + "</p>");
65 writer.println("<p>" + "Authentication Type : " + authType + "</p>");
66 writer.println(PAGE_FOOTER);
67 writer.close();
68 }
69
70 }