2015-08-02 9 views
7

Ich versuche ein Beispiel auf Feder Sicherheit (Benutzer-Rolle Autorisierung) mit @ PreAuthorize Annotation, blieb mit unten Fehler stecken.@PreAuthorize funktioniert nicht - Gibt es eine nicht auflösbare Zirkularreferenz?

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) 

Meine WebSecurityConfigurerAdapter Erweiterungsklasse ist:

@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"); 

} 

Und Methode Ebene Berechtigungsprüfung in Usercontroller:

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

Ich erhalte Benutzerberechtigungen (List) zum Zeitpunkt der Login mit 111 darin

Kann mir jemand bei einem Fehler helfen?

Antwort

0

Verwenden Sie keinen statischen Modifikator für WebSecurityConfiguratorAdapter.

Versuchen folgenden Ausschnitt:

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

@EnableAutoConfiguration den Vermerk nicht um den Controller sein sollte, sondern um Ihre Anwendung.

1

Kürzlich hatte ich das ähnliche Problem.
In meinem Projekt gab es @PreAuthorize Anmerkungen und auch Aspekte, die Audit-Logik behandelt.
In meinem Fall war es genug:
1. Sicherheitsupdate config in der folgenden Art und Weise

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

2.Add Abhängigkeit in pom.xml

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

Hoffnung jemand das hilfreich finden.

Verwandte Themen