wlowrd 2024-04-22 23:04 采纳率: 33.3%
浏览 565
已结题

springboot项目出现webSecurityConfigurerAdapter.class cannot be opened because it does not exist异常

项目使用的springsecurity版本为6.1.2,oauth2版本为2.2.5
springsecurity在5.X以后已经弃用了WebSecurityConfigurerAdapter,使用全局搜索并没有找出有任何依赖WebSecurityConfigurerAdapter的, 所以本没有在本项目中的任何地方引用WebSecurityConfigurerAdapter, 但是在项目启动时仍然出现WebSecurityConfigurerAdapter.class cannot be opened because it does not exist的情况


org.springframework.beans.factory.BeanDefinitionStoreException: I/O failure while processing configuration class [org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerSecurityConfiguration]
    at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:251) ~[spring-context-6.1.2.jar:6.1.2]
    at org.springframework.context.annotation.ConfigurationClassParser.processImports(ConfigurationClassParser.java:534) ~[spring-context-6.1.2.jar:6.1.2]
    at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:321) ~[spring-context-6.1.2.jar:6.1.2]
    at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:246) ~[spring-context-6.1.2.jar:6.1.2]
    at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:189) ~[spring-context-6.1.2.jar:6.1.2]
    at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:314) ~[spring-context-6.1.2.jar:6.1.2]
    at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:246) ~[spring-context-6.1.2.jar:6.1.2]
    at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:197) ~[spring-context-6.1.2.jar:6.1.2]
    at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:165) ~[spring-context-6.1.2.jar:6.1.2]
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:416) ~[spring-context-6.1.2.jar:6.1.2]
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:289) ~[spring-context-6.1.2.jar:6.1.2]
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:349) ~[spring-context-6.1.2.jar:6.1.2]
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:118) ~[spring-context-6.1.2.jar:6.1.2]
    at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:789) ~[spring-context-6.1.2.jar:6.1.2]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:606) ~[spring-context-6.1.2.jar:6.1.2]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:146) ~[spring-boot-3.2.1.jar:3.2.1]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:762) ~[spring-boot-3.2.1.jar:3.2.1]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:464) ~[spring-boot-3.2.1.jar:3.2.1]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:334) ~[spring-boot-3.2.1.jar:3.2.1]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1358) ~[spring-boot-3.2.1.jar:3.2.1]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1347) ~[spring-boot-3.2.1.jar:3.2.1]
    at org.piggymetrics.AuthApplication.main(AuthApplication.java:24) ~[classes/:na]
Caused by: java.io.FileNotFoundException: class path resource [org/springframework/security/config/annotation/web/configuration/WebSecurityConfigurerAdapter.class] cannot be opened because it does not exist
    at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:215) ~[spring-core-6.1.2.jar:6.1.2]
    at org.springframework.core.type.classreading.SimpleMetadataReader.getClassReader(SimpleMetadataReader.java:54) ~[spring-core-6.1.2.jar:6.1.2]
    at org.springframework.core.type.classreading.SimpleMetadataReader.<init>(SimpleMetadataReader.java:48) ~[spring-core-6.1.2.jar:6.1.2]
    at org.springframework.core.type.classreading.SimpleMetadataReaderFactory.getMetadataReader(SimpleMetadataReaderFactory.java:103) ~[spring-core-6.1.2.jar:6.1.2]
    at org.springframework.boot.type.classreading.ConcurrentReferenceCachingMetadataReaderFactory.createMetadataReader(ConcurrentReferenceCachingMetadataReaderFactory.java:86) ~[spring-boot-3.2.1.jar:3.2.1]
    at org.springframework.boot.type.classreading.ConcurrentReferenceCachingMetadataReaderFactory.getMetadataReader(ConcurrentReferenceCachingMetadataReaderFactory.java:73) ~[spring-boot-3.2.1.jar:3.2.1]
    at org.springframework.core.type.classreading.SimpleMetadataReaderFactory.getMetadataReader(SimpleMetadataReaderFactory.java:81) ~[spring-core-6.1.2.jar:6.1.2]
    at org.springframework.context.annotation.ConfigurationClassParser.asSourceClass(ConfigurationClassParser.java:630) ~[spring-context-6.1.2.jar:6.1.2]
    at org.springframework.context.annotation.ConfigurationClassParser$SourceClass.getSuperClass(ConfigurationClassParser.java:943) ~[spring-context-6.1.2.jar:6.1.2]
    at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:351) ~[spring-context-6.1.2.jar:6.1.2]
    at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:246) ~[spring-context-6.1.2.jar:6.1.2]
    ... 21 common frames omitted

OAuth2AuthorizationConfig类如下

