| 1 |
// |
| 2 |
// ======================================================================== |
| 3 |
// Copyright (c) 1995-2022 Mort Bay Consulting Pty Ltd and others. |
| 4 |
// ------------------------------------------------------------------------ |
| 5 |
// All rights reserved. This program and the accompanying materials |
| 6 |
// are made available under the terms of the Eclipse Public License v1.0 |
| 7 |
// and Apache License v2.0 which accompanies this distribution. |
| 8 |
// |
| 9 |
// The Eclipse Public License is available at |
| 10 |
// http://www.eclipse.org/legal/epl-v10.html |
| 11 |
// |
| 12 |
// The Apache License v2.0 is available at |
| 13 |
// http://www.opensource.org/licenses/apache2.0.php |
| 14 |
// |
| 15 |
// You may elect to redistribute this code under either of these licenses. |
| 16 |
// ======================================================================== |
| 17 |
// |
| 18 |
|
| 19 |
package com.acme.test; |
| 20 |
|
| 21 |
import java.io.IOException; |
| 22 |
import java.io.PrintWriter; |
| 23 |
import javax.servlet.annotation.HttpConstraint; |
| 24 |
import javax.servlet.annotation.ServletSecurity; |
| 25 |
import javax.servlet.annotation.WebServlet; |
| 26 |
import javax.servlet.http.HttpServlet; |
| 27 |
import javax.servlet.http.HttpServletRequest; |
| 28 |
import javax.servlet.http.HttpServletResponse; |
| 29 |
|
| 30 |
@WebServlet(urlPatterns = "/sec/*") |
| 31 |
@ServletSecurity(@HttpConstraint(rolesAllowed = "admin")) |
| 32 |
public class SecuredServlet extends HttpServlet |
| 33 |
{ |
| 34 |
@Override |
| 35 |
protected void doPost(HttpServletRequest req, HttpServletResponse resp) |
| 36 |
throws IOException |
| 37 |
{ |
| 38 |
PrintWriter writer = resp.getWriter(); |
| 39 |
writer.println("<html>"); |
| 40 |
writer.println("<HEAD><link rel=\"stylesheet\" type=\"text/css\" href=\"../stylesheet.css\"/></HEAD>"); |
| 41 |
writer.println("<body>"); |
| 42 |
writer.println("<h1>@ServletSecurity</h1>"); |
| 43 |
writer.println("<pre>"); |
| 44 |
writer.println("@ServletSecurity"); |
| 45 |
writer.println("public class SecuredServlet"); |
| 46 |
writer.println("</pre>"); |
| 47 |
writer.println("<p><b>Result: <span class=\"pass\">PASS</span></b></p>"); |
| 48 |
String context = getServletConfig().getServletContext().getContextPath(); |
| 49 |
if (!context.endsWith("/")) |
| 50 |
context += "/"; |
| 51 |
writer.println("<p><A HREF=\"" + context + "logout.jsp\">Logout</A></p>"); |
| 52 |
writer.println("</body>"); |
| 53 |
writer.println("</html>"); |
| 54 |
writer.flush(); |
| 55 |
writer.close(); |
| 56 |
} |
| 57 |
} |