Debian Codegraph @

jetty-ee10/jetty-ee10-demos/jetty-ee10-demo-spec/jetty-ee10-demo-spec-webapp/src/main/java/org/example/test/SecuredServlet.java

raw
1 //
2 // ========================================================================
3 // Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
4 //
5 // This program and the accompanying materials are made available under the
6 // terms of the Eclipse Public License v. 2.0 which is available at
7 // https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
8 // which is available at https://www.apache.org/licenses/LICENSE-2.0.
9 //
10 // SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
11 // ========================================================================
12 //
13
14 package org.example.test;
15
16 import java.io.IOException;
17 import java.io.PrintWriter;
18
19 import jakarta.servlet.annotation.HttpConstraint;
20 import jakarta.servlet.annotation.ServletSecurity;
21 import jakarta.servlet.annotation.WebServlet;
22 import jakarta.servlet.http.HttpServlet;
23 import jakarta.servlet.http.HttpServletRequest;
24 import jakarta.servlet.http.HttpServletResponse;
25
26 @WebServlet(urlPatterns = "/sec/*")
27 @ServletSecurity(@HttpConstraint(rolesAllowed = "admin"))
28 public class SecuredServlet extends HttpServlet
29 {
30 @Override
31 protected void doPost(HttpServletRequest req, HttpServletResponse resp)
32 throws IOException
33 {
34 PrintWriter writer = resp.getWriter();
35 writer.println("<html>");
36 writer.println("<head><link rel=\"stylesheet\" type=\"text/css\" href=\"../stylesheet.css\"/></head>");
37 writer.println("<body>");
38 writer.println("<h1>@ServletSecurity</h1>");
39 writer.println("<pre>");
40 writer.println("@ServletSecurity");
41 writer.println("public class SecuredServlet");
42 writer.println("</pre>");
43 writer.println("<p><b>Result: <span class=\"pass\">PASS</span></b></p>");
44 String context = getServletConfig().getServletContext().getContextPath();
45 if (!context.endsWith("/"))
46 context += "/";
47 writer.println("<p><a href=\"" + context + "logout.jsp\">Logout</A></p>");
48 writer.println("</body>");
49 writer.println("</html>");
50 writer.flush();
51 writer.close();
52 }
53 }