首页 > 解决方案 > 创建名为“projectingArgumentResolverBeanPostProcessor”的 bean 时出错

问题描述

我在我的项目中设置我的网络安全,但我看到一个错误。这是错误

org.springframework.beans.factory.BeanCreationException:在类路径资源[org/springframework/data/web/config/ProjectingArgumentResolverRegistrar.class]中定义的名称为“projectingArgumentResolverBeanPostProcessor”的bean创建错误:bean实例化之前的BeanPostProcessor失败;嵌套异常是 org.springframework.beans.factory.BeanCreationException:创建名为“metaDataSourceAdvisor”的 bean 时出错:设置构造函数参数时无法解析对 bean“methodSecurityMetadataSource”的引用;嵌套异常是 org.springframework.beans.factory.BeanCreationException:在类路径资源 [org/springframework/security/config/annotation/method/configuration/GlobalMethodSecurityConfiguration. class]: 通过工厂方法实例化 Bean 失败;嵌套异常是 org.springframework.beans.BeanInstantiationException:无法实例化 [org.springframework.security.access.method.MethodSecurityMetadataSource]:工厂方法“methodSecurityMetadataSource”抛出异常;嵌套异常是 java.lang.IllegalStateException: 在所有全局方法配置的组合中,在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:510) 处实际上没有激活注解支持 ~[spring-beans -5.1.5.RELEASE.jar:5.1.5.RELEASE] 在 org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320) ~[spring-beans-5.1.5.RELEASE .jar:5.1.5.RELEASE] 在 org.springframework.beans。

我的鳕鱼是:

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter{
    @Autowired
    private Environment env;
    @Autowired
    private UserSecurityService usersecurityservice;
    private BCryptPasswordEncoder passwordencoder(){
        return SecurityUtility.passwordEncoder();
    }
    private static final String[]PUBLIC_MATCHES = {
            "/css/**",
            "/js/**",
            "/img/**",
            "/signUp",
            "/",
            "/newUser",
            "/forgetPassword",
            "/login",
            "/fonts/**",
            "/bookshelf/**",
            "/bookDetail/**",
            "/hours",
            "/faq",
            "/searchByCategory",
            "/searchBook"

    };
@Override
protected void configure(HttpSecurity   http)throws Exception{
    http
    .authorizeRequests().
    /*antMatchers("/**").*/
    antMatchers(PUBLIC_MATCHES).
    permitAll().anyRequest().authenticated();

http
    .csrf().disable().cors().disable()
    .formLogin().failureUrl("/login?error")
    .defaultSuccessUrl("/")
    .successForwardUrl("/login")
    .and()
    .logout().logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
    .logoutSuccessUrl("/?logout").deleteCookies("remember-me").permitAll()
    .and()
    .rememberMe();

}
@Autowired
public void configureGlobal (AuthenticationManagerBuilder auth) throws Exception{
    auth.userDetailsService(usersecurityservice).passwordEncoder(passwordencoder());
}

用户安全类是:

@Service
public class UserSecurityService implements UserDetailsService {

    @Autowired()
    private UserRepository userRepository;

public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
    // TODO Auto-generated method stub
    try{

    }catch(Exception ex){
        System.out.println("Error acoured hear:");
    }
    User user=userRepository.findByUsername(username);
    if(null==user){
        throw new UsernameNotFoundException("Username not found");
    }
    return user;
}

当我删除“@EnableGlobalMethodSecurity”注释程序正确运行时,我之前使用过这个代码并且它工作正常。

标签: javaspringspring-bootwebsecurity

解决方案


你最近更新spring了吗?在以前的版本中,可以有一个 null MethodSecurityMetadataSource,但现在他们添加了这个检查,如果你没有启用至少一个方法安全元数据源,他们会抛出你得到的异常(“在所有全局方法的组合中配置,实际上没有激活注释支持”)。当我从 spring 5.0.7 更新到 5.1.5 时,这发生在我身上。这是讨论此更改的问题

要修复它,请在@EnableGlobalMethodSecurity注释属性中启用元数据源之一,或者,如果像我一样,您正在使用某种GlobalMethodSecurityConfiguration,请确保该方法customMethodSecurityMetadataSource返回 not-null


推荐阅读