Spring Security Java Config

前端 未结 4 636
一生所求
一生所求 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:13

    from the stacktrace, you don't have authentication manager defiend correctly :

    Caused by: java.lang.IllegalArgumentException: Expecting to only find a single bean for type interface org.springframework.security.authentication.AuthenticationManager, but found []
    

    Here is working example :

    @Configuration
    @EnableWebSecurity
    public class SecurityConfig extends WebSecurityConfigurerAdapter {
    
        @Autowired
        public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
            auth
                .inMemoryAuthentication()
                    .withUser("user").password("password").roles("USER");
        }
    }
    

    Just add the global method annotation, and change to method to not override and be autowired.

提交回复
热议问题