如何解决java.lang.ClassNotFoundException:org.springframework.security.core.context.SecurityContextHolder

时间:2018-07-26 12:33:03

标签: java spring spring-boot

我是Spring Boot的新手。我正在使用spring boot 1.5.1springSecurityVersion 3.2.3org.thymeleaf.extras:thymeleaf-extras-springsecurity4版本,但是却遇到了异常

java.lang.ClassNotFoundException: org.springframework.security.core.context.SecurityContextHolder
at java.net.URLClassLoader.findClass(URLClassLoader.java:381) ~[na:1.8.0_121]
at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[na:1.8.0_121]
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331) ~[na:1.8.0_121]
at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[na:1.8.0_121]
at org.thymeleaf.extras.springsecurity4.auth.AuthUtils.getAuthenticationObject(AuthUtils.java:95) ~[thymeleaf-extras-springsecurity4-2.1.2.RELEASE.jar:2.1.2.RELEASE]
at org.thymeleaf.extras.springsecurity4.dialect.SpringSecurityDialect.getAdditionalExpressionObjects(SpringSecurityDialect.java:117) ~[thymeleaf-extras-springsecurity4-2.1.2.RELEASE.jar:2.1.2.RELEASE]
at org.thymeleaf.context.AbstractDialectAwareProcessingContext.computeExpressionObjects(AbstractDialectAwareProcessingContext.java:113) ~[thymeleaf-2.1.4.RELEASE.jar:2.1.4.RELEASE]
at org.thymeleaf.context.AbstractProcessingContext.getExpressionObjects(AbstractProcessingContext.java:132) ~[thymeleaf-2.1.4.RELEASE.jar:2.1.4.RELEASE]
at org.thymeleaf.spring4.expression.SpelVariableExpressionEvaluator.computeExpressionObjects(SpelVariableExpressionEvaluator.java:197) ~[thymeleaf-spring4-2.1.4.RELEASE.jar:2.1.4.RELEASE]
at org.thymeleaf.spring4.expression.SpelVariableExpressionEvaluator.evaluate(SpelVariableExpressionEvaluator.java:115) ~[thymeleaf-spring4-2.1.4.RELEASE.jar:2.1.4.RELEASE]
at org.thymeleaf.standard.expression.VariableExpression.executeVariable(VariableExpression.java:154) ~[thymeleaf-2.1.4.RELEASE.jar:2.1.4.RELEASE]
at org.thymeleaf.standard.expression.SimpleExpression.executeSimple(SimpleExpression.java:59) ~[thymeleaf-2.1.4.RELEASE.jar:2.1.4.RELEASE

我的配置有什么错误?

1 个答案:

答案 0 :(得分:0)

如问题中所述,您正在使用Spring Security 3.2.3,但也使用thymeleaf-extras-springsecurity4依赖项,该依赖项仅适用于Spring Security 4.x。

由于这种不兼容,Thymeleaf依赖项无法找到某些类,这就是为什么它抛出ClassNotFoundException的原因。

解决方案是使用以下任一兼容的库:

  1. 将Spring Security版本升级到 v4.x 。通常,默认情况下,Spring Boot 1.3及更高版本带有Spring Security4.x。因此,从理论上讲,除非您的某个地方存在依赖性冲突,否则您应该能够使用Spring Security 4。如果没有手动执行此操作,请检查依赖关系树的根本原因,以了解为什么使用Spring Security 3.2.3。
  2. 将Thymeleaf依赖项降级为thymeleaf-extras-springsecurity3,例如,在使用Maven时:

    <dependency>
        <groupId>org.thymeleaf.extras</groupId>
        <artifactId>thymeleaf-extras-springsecurity3</artifactId>
    </dependency>