@Configuration //声明一个类为配置类
@EnableAuthorizationServer
public class OAuth2AuthorizationConfig extends AuthorizationServerConfigurerAdapter{

    //存储和管理 OAuth 令牌的信息库
    private final TokenStore tokenStore = new InMemoryTokenStore();

    //Bean 名称 authenticationManagerBean 表示这是认证管理器的自定义实现。
    //认证管理器用于对用户凭据进行身份验证。
    @Autowired
    @Qualifier("authenticationManager")
    private AuthenticationManager authenticationManager;

    @Autowired
    private MongoUserDetailsService userDetailsService;


    /**
     * 用来配置客户端详情服务(ClientDetailsService),客户端详情信息在
     * 这里进行初始化,能够把客户端详情信息写死在这里或者是通过数据库来存储调取详情信息。
     *
     * @param clients
     * @throws Exception
     */
    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        clients.inMemory()
                .withClient("browser")  //创建一个新的客户端,其中 client-id 是客户端的标识符。
                .authorizedGrantTypes("refresh_token", "password")  //指定客户端支持哪些授权类型,例如密码授权、客户端凭据授权等。
                .scopes("ui")
                .and()  //指定客户端可以访问哪些范围。
                .withClient("account-service")
                .authorizedGrantTypes("client_credentials", "refresh_token")
                .scopes("server");
    }

    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        endpoints
                .tokenStore(tokenStore) //指定用于存储令牌的令牌存储。
                .authenticationManager(authenticationManager)   //指定用于对用户进行身份验证的身份验证管理器。
                .userDetailsService(userDetailsService);    //指定用于加载用户详细信息的
    }

    /**
     * 用来配置令牌端点的安全约束
     * @param security
     * @throws Exception
     */
    @Override
    public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
        security
                //允许任何人访问令牌密钥端点。这允许客户端应用程序获取验证令牌所需的公钥。
                .tokenKeyAccess("permitAll()")
                //要求对令牌检查端点进行身份验证。这意味着只有经过身份验证的用户才能访问该端点,该端点用于验证令牌是否有效。
                .checkTokenAccess("isAuthenticated()")
                //禁用用密码编码器。这通常用于开发和测试目的,因为它允许使用明文密码。
                .passwordEncoder(null);
    }

}

各位技术大V有没有什么好的解决方案

  • 写回答

6条回答 默认 最新

  • GISer Liu 2024-04-22 23:39
    关注

    该回答引用自GPT-3.5,由博主GISer Liu编写:

    根据您描述的问题,出现了 WebSecurityConfigurerAdapter.class cannot be opened because it does not exist 异常。该异常涉及到了 AuthorizationServerSecurityConfiguration 类的配置。在 Spring Security 5.x 版本后,WebSecurityConfigurerAdapter
    已经被弃用,但您的项目中仍然存在对其的依赖导致了问题。
    解决这个问题的思路如下:

    1. 检查依赖 :确保您的项目中没有直接或间接地引入了对 WebSecurityConfigurerAdapter 的依赖。这可能涉及到查看 pom.xmlbuild.gradle 文件中的依赖项,以及检查项目中的其他配置文件。
    2. 升级依赖 :如果您的项目中存在对旧版本 Spring Security 的依赖,尝试将其升级到与您当前使用的 Spring Security 版本兼容的版本。同时,确保升级后相关的配置也进行了更新以适应新版本的 API。
    3. 排除冲突依赖 :有时候,项目中存在多个依赖项可能引入了相同的类,导致冲突。您可以尝试使用 Maven 或 Gradle 的依赖排除功能来排除掉冲突的依赖项。
    4. 查看自定义配置 :检查项目中的自定义配置类,特别是关于 Spring Security 的配置类,确保没有直接或间接地继承了 WebSecurityConfigurerAdapter,或者在配置类中引入了与 WebSecurityConfigurerAdapter 相关的配置。
    5. 查看间接依赖 :有时候,某些依赖项可能间接地引入了对 WebSecurityConfigurerAdapter 的依赖。您可以使用 Maven 或 Gradle 的依赖树功能来查看项目中所有依赖的传递依赖,以确定是否存在间接依赖问题。
    6. 查看日志 :在项目启动时,查看详细的日志信息,特别是关于类加载和依赖项冲突的日志信息,以便更好地定位问题所在。
      根据您的情况,您可以先尝试检查项目的依赖,排除可能存在的冲突或过时的依赖项,然后逐步调整配置以解决问题。

    如果该回答解决了您的问题,请采纳!如果没有,请私信联系或评论您的疑惑

    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 7月28日
  • 创建了问题 4月22日