001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.apache.shiro.web.jaxrs; 020 021 022import org.apache.shiro.authz.annotation.RequiresAuthentication; 023import org.apache.shiro.authz.annotation.RequiresGuest; 024import org.apache.shiro.authz.annotation.RequiresPermissions; 025import org.apache.shiro.authz.annotation.RequiresRoles; 026import org.apache.shiro.authz.annotation.RequiresUser; 027import org.apache.shiro.authz.aop.AuthenticatedAnnotationHandler; 028import org.apache.shiro.authz.aop.AuthorizingAnnotationHandler; 029import org.apache.shiro.authz.aop.DenyAllAnnotationHandler; 030import org.apache.shiro.authz.aop.GuestAnnotationHandler; 031import org.apache.shiro.authz.aop.PermissionAnnotationHandler; 032import org.apache.shiro.authz.aop.PermitAllAnnotationHandler; 033import org.apache.shiro.authz.aop.RoleAnnotationHandler; 034import org.apache.shiro.authz.aop.RolesAllowedAnnotationHandler; 035import org.apache.shiro.authz.aop.UserAnnotationHandler; 036 037import javax.annotation.security.DenyAll; 038import javax.annotation.security.PermitAll; 039import javax.annotation.security.RolesAllowed; 040import javax.ws.rs.container.ContainerRequestContext; 041import javax.ws.rs.container.ContainerRequestFilter; 042import java.io.IOException; 043import java.lang.annotation.Annotation; 044import java.util.Collection; 045import java.util.Collections; 046import java.util.HashMap; 047import java.util.Map; 048 049/** 050 * A filter that grants or denies access to a JAX-RS resource based on the Shiro annotations on it. 051 * 052 * @see org.apache.shiro.authz.annotation 053 * @since 1.4 054 */ 055public class AnnotationAuthorizationFilter implements ContainerRequestFilter { 056 057 private final Map<AuthorizingAnnotationHandler, Annotation> authzChecks; 058 059 public AnnotationAuthorizationFilter(Collection<Annotation> authzSpecs) { 060 Map<AuthorizingAnnotationHandler, Annotation> authChecks = 061 new HashMap<AuthorizingAnnotationHandler, Annotation>(authzSpecs.size()); 062 for (Annotation authSpec : authzSpecs) { 063 authChecks.put(createHandler(authSpec), authSpec); 064 } 065 this.authzChecks = Collections.unmodifiableMap(authChecks); 066 } 067 068 private static AuthorizingAnnotationHandler createHandler(Annotation annotation) { 069 Class<?> t = annotation.annotationType(); 070 if (RequiresPermissions.class.equals(t)) { 071 return new PermissionAnnotationHandler(); 072 } else if (RequiresRoles.class.equals(t)) { 073 return new RoleAnnotationHandler(); 074 } else if (RequiresUser.class.equals(t)) { 075 return new UserAnnotationHandler(); 076 } else if (RequiresGuest.class.equals(t)) { 077 return new GuestAnnotationHandler(); 078 } else if (RequiresAuthentication.class.equals(t)) { 079 return new AuthenticatedAnnotationHandler(); 080 } else if (RolesAllowed.class.equals(t)) { 081 return new RolesAllowedAnnotationHandler(); 082 } else if (PermitAll.class.equals(t)) { 083 return new PermitAllAnnotationHandler(); 084 } else if (DenyAll.class.equals(t)) { 085 return new DenyAllAnnotationHandler(); 086 } else { 087 throw new IllegalArgumentException("Cannot create a handler for the unknown for annotation " + t); 088 } 089 } 090 091 @Override 092 public void filter(ContainerRequestContext requestContext) throws IOException { 093 094 for (Map.Entry<AuthorizingAnnotationHandler, Annotation> authzCheck : authzChecks.entrySet()) { 095 AuthorizingAnnotationHandler handler = authzCheck.getKey(); 096 Annotation authzSpec = authzCheck.getValue(); 097 handler.assertAuthorized(authzSpec); 098 } 099 } 100 101}