| 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 |
|
| 18 |
import jakarta.annotation.security.DeclareRoles; |
| 19 |
import jakarta.servlet.ServletConfig; |
| 20 |
import jakarta.servlet.ServletException; |
| 21 |
import jakarta.servlet.ServletOutputStream; |
| 22 |
import jakarta.servlet.http.HttpServlet; |
| 23 |
import jakarta.servlet.http.HttpServletRequest; |
| 24 |
import jakarta.servlet.http.HttpServletResponse; |
| 25 |
|
| 26 |
/** |
| 27 |
* RoleAnnotationTest |
| 28 |
* |
| 29 |
* Use DeclareRolesAnnotations from within Jetty. |
| 30 |
*/ |
| 31 |
@DeclareRoles({"server-administrator", "user"}) |
| 32 |
public class RoleAnnotationTest extends HttpServlet |
| 33 |
{ |
| 34 |
private ServletConfig _config; |
| 35 |
|
| 36 |
@Override |
| 37 |
public void init(ServletConfig config) throws ServletException |
| 38 |
{ |
| 39 |
super.init(config); |
| 40 |
_config = config; |
| 41 |
} |
| 42 |
|
| 43 |
@Override |
| 44 |
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException |
| 45 |
{ |
| 46 |
doGet(request, response); |
| 47 |
} |
| 48 |
|
| 49 |
@Override |
| 50 |
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException |
| 51 |
{ |
| 52 |
try |
| 53 |
{ |
| 54 |
response.setContentType("text/html"); |
| 55 |
ServletOutputStream out = response.getOutputStream(); |
| 56 |
out.println("<html>"); |
| 57 |
out.println("<head><link rel=\"stylesheet\" type=\"text/css\" href=\"stylesheet.css\"/></head>"); |
| 58 |
out.println("<h1>Jetty DeclareRoles Annotation Results</h1>"); |
| 59 |
out.println("<body>"); |
| 60 |
|
| 61 |
out.println("<h2>Roles</h2>"); |
| 62 |
boolean result = request.isUserInRole("other"); |
| 63 |
out.println("<br/><b>Result: isUserInRole(\"other\")=" + result + ":" + (result == false ? " <span class=\"pass\">PASS" : " <span class=\"fail\">FAIL") + "</span></b>"); |
| 64 |
|
| 65 |
result = request.isUserInRole("manager"); |
| 66 |
out.println("<br/><b>Result: isUserInRole(\"manager\")=" + result + ":" + (result ? " <span class=\"pass\">PASS" : " <span class=\"fail\">FAIL") + "</span></b>"); |
| 67 |
result = request.isUserInRole("user"); |
| 68 |
out.println("<br/><b>Result: isUserInRole(\"user\")=" + result + ":" + (result ? " <span class=\"pass\">PASS" : " <span class=\"fail\">FAIL") + "</span></b>"); |
| 69 |
String context = _config.getServletContext().getContextPath(); |
| 70 |
if (!context.endsWith("/")) |
| 71 |
context += "/"; |
| 72 |
|
| 73 |
out.println("<p><a href=\"" + context + "logout.jsp\">Logout</A></p>"); |
| 74 |
|
| 75 |
out.println("</body>"); |
| 76 |
out.println("</html>"); |
| 77 |
out.flush(); |
| 78 |
} |
| 79 |
catch (Exception e) |
| 80 |
{ |
| 81 |
throw new ServletException(e); |
| 82 |
} |
| 83 |
} |
| 84 |
} |