Skip to content

Commit 43240d9

Browse files
georgecaobdemers
authored andcommitted
[SHIRO-890] Avoid another proxy creator when @EnableAspectJAutoProxy enabled
Fixes: SHIRO-890
1 parent bfc75e1 commit 43240d9

File tree

6 files changed

+227
-2
lines changed

6 files changed

+227
-2
lines changed

support/spring-boot/spring-boot-starter/pom.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,11 @@
8282
<version>${commons.logging.version}</version>
8383
<scope>test</scope>
8484
</dependency>
85-
85+
<dependency>
86+
<groupId>org.aspectj</groupId>
87+
<artifactId>aspectjweaver</artifactId>
88+
<optional>true</optional>
89+
</dependency>
8690
</dependencies>
8791

8892
<build>

support/spring-boot/spring-boot-starter/src/main/java/org/apache/shiro/spring/boot/autoconfigure/ShiroAnnotationProcessorAutoConfiguration.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@
2121
import org.apache.shiro.mgt.SecurityManager;
2222
import org.apache.shiro.spring.config.AbstractShiroAnnotationProcessorConfiguration;
2323
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
24+
import org.springframework.aop.config.AopConfigUtils;
2425
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
26+
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
27+
import org.springframework.boot.autoconfigure.aop.AopAutoConfiguration;
2528
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
2629
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
2730
import org.springframework.context.annotation.Bean;
@@ -32,13 +35,14 @@
3235
* @since 1.4.0
3336
*/
3437
@SuppressWarnings("SpringFacetCodeInspection")
38+
@AutoConfigureAfter(AopAutoConfiguration.class)
3539
@Configuration
3640
@ConditionalOnProperty(name = "shiro.annotations.enabled", matchIfMissing = true)
3741
public class ShiroAnnotationProcessorAutoConfiguration extends AbstractShiroAnnotationProcessorConfiguration {
3842

3943
@Bean
4044
@DependsOn("lifecycleBeanPostProcessor")
41-
@ConditionalOnMissingBean
45+
@ConditionalOnMissingBean(name = AopConfigUtils.AUTO_PROXY_CREATOR_BEAN_NAME)
4246
@Override
4347
public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator() {
4448
return super.defaultAdvisorAutoProxyCreator();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.shiro.spring.boot.autoconfigure;
20+
21+
import org.junit.Test
22+
import org.junit.runner.RunWith
23+
import org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator
24+
import org.springframework.aop.config.AopConfigUtils
25+
import org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator
26+
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator
27+
import org.springframework.beans.BeansException
28+
import org.springframework.beans.factory.annotation.Autowired
29+
import org.springframework.boot.test.context.SpringBootTest
30+
import org.springframework.context.ApplicationContext
31+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner
32+
33+
import static org.hamcrest.Matchers.*
34+
import static org.hamcrest.MatcherAssert.assertThat
35+
36+
@SpringBootTest(classes = AspectjAndDefaultProxyCreatorApplication.class)
37+
@RunWith(SpringJUnit4ClassRunner.class)
38+
class AspectjAndDefaultProxyCreatorTest {
39+
40+
@Autowired
41+
private ApplicationContext applicationContext
42+
43+
@Test
44+
void defaultAdvisorAutoProxyCreator() throws BeansException {
45+
// There are two proxy creators before SHIRO-890 which causes problem when @EnableAspectJAutoProxy is enabled.
46+
String[] names = ["defaultAdvisorAutoProxyCreator", AopConfigUtils.AUTO_PROXY_CREATOR_BEAN_NAME]
47+
for (String name : names) {
48+
Object creator = applicationContext.getBean(name)
49+
assertThat(creator, anyOf(
50+
instanceOf(DefaultAdvisorAutoProxyCreator.class),
51+
instanceOf(AnnotationAwareAspectJAutoProxyCreator.class)
52+
))
53+
}
54+
String[] beanNames = applicationContext.getBeanNamesForType(AbstractAdvisorAutoProxyCreator.class)
55+
assertThat(names, arrayContainingInAnyOrder(beanNames))
56+
}
57+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.shiro.spring.boot.autoconfigure;
20+
21+
import org.junit.Test
22+
import org.junit.runner.RunWith
23+
import org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator
24+
import org.springframework.aop.config.AopConfigUtils
25+
import org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator
26+
import org.springframework.beans.BeansException
27+
import org.springframework.beans.factory.annotation.Autowired
28+
import org.springframework.boot.test.context.SpringBootTest
29+
import org.springframework.context.ApplicationContext
30+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner
31+
32+
import static org.hamcrest.MatcherAssert.assertThat
33+
import static org.hamcrest.Matchers.*
34+
35+
@SpringBootTest(classes = AspectjEnabledApplication.class)
36+
@RunWith(SpringJUnit4ClassRunner.class)
37+
class ShiroAnnotationProcessorAutoConfigurationTest {
38+
39+
@Autowired
40+
private ApplicationContext applicationContext
41+
42+
@Test
43+
void defaultAdvisorAutoProxyCreator() throws BeansException {
44+
// There is only one proxy creator, and it's AnnotationAwareAspectJAutoProxyCreator as expected.
45+
Object creator = applicationContext.getBean(AopConfigUtils.AUTO_PROXY_CREATOR_BEAN_NAME)
46+
assertThat("@EnableAspectJAutoProxy will create an instance of AnnotationAwareAspectJAutoProxyCreator",
47+
creator, instanceOf(AnnotationAwareAspectJAutoProxyCreator.class))
48+
String[] names = applicationContext.getBeanNamesForType(AbstractAdvisorAutoProxyCreator.class)
49+
assertThat(names, arrayWithSize(1))
50+
}
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.shiro.spring.boot.autoconfigure;
20+
21+
import org.apache.shiro.realm.Realm;
22+
import org.apache.shiro.realm.text.TextConfigurationRealm;
23+
import org.springframework.aop.config.AopConfigUtils;
24+
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
25+
import org.springframework.boot.SpringApplication;
26+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
27+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
28+
import org.springframework.context.annotation.Bean;
29+
import org.springframework.context.annotation.DependsOn;
30+
import org.springframework.context.annotation.EnableAspectJAutoProxy;
31+
32+
@EnableAutoConfiguration
33+
@EnableAspectJAutoProxy
34+
public class AspectjAndDefaultProxyCreatorApplication {
35+
36+
public static void main(String... args) {
37+
SpringApplication.run(AspectjAndDefaultProxyCreatorApplication.class);
38+
}
39+
40+
@Bean
41+
@SuppressWarnings("Duplicates")
42+
Realm getTextConfigurationRealm() {
43+
44+
TextConfigurationRealm realm = new TextConfigurationRealm();
45+
realm.setUserDefinitions("joe.coder=password,user\n" +
46+
"jill.coder=password,admin");
47+
48+
realm.setRoleDefinitions("admin=read,write\n" +
49+
"user=read");
50+
realm.setCachingEnabled(true);
51+
return realm;
52+
}
53+
54+
@Bean
55+
@DependsOn("lifecycleBeanPostProcessor")
56+
@ConditionalOnMissingBean(value = DefaultAdvisorAutoProxyCreator.class, name = AopConfigUtils.AUTO_PROXY_CREATOR_BEAN_NAME)
57+
public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator() {
58+
return new DefaultAdvisorAutoProxyCreator();
59+
}
60+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.shiro.spring.boot.autoconfigure;
20+
21+
import org.apache.shiro.realm.Realm;
22+
import org.apache.shiro.realm.text.TextConfigurationRealm;
23+
import org.springframework.boot.SpringApplication;
24+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
25+
import org.springframework.context.annotation.Bean;
26+
import org.springframework.context.annotation.EnableAspectJAutoProxy;
27+
28+
@EnableAutoConfiguration
29+
@EnableAspectJAutoProxy
30+
public class AspectjEnabledApplication {
31+
32+
public static void main(String... args) {
33+
SpringApplication.run(AspectjEnabledApplication.class);
34+
}
35+
36+
@Bean
37+
@SuppressWarnings("Duplicates")
38+
Realm getTextConfigurationRealm() {
39+
40+
TextConfigurationRealm realm = new TextConfigurationRealm();
41+
realm.setUserDefinitions("joe.coder=password,user\n" +
42+
"jill.coder=password,admin");
43+
44+
realm.setRoleDefinitions("admin=read,write\n" +
45+
"user=read");
46+
realm.setCachingEnabled(true);
47+
return realm;
48+
}
49+
}

0 commit comments

Comments
 (0)