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.web.filter.authz.AuthorizationFilter; 028 029import javax.ws.rs.Priorities; 030import javax.ws.rs.container.DynamicFeature; 031import javax.ws.rs.container.ResourceInfo; 032import javax.ws.rs.core.FeatureContext; 033import java.lang.annotation.Annotation; 034import java.util.ArrayList; 035import java.util.List; 036import java.util.stream.Collectors; 037import java.util.stream.Stream; 038import javax.annotation.security.DenyAll; 039import javax.annotation.security.PermitAll; 040import javax.annotation.security.RolesAllowed; 041 042import static org.apache.shiro.web.jaxrs.SubjectPrincipalRequestFilter.SHIRO_WEB_JAXRS_DISABLE_PRINCIPAL_PARAM; 043 044/** 045 * Wraps {@link AuthorizationFilter filters} around JAX-RS resources that are annotated with Shiro annotations. 046 * 047 * @since 1.4 048 */ 049public class ShiroAnnotationFilterFeature implements DynamicFeature { 050 051 private static final List<Class<? extends Annotation>> SHIRO_ANNOTATIONS = List.of( 052 RequiresPermissions.class, 053 RequiresRoles.class, 054 RequiresAuthentication.class, 055 RequiresUser.class, 056 RequiresGuest.class); 057 058 private static final List<Class<? extends Annotation>> JSR_250_ANNOTATIONS = List.of( 059 RolesAllowed.class, PermitAll.class, DenyAll.class); 060 061 @Override 062 public void configure(ResourceInfo resourceInfo, FeatureContext context) { 063 List<Annotation> authzSpecs = new ArrayList<>(); 064 var annotations = SHIRO_ANNOTATIONS; 065 if (Boolean.TRUE.equals(context.getConfiguration().getProperty(SHIRO_WEB_JAXRS_DISABLE_PRINCIPAL_PARAM))) { 066 annotations = Stream.concat(SHIRO_ANNOTATIONS.stream(), JSR_250_ANNOTATIONS.stream()) 067 .collect(Collectors.toList()); 068 } 069 070 for (Class<? extends Annotation> annotationClass : annotations) { 071 // XXX What is the performance of getAnnotation vs getAnnotations? 072 Annotation classAuthzSpec = resourceInfo.getResourceClass().getAnnotation(annotationClass); 073 Annotation methodAuthzSpec = resourceInfo.getResourceMethod().getAnnotation(annotationClass); 074 075 if (classAuthzSpec != null) { 076 authzSpecs.add(classAuthzSpec); 077 } 078 if (methodAuthzSpec != null) { 079 authzSpecs.add(methodAuthzSpec); 080 } 081 } 082 083 if (!authzSpecs.isEmpty()) { 084 context.register(new AnnotationAuthorizationFilter(authzSpecs), Priorities.AUTHORIZATION); 085 } 086 } 087}