使用Spring項目並學習使用Spring Security。 該項目正在工作,但突然決定不。任何人都可以闡明爲什麼?Spring Security NoClassDefFoundError
WebInit.java
package com.catalyst.Config;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration.Dynamic;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
public class WebInit implements WebApplicationInitializer
{
@Override
public void onStartup(ServletContext servletContext) throws ServletException
{
Dynamic hServlet;
AnnotationConfigWebApplicationContext hAnnoCTX;
hAnnoCTX = new AnnotationConfigWebApplicationContext();
hAnnoCTX.register(WebMVCConfig.class);
hAnnoCTX.setServletContext(servletContext);
hServlet = servletContext.addServlet("dispatcher", new DispatcherServlet(hAnnoCTX));
hServlet.addMapping("/");
hServlet.setLoadOnStartup(1);
}
}
WebMVCConfig.java
package com.catalyst.Config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.JstlView;
import org.springframework.web.servlet.view.UrlBasedViewResolver;
@Configuration
@ComponentScan("com.catalyst")
@EnableWebMvc
public class WebMVCConfig extends WebMvcConfigurerAdapter
{
@Bean
public UrlBasedViewResolver setupViewResolver()
{
UrlBasedViewResolver hResolver;
hResolver = new UrlBasedViewResolver();
hResolver.setPrefix("/WEB-INF/JSP/");
hResolver.setSuffix(".jsp");
hResolver.setViewClass(JstlView.class);
return(hResolver);
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry hRegistry)
{
hRegistry.addResourceHandler("/Resources/**").addResourceLocations("/WEB-INF/Resources/*");
}
}
SpringSecurity.java
package com.catalyst.Config;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@Configuration
@EnableWebSecurity
public class SpringSecurity extends WebSecurityConfigurerAdapter
{
@Override
public void configure(WebSecurity web) throws Exception
{
web.ignoring().antMatchers("/", "/Error/**", "/Resources/**");
}
@Override
protected void configure(HttpSecurity http) throws Exception
{
http
.authorizeRequests()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/Login").and()
.httpBasic();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception
{
auth
.inMemoryAuthentication()
.withUser("user").password("password").roles("USER").and()
.withUser("admin").password("password").roles("USER", "ADMIN");
}
}
的pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.catalyst</groupId>
<artifactId>prjPersonal</artifactId>
<version>1.0_Personal</version>
<packaging>war</packaging>
<name>prjPersonal</name>
<properties>
<endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- Versions -->
<jstl.version>1.2</jstl.version>
<mysql.version>5.1.35</mysql.version>
<spring.version>4.0.1.RELEASE</spring.version>
<hibernate.version>5.0.0.CR1</hibernate.version>
</properties>
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
<!-- Spring -->
<dependency>
\t <groupId>org.springframework</groupId>
\t <artifactId>spring-core</artifactId>
\t <version>4.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
<version>4.0.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>4.0.1.RELEASE</version>
</dependency>
<!-- JSTL -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>${jstl.version}</version>
</dependency>
<!-- Hibernate -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency>
<!-- MySQL -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<compilerArguments>
<endorseddirs>${endorsed.dir}</endorseddirs>
</compilerArguments>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<outputDirectory>${endorsed.dir}</outputDirectory>
<silent>true</silent>
<artifactItems>
<artifactItem>
<groupId>javax</groupId>
<artifactId>javaee-endorsed-api</artifactId>
<version>7.0</version>
<type>jar</type>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Tomcat的錯誤
HTTP Status 500 - Servlet.init() for servlet dispatcher threw exception
root cause:
java.lang.NoClassDefFoundError: org/springframework/security/web/access/WebInvocationPrivilegeEvaluator
\t java.lang.Class.getDeclaredMethods0(Native Method)
\t java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
\t java.lang.Class.getDeclaredMethods(Class.java:1975)
\t org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:467)
\t org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:451)
\t org.springframework.util.ReflectionUtils.getUniqueDeclaredMethods(ReflectionUtils.java:512)
\t org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.getTypeForFactoryMethod(AbstractAutowireCapableBeanFactory.java:663)
\t org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.predictBeanType(AbstractAutowireCapableBeanFactory.java:593)
\t org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:1396)
\t org.springframework.beans.factory.support.DefaultListableBeanFactory.doGetBeanNamesForType(DefaultListableBeanFactory.java:382)
\t org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanNamesForType(DefaultListableBeanFactory.java:353)
\t org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeansOfType(DefaultListableBeanFactory.java:463)
\t org.springframework.context.support.AbstractApplicationContext.getBeansOfType(AbstractApplicationContext.java:1071)
\t org.springframework.beans.factory.BeanFactoryUtils.beansOfTypeIncludingAncestors(BeanFactoryUtils.java:277)
\t org.springframework.web.servlet.DispatcherServlet.initHandlerMappings(DispatcherServlet.java:549)
\t org.springframework.web.servlet.DispatcherServlet.initStrategies(DispatcherServlet.java:463)
\t org.springframework.web.servlet.DispatcherServlet.onRefresh(DispatcherServlet.java:452)
\t org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:550)
\t org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:484)
\t org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
\t javax.servlet.GenericServlet.init(GenericServlet.java:158)
\t org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
\t org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
\t org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:617)
\t org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
\t org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
\t org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:668)
\t org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2463)
\t org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2452)
\t java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
\t java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
\t org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
\t java.lang.Thread.run(Thread.java:745)
你檢查被行家下載,如果其中任何包含WebInvocationPrivilegeEvaluator的罐子? – Marged
謝謝!我用你的建議解決了它。 解決方案是添加spring-security-web –