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.guice.aop; 020 021import com.google.inject.AbstractModule; 022import com.google.inject.matcher.AbstractMatcher; 023import com.google.inject.matcher.Matchers; 024import org.apache.shiro.aop.AnnotationMethodInterceptor; 025import org.apache.shiro.aop.AnnotationResolver; 026import org.apache.shiro.aop.DefaultAnnotationResolver; 027import org.apache.shiro.authz.aop.AuthenticatedAnnotationMethodInterceptor; 028import org.apache.shiro.authz.aop.GuestAnnotationMethodInterceptor; 029import org.apache.shiro.authz.aop.PermissionAnnotationMethodInterceptor; 030import org.apache.shiro.authz.aop.RoleAnnotationMethodInterceptor; 031import org.apache.shiro.authz.aop.UserAnnotationMethodInterceptor; 032 033import java.lang.annotation.Annotation; 034import java.lang.reflect.Method; 035 036/** 037 * Install this module to enable Shiro AOP functionality in Guice. You may extend it to add your own Shiro 038 * interceptors, override the default ones, or provide a specific {@link org.apache.shiro.aop.AnnotationResolver}. 039 */ 040public class ShiroAopModule extends AbstractModule { 041 @Override 042 protected final void configure() { 043 AnnotationResolver resolver = createAnnotationResolver(); 044 configureDefaultInterceptors(resolver); 045 configureInterceptors(resolver); 046 } 047 048 protected final void bindShiroInterceptor(final AnnotationMethodInterceptor methodInterceptor) { 049 bindInterceptor(Matchers.any(), new AbstractMatcher<Method>() { 050 public boolean matches(Method method) { 051 Class<? extends Annotation> annotation = methodInterceptor.getHandler().getAnnotationClass(); 052 return method.getAnnotation(annotation) != null 053 || method.getDeclaringClass().getAnnotation(annotation) != null; 054 } 055 }, new AopAllianceMethodInterceptorAdapter(methodInterceptor)); 056 } 057 058 protected AnnotationResolver createAnnotationResolver() { 059 return new DefaultAnnotationResolver(); 060 } 061 062 protected void configureDefaultInterceptors(AnnotationResolver resolver) { 063 bindShiroInterceptor(new RoleAnnotationMethodInterceptor(resolver)); 064 bindShiroInterceptor(new PermissionAnnotationMethodInterceptor(resolver)); 065 bindShiroInterceptor(new AuthenticatedAnnotationMethodInterceptor(resolver)); 066 bindShiroInterceptor(new UserAnnotationMethodInterceptor(resolver)); 067 bindShiroInterceptor(new GuestAnnotationMethodInterceptor(resolver)); 068 } 069 070 protected void configureInterceptors(AnnotationResolver resolver) { 071 072 } 073}