2017-08-16 89 views
0

我一直在为Spring数据库身份验证应用程序开发Spring引导程序。在本地主机上安装MySQL数据库。但问题是我甚至无法运行我的春季应用程序。我在Windows操作系统上使用Eclipse和Maven。我创建的文件和错误如下:Spring Boot jdbcAuthentication dataSource未加载错误

WebSecurityConfig.java:

import org.springframework.context.annotation.Configuration; 
import org.springframework.beans.factory.annotation.Autowired; 
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 javax.sql.DataSource; 
@Configuration 
public class WebSecurityConfig extends WebSecurityConfigurerAdapter { 

    @Autowired 
    private DataSource dxataSource; 

    @Autowired 
    public void configure(AuthenticationManagerBuilder auth) throws Exception { 
     auth.jdbcAuthentication().dataSource((DataSource) dxataSource).usersByUsernameQuery("select username,password, enabled from users where username=?"); 
    } 

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
    http.authorizeRequests().anyRequest().fullyAuthenticated().and().formLogin(); 
    } 
} 

Application.java:

import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 

@SpringBootApplication 
public class Application { 

    public static void main(String[] args) { 

     SpringApplication.run(Application.class, args); 
    } 

} 

HomeComtoller.java:

import org.springframework.web.bind.annotation.GetMapping; 
import org.springframework.web.bind.annotation.RestController; 

    @RestController 
    public class HomeController { 

    @GetMapping("/") 
    public String index() { 
     return "OK"; 
    } 
} 

我的pom.xml file:

<?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>org.springframework</groupId> 
    <artifactId>gs-authenticating-ldap</artifactId> 
    <version>0.1.0</version> 
    <parent> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-parent</artifactId> 
     <version>1.5.6.RELEASE</version> 
    </parent> 
    <properties> 
     <java.version>1.8</java.version> 
    </properties>  
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-boot-starter-jdbc</artifactId> 
     </dependency> 
    <!-- tag::security[] --> 
    <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-data-jpa</artifactId> 
     </dependency>   
    <dependencies> 
     <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>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-security</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.ldap</groupId> 
     <artifactId>spring-ldap-core</artifactId> 
     </dependency> 
    <dependency> 
     <groupId>org.springframework.security</groupId> 
     <artifactId>spring-security-ldap</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>com.unboundid</groupId> 
     <artifactId>unboundid-ldapsdk</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-test</artifactId> 
     <scope>test</scope> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.security</groupId> 
     <artifactId>spring-security-test</artifactId> 
     <scope>test</scope> 
    </dependency> 
    </dependencies> 
    <!-- end::security[] --> 
    <build> 
     <plugins> 
      <plugin> 
       <groupId>org.springframework.boot</groupId> 
       <artifactId>spring-boot-maven-plugin</artifactId> 
      </plugin> 
     </plugins> 
    </build> 
</project> 

我application.properties文件:

spring.datasource.url=jdbc:mysql://localhost:3306/testdb 
spring.datasource.username=test 
spring.datasource.password=password 

错误出现时我尝试运行应用程序(Eclipse的IDE):

Field dxataSource in auth.ldap.WebSecurityConfig required a bean of type 'javax.sql.DataSource' that could not be found. 
- Bean method 'dataSource' not loaded because @ConditionalOnClass did not find required class 'org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType' 
- Bean method 'dataSource' not loaded because @ConditionalOnClass did not find required classes 'javax.transaction.TransactionManager', 'org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType 

任何意见和建议将真正有用的。谢谢。

+0

也许失踪了? spring.datasource.driver-class-name = com.mysql.jdbc.Driver –

+0

你有没有看到https://stackoverflow.com/questions/41741135/spring-boot-auto-configuration-for-datasource –

回答

3

。在你的pom.xml file.You错误已经添加了两个标签之外的依赖关系,也忘了提版本的依赖called-“弹簧引导起动JDBC”。

尝试用下面的pom文件替换它。

<?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>org.springframework</groupId> 
    <artifactId>gs-authenticating-ldap</artifactId> 
    <version>0.1.0</version> 
    <parent> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-parent</artifactId> 
     <version>1.5.6.RELEASE</version> 
    </parent> 
    <properties> 
     <java.version>1.8</java.version> 
    </properties>  

    <dependencies> 
    <dependency> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-starter-jdbc</artifactId> 
    <version>1.5.2.RELEASE</version> 
</dependency> 
<dependency> 
    <groupId>mysql</groupId> 
    <artifactId>mysql-connector-java</artifactId> 
    <version>5.1.6</version> 
</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>  
    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-security</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.ldap</groupId> 
     <artifactId>spring-ldap-core</artifactId> 
     </dependency> 
    <dependency> 
     <groupId>org.springframework.security</groupId> 
     <artifactId>spring-security-ldap</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>com.unboundid</groupId> 
     <artifactId>unboundid-ldapsdk</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-test</artifactId> 
     <scope>test</scope> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.security</groupId> 
     <artifactId>spring-security-test</artifactId> 
     <scope>test</scope> 
    </dependency> 
    </dependencies> 
    <!-- end::security[] --> 
    <build> 
     <plugins> 
      <plugin> 
       <groupId>org.springframework.boot</groupId> 
       <artifactId>spring-boot-maven-plugin</artifactId> 
      </plugin> 
     </plugins> 
    </build> 
</project> 
+0

我改变了我的pom。 XML文件和它的工作,非常感谢你。 – user8469789

+0

upvote将不胜感激。 –

相关问题