7

I'm trying an example on spring security (user-role authorization) using @PreAuthorize annotation, got stuck up with below error.

Caused by: org.springframework.beans.BeanInstantiationException:         
    Failed to instantiate [org.aopalliance.intercept.MethodInterceptor]:    
    Factory method 'methodSecurityInterceptor' threw exception; nested exception is     org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'methodSecurityInterceptor': Requested bean is currently in creation: Is there an unresolvable circular reference?
                at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189)
                at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588)
                ... 91 more
        Caused by: org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'methodSecurityInterceptor': Requested bean is currently in creation: I
        s there an unresolvable circular reference?
                at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.beforeSingletonCreation(DefaultSingletonBeanRegistry.java:347)
                at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
                at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
                at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
                at org.springframework.security.access.intercept.aopalliance.MethodSecurityMetadataSourceAdvisor.getAdvice(MethodSecurityMetadataSourceAdvisor.java:107)
                at org.springframework.aop.aspectj.AspectJProxyUtils.isAspectJAdvice(AspectJProxyUtils.java:67)
                at org.springframework.aop.aspectj.AspectJProxyUtils.makeAdvisorChainAspectJCapableIfNecessary(AspectJProxyUtils.java:49)

My WebSecurityConfigurerAdapter extension class is:

@Configuration
@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
@EnableGlobalMethodSecurity(prePostEnabled = true)
public static class FormLoginWebSecurityConfigurerAdapter extends
        WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {

        http.authorizeRequests().anyRequest().authenticated().and()
                .formLogin().loginPage("/login").defaultSuccessUrl("/home")
                .permitAll().and().logout().permitAll()
                .logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
                .logoutSuccessUrl("/login?logout").permitAll()
                .and().httpBasic()

                .and().exceptionHandling()
                .accessDeniedPage("/access?error");

}

And method level authorization check in UserController:

 @Controller
    @EnableAutoConfiguration
    public class UserController {
    @PreAuthorize("hasAnyAuthority('111')")
        @RequestMapping(value = "/users")
        public String userManagement(Model model) {
            .
            return something;
        }
    }

I am getting user authorities (List) at the time of login which has 111 in it

Can any one help me with error am facing?

Vishwa
  • 607
  • 2
  • 11
  • 21

2 Answers2

1

Recently I've had the similar issue.
In my project there were @PreAuthorize annotations and also aspects which handled audit logic.
In my case it was enough to:
1. update security config in the following way

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(mode = ASPECTJ, prePostEnabled = true, securedEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter`

2.add dependency in pom.xml

<dependency>
     <groupId>org.springframework.security</groupId>
     <artifactId>spring-security-aspects</artifactId>
     <version>4.1.0.RELEASE</version>
</dependency>

Hope someone will find that helpful.

John Smith
  • 2,291
  • 4
  • 22
  • 33
Eugene T
  • 343
  • 1
  • 8
0

Don't use static modifier for WebSecurityConfigurerAdapter.

Try following snippet:

@Configuration
@EnableWebSecurity
@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
@EnableGlobalMethodSecurity(securedEnabled = true, proxyTargetClass = true, prePostEnabled = true)
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
// do your stuff here

@EnableAutoConfiguration anotation should be not around your controller but around your application.

Vova Rozhkov
  • 1,582
  • 2
  • 19
  • 27