仅使用JDBC注释的Spring安全性

仅使用JDBC注释的Spring安全性,spring,security,model-view-controller,jdbc,spring-boot,Spring,Security,Model View Controller,Jdbc,Spring Boot,我正在尝试使用Spring安全性创建一个Spring引导应用程序。我从中选取了以下示例。它们使用“内存中身份验证”。我也希望这样做,但是使用JDBC身份验证 这是我的网站安全配置课程: package hello; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.spring

我正在尝试使用Spring安全性创建一个Spring引导应用程序。我从中选取了以下示例。它们使用“内存中身份验证”。我也希望这样做,但是使用JDBC身份验证

这是我的网站安全配置课程:

package hello;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
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.security.config.annotation.web.servlet.configuration.EnableWebMvcSecurity;

@Configuration
@EnableWebMvcSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
                .antMatchers("/", "/home").permitAll()
                .anyRequest().authenticated()
                .and()
            .formLogin()
                .loginPage("/login")
                .permitAll()
                .and()
            .logout()
                .permitAll();
    }

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {

        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://localhost:3306/test");
        dataSource.setUsername("root");
        dataSource.setPassword("root");

        auth
            .jdbcAuthentication().dataSource(dataSource)
                .usersByUsernameQuery("select * from users where username=?")
                .authoritiesByUsernameQuery("select * from user_roles where username=?");
    }
}
我还设置了一个MySQL数据库“test”和两个表“users”和“user\u roles”。 当我运行应用程序并尝试登录时,我没有得到任何异常,但应用程序不知何故找不到用户

有人能帮我吗?我做错了什么

登录页面:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
    <head>
        <title>Spring Security Example </title>
    </head>
    <body>
        <div th:if="${param.error}">
            Invalid username and password.
        </div>
        <div th:if="${param.logout}">
            You have been logged out.
        </div>
        <form th:action="@{/login}" method="post">
            <div><label> User Name : <input type="text" name="username"/> </label></div>
            <div><label> Password: <input type="password" name="password"/> </label></div>
            <div><input type="submit" value="Sign In"/></div>
        </form>
    </body>
</html>

Spring安全示例
无效的用户名和密码。
您已注销。
用户名:
密码:
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>org.springframework</groupId>
    <artifactId>gs-securing-web</artifactId>
    <version>0.1.0</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.2.2.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <!-- tag::security[] -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <!-- end::security[] -->

        <!-- JDBC -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>3.2.8.RELEASE</version>
        </dependency>

        <!-- MySQL -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
    </dependencies>


    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

4.0.0
org.springframework
gs安全网
0.1.0
org.springframework.boot
spring启动程序父级
1.2.2.1发布
org.springframework.boot
弹簧启动装置
org.springframework.boot
弹簧启动安全
org.springframework
SpringJDBC
3.2.8.1发布
mysql
mysql连接器java
org.springframework.boot
springbootmaven插件

建议将数据源作为一个单独的Bean来解决,而不是在configureGlobal方法中实例化它:

@Bean
public DataSource dataSource() {
    // create and return a new JDBC DataSource ...
}
auth.jdbcAuthentication().dataSource(dataSource)
您可以像在configureGlobal方法中一样使用Bean:

@Bean
public DataSource dataSource() {
    // create and return a new JDBC DataSource ...
}
auth.jdbcAuthentication().dataSource(dataSource)

您的用户必须至少分配一个角色。如果用户没有角色,则身份验证失败(即使您希望能够在没有任何特定角色的情况下登录)。还要确保用户的
enabled
标志为true。

Stacktrace(例如日志)之外的内容是什么?对于初学者来说,您的查询是错误的,用户的查询必须返回3列(用户名、密码和enabled),权限2的查询(用户名、角色)。现在它返回你放在那里的任何东西。另外,为什么您的依赖项中有SpringJDBC版本3.2.8?现在混合使用spring版本,只需添加
spring boot starter jdbc
即可获得所需的依赖项(或者至少删除版本标记)。