我正在使用带有以下安全文件的数据库在spring boot中创建一个安全应用程序: package com.example;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.access.SecurityConfig;
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.configuration.WebSecurityConfigurerAdapter;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
@Configuration
public class SecureLogin extends WebSecurityConfigurerAdapter{
private DataSource dataSource;
@Override
public void configure(AuthenticationManagerBuilder auth)
throws Exception {
dataSource=(DataSource) DeleteitagainApplication.ctx.getBean("dataSource");
auth.jdbcAuthentication().dataSource(dataSource).
usersByUsernameQuery("select username,password, enabled from users where username=?").
authoritiesByUsernameQuery("select username, role from user_roles where username=?");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
.authorizeRequests()
.antMatchers("/css/**","/js/**","/view/**","/**").permitAll()
.anyRequest().authenticated()
.and()
.formLogin().permitAll()
.and()
.logout().permitAll();
}
}
我得到以下例外:
org.springframework.beans.factory.BeanCreationException:使用名称' springSecurityFilterChain'创建bean时出错。在类路径资源中定义[org / springframework / security / config / annotation / web / configuration / WebSecurityConfiguration.class]:通过工厂方法的Bean实例化失败;嵌套异常是org.springframework.beans.BeanInstantiationException:无法实例化[javax.servlet.Filter]:工厂方法' springSecurityFilterChain'抛出异常;嵌套异常是java.lang.NullPointerException 在org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:599)〜[spring-beans-4.2.7.RELEASE.jar:4.2.7.RELEASE] 在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1123)~ [spring-beans-4.2.7.RELEASE.jar:4.2.7.RELEASE] 在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1018)〜[spring-beans-4.2.7.RELEASE.jar:4.2.7.RELEASE] 在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510)〜[spring-beans-4.2.7.RELEASE.jar:4.2.7.RELEASE] 在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)〜[spring-beans-4.2.7.RELEASE.jar:4.2.7.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory $ 1.getObject(AbstractBeanFactory.java:306)〜[spring-beans-4.2.7.RELEASE.jar:4.2.7.RELEASE] 在org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)〜[spring-beans-4.2.7.RELEASE.jar:4.2.7.RELEASE] 在org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)〜[spring-beans-4.2.7.RELEASE.jar:4.2.7.RELEASE] 在org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)〜[spring-beans-4.2.7.RELEASE.jar:4.2.7.RELEASE] 在org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1060)〜[spring-context-4.2.7.RELEASE.jar:4.2.7.RELEASE] 在org.springframework.web.filter.DelegatingFilterProxy.initDelegate(DelegatingFilterProxy.java:326)〜[spring-web-4.2.7.RELEASE.jar:4.2.7.RELEASE] 在org.springframework.web.filter.DelegatingFilterProxy.initFilterBean(DelegatingFilterProxy.java:235)〜[spring-web-4.2.7.RELEASE.jar:4.2.7.RELEASE] 在org.springframework.web.filter.GenericFilterBean.init(GenericFilterBean.java:199)〜[spring-web-4.2.7.RELEASE.jar:4.2.7.RELEASE] 在org.apache.catalina.core.ApplicationFilterConfig.initFilter(ApplicationFilterConfig.java:279)〜[tomcat-embed-core-8.0.36.jar:8.0.36] 在org.apache.catalina.core.ApplicationFilterConfig。(ApplicationFilterConfig.java:109)〜[tomcat-embed-core-8.0.36.jar:8.0.36] at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4689)[tomcat-embed-core-8.0.36.jar:8.0.36] at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5329)[tomcat-embed-core-8.0.36.jar:8.0.36] 在org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147)[tomcat-embed-core-8.0.36.jar:8.0.36] 在org.apache.catalina.core.ContainerBase $ StartChild.call(ContainerBase.java:1407)[tomcat-embed-core-8.0.36.jar:8.0.36] 在org.apache.catalina.core.ContainerBase $ StartChild.call(ContainerBase.java:1397)[tomcat-embed-core-8.0.36.jar:8.0.36] at java.util.concurrent.FutureTask.run(Unknown Source)[na:1.8.0_91] at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)[na:1.8.0_91] at java.util.concurrent.ThreadPoolExecutor $ Worker.run(Unknown Source)[na:1.8.0_91] at java.lang.Thread.run(Unknown Source)[na:1.8.0_91] 引起:org.springframework.beans.BeanInstantiationException:无法实例化[javax.servlet.Filter]:工厂方法' springSecurityFilterChain'抛出异常;嵌套异常是java.lang.NullPointerException 在org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189)〜[spring-beans-4.2.7.RELEASE.jar:4.2.7.RELEASE] 在org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588)〜[spring-beans-4.2.7.RELEASE.jar:4.2.7.RELEASE] ...省略了23个常见帧 引起:java.lang.NullPointerException:null 在com.example.SecureLogin.configure(SecureLogin.java:22)〜[classes /:na] 在org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter.authenticationManager(WebSecurityConfigurerAdapter.java:234)〜[spring-security-config-4.0.4.RELEASE.jar:4.0.4.RELEASE]
我有下面的pom.xml
http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>deleteitagain</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</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>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-jpa -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>1.3.5.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<!/ P>