Spring Security Java Config

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

    According to your stacktrace, there’s no AuthenticationManager bean in your context.

    Expecting to only find a single bean for type interface org.springframework.security.authentication.AuthenticationManager, but found []
    

    It seems that you need to explicitly expose AuthenticationManager as a bean; try this:

    @Configuration
    @EnableWebSecurity
    @EnableGlobalMethodSecurity(prePostEnabled = true)
    public class SecurityConfig extends WebSecurityConfigurerAdapter {
    
        @Override
        protected void registerAuthentication(AuthenticationManagerBuilder auth) throws Exception {
            auth.inMemoryAuthentication()
                .withUser( "user" ).password( "password" ).roles( "USER" );
        }
    
        @Bean @Override
        public AuthenticationManager authenticationManagerBean() throws Exception {
            return super.authenticationManagerBean();
        }
    }
    

提交回复
热议问题