|
| 1 | +package org.javaee7.jacc.contexts.servlet; |
| 2 | + |
| 3 | +import static java.security.Policy.getPolicy; |
| 4 | +import static java.util.Collections.list; |
| 5 | + |
| 6 | +import java.io.IOException; |
| 7 | +import java.security.CodeSource; |
| 8 | +import java.security.Permission; |
| 9 | +import java.security.PermissionCollection; |
| 10 | +import java.security.Principal; |
| 11 | +import java.security.ProtectionDomain; |
| 12 | +import java.security.cert.Certificate; |
| 13 | +import java.util.HashSet; |
| 14 | +import java.util.Set; |
| 15 | + |
| 16 | +import javax.security.auth.Subject; |
| 17 | +import javax.security.jacc.PolicyContext; |
| 18 | +import javax.security.jacc.PolicyContextException; |
| 19 | +import javax.security.jacc.WebRoleRefPermission; |
| 20 | +import javax.servlet.ServletException; |
| 21 | +import javax.servlet.annotation.WebServlet; |
| 22 | +import javax.servlet.http.HttpServlet; |
| 23 | +import javax.servlet.http.HttpServletRequest; |
| 24 | +import javax.servlet.http.HttpServletResponse; |
| 25 | + |
| 26 | +/** |
| 27 | + * This Servlet demonstrates both how to obtain the Subject and then how to retrieve the roles from |
| 28 | + * this Subject. |
| 29 | + * |
| 30 | + * @author Arjan Tijms |
| 31 | + * |
| 32 | + */ |
| 33 | +@WebServlet(urlPatterns = "/subjectServlet") |
| 34 | +public class SubjectServlet extends HttpServlet { |
| 35 | + |
| 36 | + private static final long serialVersionUID = 1L; |
| 37 | + |
| 38 | + @Override |
| 39 | + public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
| 40 | + |
| 41 | + try { |
| 42 | + Subject subject = (Subject) PolicyContext.getContext("javax.security.auth.Subject.container"); |
| 43 | + |
| 44 | + if (subject != null) { |
| 45 | + response.getWriter().print("Obtained subject from context.\n"); |
| 46 | + |
| 47 | + // Get the permissions associated with the Subject we obtained |
| 48 | + PermissionCollection permissionCollection = getPermissionCollection(subject); |
| 49 | + |
| 50 | + // Resolve any potentially unresolved permissions |
| 51 | + permissionCollection.implies(new WebRoleRefPermission("", "nothing")); |
| 52 | + |
| 53 | + // Filter just the roles from all the permissions, which may include things like |
| 54 | + // java.net.SocketPermission, java.io.FilePermission, and obtain the actual role names. |
| 55 | + Set<String> roles = filterRoles(request, permissionCollection); |
| 56 | + |
| 57 | + for (String role : roles) { |
| 58 | + response.getWriter().print("User has role " + role + "\n"); |
| 59 | + } |
| 60 | + } |
| 61 | + } catch (PolicyContextException e) { |
| 62 | + e.printStackTrace(response.getWriter()); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + private PermissionCollection getPermissionCollection(Subject subject) { |
| 67 | + return getPolicy().getPermissions( |
| 68 | + new ProtectionDomain( |
| 69 | + new CodeSource(null, (Certificate[]) null), |
| 70 | + null, null, |
| 71 | + subject.getPrincipals().toArray(new Principal[subject.getPrincipals().size()]) |
| 72 | + ) |
| 73 | + ); |
| 74 | + } |
| 75 | + |
| 76 | + private Set<String> filterRoles(HttpServletRequest request, PermissionCollection permissionCollection) { |
| 77 | + Set<String> roles = new HashSet<>(); |
| 78 | + for (Permission permission : list(permissionCollection.elements())) { |
| 79 | + if (permission instanceof WebRoleRefPermission) { |
| 80 | + String role = permission.getActions(); |
| 81 | + |
| 82 | + // Note that the WebRoleRefPermission is given for every Servlet in the application, even when |
| 83 | + // no role refs are used anywhere. This will also include Servlets like the default servlet and the |
| 84 | + // implicit JSP servlet. So if there are 2 application roles, and 3 application servlets, then |
| 85 | + // at least 6 WebRoleRefPermission elements will be present in the collection. |
| 86 | + if (!roles.contains(role) && request.isUserInRole(role)) { |
| 87 | + roles.add(role); |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + return roles; |
| 93 | + } |
| 94 | + |
| 95 | + |
| 96 | +} |
0 commit comments