|
| 1 | +<!doctype html> |
| 2 | +<!-- |
| 3 | +
|
| 4 | + DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
| 5 | +
|
| 6 | + Copyright (c) 2015 Oracle and/or its affiliates. All rights reserved. |
| 7 | +
|
| 8 | + The contents of this file are subject to the terms of either the GNU |
| 9 | + General Public License Version 2 only ("GPL") or the Common Development |
| 10 | + and Distribution License("CDDL") (collectively, the "License"). You |
| 11 | + may not use this file except in compliance with the License. You can |
| 12 | + obtain a copy of the License at |
| 13 | + http://glassfish.java.net/public/CDDL+GPL_1_1.html |
| 14 | + or packager/legal/LICENSE.txt. See the License for the specific |
| 15 | + language governing permissions and limitations under the License. |
| 16 | +
|
| 17 | + When distributing the software, include this License Header Notice in each |
| 18 | + file and include the License file at packager/legal/LICENSE.txt. |
| 19 | +
|
| 20 | + GPL Classpath Exception: |
| 21 | + Oracle designates this particular file as subject to the "Classpath" |
| 22 | + exception as provided by Oracle in the GPL Version 2 section of the License |
| 23 | + file that accompanied this code. |
| 24 | +
|
| 25 | + Modifications: |
| 26 | + If applicable, add the following below the License Header, with the fields |
| 27 | + enclosed by brackets [] replaced by your own identifying information: |
| 28 | + "Portions Copyright [year] [name of copyright owner]" |
| 29 | +
|
| 30 | + Contributor(s): |
| 31 | + If you wish your version of this file to be governed by only the CDDL or |
| 32 | + only the GPL Version 2, indicate your decision by adding "[Contributor] |
| 33 | + elects to include this software in this distribution under the [CDDL or GPL |
| 34 | + Version 2] license." If you don't indicate a single choice of license, a |
| 35 | + recipient has the option to distribute your version of this file under |
| 36 | + either the CDDL, the GPL Version 2 or to extend the choice of license to |
| 37 | + its licensees as provided above. However, if you add GPL Version 2 code |
| 38 | + and therefore, elected the GPL Version 2 license, then the option applies |
| 39 | + and therefore, elected the GPL Version 2 license, then the option applies |
| 40 | + only if the new code is made subject to such option by the copyright |
| 41 | + holder. |
| 42 | +
|
| 43 | +--> |
| 44 | +<html lang="en"> |
| 45 | + <head> |
| 46 | + <meta charset="utf-8"> |
| 47 | + <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> |
| 48 | + |
| 49 | + <title>Entity Data Filtering - Role-based Entity Filtering</title> |
| 50 | + |
| 51 | + <!-- Bootstrap --> |
| 52 | + <link href="http://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet"> |
| 53 | + </head> |
| 54 | + <body> |
| 55 | + |
| 56 | + <div class="container"> |
| 57 | + <div class="row"> |
| 58 | + <div class="col-lg-offset-1 col-lg-10"> |
| 59 | + <header class="page-header"> |
| 60 | + <h1>Entity Data Filtering <small>Role-based Entity Filtering using security annotations</small></h1> |
| 61 | + </header> |
| 62 | + |
| 63 | + <p> |
| 64 | + This example demonstrates how to use entity filtering feature together with security annotations (from |
| 65 | + <code>javax.annotation.security</code> package) and how to apply them on domain classes as well as on |
| 66 | + JAX-RS resource classes or JAX-RS resource methods. |
| 67 | + </p> |
| 68 | + <p> |
| 69 | + In addition to domain classes and JAX-RS resources (with security annotations applied) there is also one |
| 70 | + (pre-matching) container request filter, <code>SecurityRequestFilter</code>. The filter sets security |
| 71 | + context for each incoming request as if the request was invoked by a user in role "manager". |
| 72 | + </p> |
| 73 | + <p> |
| 74 | + The full description how Entity Data Filtering can be found in Jersey User Guide, chapter |
| 75 | + <a href="https://jersey.java.net/documentation/latest/entity-filtering.html" target="_blank">Entity Data Filtering</a>. |
| 76 | + Sections relevant to this example (describing this exact example) are: |
| 77 | + <ul> |
| 78 | + <li><a href="https://jersey.java.net/documentation/latest/entity-filtering.html#d0e13911" target="_blank">Enabling and configuring Entity Filtering in your application</a></li> |
| 79 | + <li><a href="https://jersey.java.net/documentation/latest/entity-filtering.html#ef.security.annotations" target="_blank">Role-based Entity Filtering using (javax.annotation.security) annotations</a></li> |
| 80 | + </ul> |
| 81 | + </p> |
| 82 | + |
| 83 | + <h2>Contents</h2> |
| 84 | + |
| 85 | + <p> |
| 86 | + The mapping of the URI path space is presented in the following table: |
| 87 | + </p> |
| 88 | + |
| 89 | + <table class="table table-bordered"> |
| 90 | + <thead> |
| 91 | + <tr> |
| 92 | + <th>URI path</th> |
| 93 | + <th>Resource class</th> |
| 94 | + <th>HTTP methods</th> |
| 95 | + <th>Allowed values</th> |
| 96 | + <th>Notes</th> |
| 97 | + </tr> |
| 98 | + </thead> |
| 99 | + |
| 100 | + <tbody> |
| 101 | + <tr> |
| 102 | + <td><code>/restricted-resource/denyAll</code></td> |
| 103 | + <td>RestrictedResource</td> |
| 104 | + <td>GET</td> |
| 105 | + <td>N/A</td> |
| 106 | + <td>@DenyAll annotation used - returns HTTP 403, Forbidden response</td> |
| 107 | + </tr> |
| 108 | + <tr> |
| 109 | + <td><code>/restricted-resource/permitAll</code></td> |
| 110 | + <td>RestrictedResource</td> |
| 111 | + <td>GET</td> |
| 112 | + <td>N/A</td> |
| 113 | + <td> |
| 114 | + @PermitAll annotation used<br/> |
| 115 | + Role-based view on RestrictedEntity class - permitAll, simpleField |
| 116 | + </td> |
| 117 | + </tr> |
| 118 | + <tr> |
| 119 | + <td><code>/restricted-resource/rolesAllowed</code></td> |
| 120 | + <td>RestrictedResource</td> |
| 121 | + <td>GET</td> |
| 122 | + <td>N/A</td> |
| 123 | + <td> |
| 124 | + @RolesAllowed({"manager"}) annotation used, user in role "manager"<br/> |
| 125 | + Role-based view on RestrictedEntity class - permitAll, simpleField, mixedField.managerField |
| 126 | + </td> |
| 127 | + </tr> |
| 128 | + <tr> |
| 129 | + <td><code>/unrestricted-resource</code></td> |
| 130 | + <td>UnrestrictedResource</td> |
| 131 | + <td>GET</td> |
| 132 | + <td>N/A</td> |
| 133 | + <td> |
| 134 | + No security annotation used, user in role "manager"<br/> |
| 135 | + Role-based view on RestrictedEntity class - permitAll, simpleField, mixedField.managerField |
| 136 | + </td> |
| 137 | + </tr> |
| 138 | + </tbody> |
| 139 | + </table> |
| 140 | + |
| 141 | + <p> |
| 142 | + Application is based on Grizzly container (see <code>App</code>). Everything needed (resources/providers) |
| 143 | + is registered in <code>SecurityEntityFilteringApplication</code>. |
| 144 | + </p> |
| 145 | + |
| 146 | + <h2>Running the Example</h2> |
| 147 | + |
| 148 | + <p>Run the example as follows:</p> |
| 149 | + <blockquote> |
| 150 | + <pre>mvn clean package exec:java</pre> |
| 151 | + </blockquote> |
| 152 | + |
| 153 | + <p> |
| 154 | + This deploys current example using Grizzly. You can access the application at: |
| 155 | + <ul> |
| 156 | + <li><a href="http://localhost:8080/unrestricted-resource">http://localhost:8080/unrestricted-resource</a></li> |
| 157 | + <li><a href="http://localhost:8080/restricted-resource/denyAll">http://localhost:8080/restricted-resource/denyAll</a></li> |
| 158 | + <li><a href="http://localhost:8080/restricted-resource/permitAll">http://localhost:8080/restricted-resource/permitAll</a></li> |
| 159 | + <li><a href="http://localhost:8080/restricted-resource/rolesAllowed">http://localhost:8080/restricted-resource/rolesAllowed</a></li> |
| 160 | + <li><a href="http://localhost:8080/restricted-resource/runtimeRolesAllowed?roles=manager,user">http://localhost:8080/restricted-resource/runtimeRolesAllowed?roles=manager,user</a></li> |
| 161 | + </ul> |
| 162 | + </p> |
| 163 | + |
| 164 | + <h2>Using Jackson instead of MOXy</h2> |
| 165 | + |
| 166 | + <p> |
| 167 | + This examples uses by default Entity Data Filtering feature together with MOXy. To switch MOXy JSON provider |
| 168 | + to Jackson (2.x) JSON provider simply |
| 169 | + |
| 170 | + <ul> |
| 171 | + <li> |
| 172 | + comment registration of MOXy ContextResolver, and<br/> |
| 173 | + <code>register(new MoxyJsonConfig().setFormattedOutput(true).resolver())</code> |
| 174 | + </li> |
| 175 | + <li> |
| 176 | + uncomment registration of JacksonFeature<br/> |
| 177 | + <code>register(JacksonFeature.class)</code> |
| 178 | + </li> |
| 179 | + </ul> |
| 180 | + |
| 181 | + in <code>SecurityEntityFilteringApplication</code> class. |
| 182 | + </p> |
| 183 | + |
| 184 | + </div> |
| 185 | + </div> |
| 186 | + </div> |
| 187 | + |
| 188 | + <script src="http://netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script> |
| 189 | + </body> |
| 190 | +</html> |
0 commit comments