Spring Security Java Config

前端 未结 4 597
一生所求
一生所求 2021-01-03 03:55

I\'m trying to use JavaConfig instead of XML configuration for Spring Security. I would like to use @PreAuthorization for declaring access rights.

My Sp

4条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-03 04:30

    As NimChimpsky notes, Spring 3.2.0.RELEASE requires a configuration update.

    Using this configuration, I was getting exactly the same exception as the OP, but only about half the time. The other half of the time the app started up just fine. I assume that there's some kind of race condition.

    Originally I was pulling the security configuration in using @Import on my root configuration:

    @Configuration
    @Import(SecurityConfig.class)
    ... other annotations ...
    public class RootConfig {
       ...
    }
    

    When I replaced that with

    public class WebAppInit extends AbstractAnnotationConfigDispatcherServletInitializer {
    
        @Override
        protected Class[] getRootConfigClasses() {
            return new Class[] { RootConfig.class, SecurityConfig.class };
        }
    }
    

    the problem went away. Didn't dive into the reasons why, so I can't speak to that, but you might give it a try.

提交回复
热议问题