View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
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 org.eclipse.jetty.annotations;
20  
21  import java.util.ArrayList;
22  import java.util.List;
23  
24  import javax.servlet.ServletSecurityElement;
25  import javax.servlet.annotation.ServletSecurity;
26  import javax.servlet.annotation.ServletSecurity.EmptyRoleSemantic;
27  import javax.servlet.annotation.ServletSecurity.TransportGuarantee;
28  
29  import org.eclipse.jetty.annotations.AnnotationIntrospector.AbstractIntrospectableAnnotationHandler;
30  import org.eclipse.jetty.security.ConstraintAware;
31  import org.eclipse.jetty.security.ConstraintMapping;
32  import org.eclipse.jetty.security.ConstraintSecurityHandler;
33  import org.eclipse.jetty.servlet.ServletHolder;
34  import org.eclipse.jetty.servlet.ServletMapping;
35  import org.eclipse.jetty.util.log.Log;
36  import org.eclipse.jetty.util.log.Logger;
37  import org.eclipse.jetty.util.security.Constraint;
38  import org.eclipse.jetty.webapp.WebAppContext;
39  
40  /**
41   * ServletSecurityAnnotationHandler
42   *
43   * Inspect a class to see if it has an <code>&#064;ServletSecurity</code> annotation on it,
44   * setting up the <code>&lt;security-constraint&gt;s</code>.
45   *
46   * A servlet can be defined in:
47   * <ul>
48   * <li>web.xml</li>
49   * <li>web-fragment.xml</li>
50   * <li>@WebServlet annotation discovered</li>
51   * <li>ServletContext.createServlet</li>
52   * </ul>
53   *
54   * The ServletSecurity annotation for a servlet should only be processed
55   * iff metadata-complete == false.
56   */
57  public class ServletSecurityAnnotationHandler extends AbstractIntrospectableAnnotationHandler
58  {
59      private static final Logger LOG = Log.getLogger(ServletSecurityAnnotationHandler.class);
60  
61      private WebAppContext _context;
62  
63      public ServletSecurityAnnotationHandler(WebAppContext wac)
64      {
65          super(false);
66          _context = wac;
67      }
68  
69      /**
70       * @see org.eclipse.jetty.annotations.AnnotationIntrospector.IntrospectableAnnotationHandler#handle(java.lang.Class)
71       */
72      public void doHandle(Class clazz)
73      {
74          if (!(_context.getSecurityHandler() instanceof ConstraintAware))
75          {
76              LOG.warn("SecurityHandler not ConstraintAware, skipping security annotation processing");
77              return;
78          }
79  
80         ServletSecurity servletSecurity = (ServletSecurity)clazz.getAnnotation(ServletSecurity.class);
81         if (servletSecurity == null)
82             return;
83  
84         //If there are already constraints defined (ie from web.xml) that match any 
85         //of the url patterns defined for this servlet, then skip the security annotation.
86  
87         List<ServletMapping> servletMappings = getServletMappings(clazz.getCanonicalName());
88         List<ConstraintMapping> constraintMappings =  ((ConstraintAware)_context.getSecurityHandler()).getConstraintMappings();
89  
90         if (constraintsExist(servletMappings, constraintMappings))
91         {
92             LOG.warn("Constraints already defined for "+clazz.getName()+", skipping ServletSecurity annotation");
93             return;
94         }
95  
96         //Make a fresh list
97         constraintMappings = new ArrayList<ConstraintMapping>();
98  
99         ServletSecurityElement securityElement = new ServletSecurityElement(servletSecurity);
100        for (ServletMapping sm : servletMappings)
101        {
102            for (String url : sm.getPathSpecs())
103            {
104                _context.getMetaData().setOrigin("constraint.url."+url,servletSecurity,clazz);
105                constraintMappings.addAll(ConstraintSecurityHandler.createConstraintsWithMappingsForPath(clazz.getName(), url, securityElement));
106            }
107        }
108 
109        //set up the security constraints produced by the annotation
110        ConstraintAware securityHandler = (ConstraintAware)_context.getSecurityHandler();
111 
112        for (ConstraintMapping m:constraintMappings)
113            securityHandler.addConstraintMapping(m);
114        
115        //Servlet Spec 3.1 requires paths with uncovered http methods to be reported
116        securityHandler.checkPathsWithUncoveredHttpMethods();
117     }
118 
119 
120 
121     /**
122      * Make a jetty Constraint object, which represents the <code>&lt;auth-constraint&gt;</code> and
123      * <code>&lt;user-data-constraint&gt;</code> elements, based on the security annotation.
124      * 
125      * @param servlet the servlet
126      * @param rolesAllowed the roles allowed
127      * @param permitOrDeny the role / permission semantic
128      * @param transport the transport guarantee
129      * @return the constraint
130      */
131     protected Constraint makeConstraint (Class servlet, String[] rolesAllowed, EmptyRoleSemantic permitOrDeny, TransportGuarantee transport)
132     {
133         return ConstraintSecurityHandler.createConstraint(servlet.getName(), rolesAllowed, permitOrDeny, transport);
134     }
135 
136 
137 
138     /**
139      * Get the ServletMappings for the servlet's class.
140      * @param className the class name
141      * @return the servlet mappings for the class
142      */
143     protected List<ServletMapping> getServletMappings(String className)
144     {
145         List<ServletMapping> results = new ArrayList<ServletMapping>();
146         ServletMapping[] mappings = _context.getServletHandler().getServletMappings();
147         for (ServletMapping mapping : mappings)
148         {
149             //Check the name of the servlet that this mapping applies to, and then find the ServletHolder for it to find it's class
150             ServletHolder holder = _context.getServletHandler().getServlet(mapping.getServletName());
151             if (holder.getClassName() != null && holder.getClassName().equals(className))
152               results.add(mapping);
153         }
154         return results;
155     }
156 
157 
158 
159     /**
160      * Check if there are already <code>&lt;security-constraint&gt;</code> elements defined that match the url-patterns for
161      * the servlet.
162      * 
163      * @param servletMappings the servlet mappings
164      * @param constraintMappings the constraint mappings
165      * @return true if constraint exists
166      */
167     protected boolean constraintsExist (List<ServletMapping> servletMappings, List<ConstraintMapping> constraintMappings)
168     {
169         boolean exists = false;
170 
171         //Check to see if the path spec on each constraint mapping matches a pathSpec in the servlet mappings.
172         //If it does, then we should ignore the security annotations.
173         for (ServletMapping mapping : servletMappings)
174         {
175             //Get its url mappings
176             String[] pathSpecs = mapping.getPathSpecs();
177             if (pathSpecs == null)
178                 continue;
179 
180             //Check through the constraints to see if there are any whose pathSpecs (url mappings)
181             //match the servlet. If so, then we already have constraints defined for this servlet,
182             //and we will not be processing the annotation (ie web.xml or programmatic override).
183            for (int i=0; constraintMappings != null && i < constraintMappings.size() && !exists; i++)
184            {
185                for (int j=0; j < pathSpecs.length; j++)
186                {
187                    //TODO decide if we need to check the origin
188                    if (pathSpecs[j].equals(constraintMappings.get(i).getPathSpec()))
189                    {
190                        exists = true;
191                        break;
192                    }
193                }
194            }
195         }
196         return exists;
197     }
198 
199 }