@PreAuthorize不工作 - 是否有无法解析的循环引用?

Vis*_*hwa 7 java spring-security user-roles spring-boot

我正在尝试使用@PreAuthorize注释的弹簧安全性(用户角色授权)的示例,遇到以下错误.

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)
Run Code Online (Sandbox Code Playgroud)

我的WebSecurityConfigurerAdapter扩展类是:

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

}
Run Code Online (Sandbox Code Playgroud)

和UserController中的方法级别授权检查:

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

我在登录时获得用户权限(List),其中有111个

任何人都可以帮我解决错误吗?

Vla*_*kov 0

不要对 WebSecurityConfigurerAdapter 使用 static 修饰符。

尝试以下片段:

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

@EnableAutoConfiguration 注释不应位于您的控制器周围,而应位于您的应用程序周围。