直到今天我的應用程序運行良好,但突然間我得到了這個異常,我不知道該如何修復它。Spring Boot - Spring安全依賴的問題:UserDetailsService.class無法打開,因爲它不存在
org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [com.company.app.Application]; nested exception is java.io.FileNotFoundException: class path resource [org/springframework/security/core/userdetails/UserDetailsService.class] cannot be opened because it does not exist
at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:180) ~[spring-context-5.0.0.RC3.jar:5.0.0.RC3]
.....
Caused by: java.io.FileNotFoundException: class path resource [org/springframework/security/core/userdetails/UserDetailsService.class] cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:177) ~[spring-core-5.0.0.RC3.jar:5.0.0.RC3]
at org.springframework.core.type.classreading.SimpleMetadataReader.<init>(SimpleMetadataReader.java:51) ~[spring-core-5.0.0.RC3.jar:5.0.0.RC3]
at org.springframework.core.type.classreading.SimpleMetadataReaderFactory.getMetadataReader(SimpleMetadataReaderFactory.java:99) ~[spring-core-5.0.0.RC3.jar:5.0.0.RC3]
at org.springframework.boot.type.classreading.ConcurrentReferenceCachingMetadataReaderFactory.createMetadataReader(ConcurrentReferenceCachingMetadataReaderFactory.java:89) ~[spring-boot-2.0.0.M3.jar:2.0.0.M3]
at org.springframework.boot.type.classreading.ConcurrentReferenceCachingMetadataReaderFactory.getMetadataReader(ConcurrentReferenceCachingMetadataReaderFactory.java:76) ~[spring-boot-2.0.0.M3.jar:2.0.0.M3]
at org.springframework.core.type.classreading.SimpleMetadataReaderFactory.getMetadataReader(SimpleMetadataReaderFactory.java:94) ~[spring-core-5.0.0.RC3.jar:5.0.0.RC3]
at org.springframework.context.annotation.ConfigurationClassParser.asSourceClass(ConfigurationClassParser.java:703) ~[spring-context-5.0.0.RC3.jar:5.0.0.RC3]
at org.springframework.context.annotation.ConfigurationClassParser$SourceClass.getInterfaces(ConfigurationClassParser.java:886) ~[spring-context-5.0.0.RC3.jar:5.0.0.RC3]
at org.springframework.context.annotation.ConfigurationClassParser.processInterfaces(ConfigurationClassParser.java:373) ~[spring-context-5.0.0.RC3.jar:5.0.0.RC3]
at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:321) ~[spring-context-5.0.0.RC3.jar:5.0.0.RC3]
at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:245) ~[spring-context-5.0.0.RC3.jar:5.0.0.RC3]
at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:190) ~[spring-context-5.0.0.RC3.jar:5.0.0.RC3]
at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:293) ~[spring-context-5.0.0.RC3.jar:5.0.0.RC3]
at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:245) ~[spring-context-5.0.0.RC3.jar:5.0.0.RC3]
at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:198) ~[spring-context-5.0.0.RC3.jar:5.0.0.RC3]
at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:166) ~[spring-context-5.0.0.RC3.jar:5.0.0.RC3]
... 43 common frames omitted
我評論貫徹UserDetailsService
類,但現在我有這樣的例外:
Caused by: java.lang.ClassNotFoundException: org.springframework.security.core.GrantedAuthority
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1269) ~[catalina.jar:9.0.0.M22]
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1104) ~[catalina.jar:9.0.0.M22]
... 80 common frames omitted
好像Spring Security的依賴會帶來一些麻煩。
我的pom.xml(摘要):
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.M3</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security.oauth</groupId>
<artifactId>spring-security-oauth2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
我實現(即使這不是問題):
@Service
public class CustomUserDetailsService implements UserDetailsService {
@Autowired
private SecurityService securityService;
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
return new CustomUserDetails(securityService.findUserCredentialsByUsername(username));
}
}
編輯
這似乎有些奇怪,因爲我可以運行我的測試,並且所有這些測試都會加載主類Application
的上下文。測試不會失敗,但是當我在Tomcat中運行應用程序時,它會失敗!
有什麼想法?由於
我已經更新我的依賴關係並啓動了'mvn clean install',但沒有任何更改 – akuma8
可能依賴關係沒有很好地下載。嘗試在C:\ users \ yourAccount \ .m2 \ org \ springframework下手動刪除它們並執行新的'mvn clean install' –
另請參閱編輯 – akuma8