获得"糟糕的SQL语法"尝试使用Spring Security登录时出错

时间:2018-05-23 16:33:19

标签: mysql spring spring-boot jdbc spring-security

我正在尝试构建一个简单的Spring Boot CRUD应用程序,该应用程序还具有具有spring boot安全性的登录和注册选项。我已经在使用MySQL数据库了,它可以正常工作以保存我的应用程序的数据。我也有注册工作。

我的Spring Security登录实现存在一些问题。

org.springframework.security.authentication.InternalAuthenticationServiceException:PreparedStatementCallback;错误的SQL语法[选择用户名,来自权限的权限,其中username =?];嵌套异常是com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:表' cheesedb.authorities'不存在

显然,我没有权威课程。我尝试使用电子邮件和密码进行授权(一旦我开始工作,我就会加密)。我有两个主要的模型/类,Cheese和Customer,但对于这个问题,我认为Customer是唯一相关的模型。

Test2Application.java

package com.example.demo;

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

@SpringBootApplication
public class Test2Application {

public static void main(String[] args) {
    SpringApplication.run(Test2Application.class, args);
}
}

SecurityConfig.java

package com.example.demo;

import org.springframework.beans.factory.annotation.Autowired;
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.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

import javax.sql.DataSource;

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    DataSource dataSource;

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.jdbcAuthentication().dataSource(dataSource)
                .usersByUsernameQuery("select email as principal, password as credentials, true from customer where email=?");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception{
        http
                .csrf().disable()
                .authorizeRequests()
                .antMatchers(
//                        "/cheese/index",
//                        "/cheese/",
                        "/**/webjars/**",
                        "/cheese/signup",
                        "/cheese/login",
//                        "/cheese/account",
//                        "/cheese/add",
//                        "/cheese/remove",
                        "/cheese/success").permitAll()
                .anyRequest().authenticated()
                .and()
                .formLogin().loginPage("/cheese/login")
                .defaultSuccessUrl("/cheese/account")
                .permitAll();
    }
}

客户

package com.example.demo.models;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.validation.constraints.Email;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

@Entity
public class Customer {

    @NotNull
    @Size(min=2, max=25)
    private String name;

    @GeneratedValue
    @Id
    private int accountNumber;

    @NotNull
    @Size(min=2, max=25)
    private String password;

    @NotNull
    @Size(min=2, max=25)
    @Email
    private String email;

    public Customer(String name, String password, String email) {
        this.name = name;
        this.password = password;
        this.email = email;
    }

    public Customer() {}

    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAccountNumber() {
        return accountNumber;
    }
}

UserController中

package com.example.demo.controllers;

import com.example.demo.models.Customer;
import com.example.demo.models.data.CustomerDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("cheese")
public class UserController {

    @Autowired
    private CustomerDao customerDao;

    @RequestMapping(value = "login")
    public String loginPage(Model model) {
        model.addAttribute("title", "Login Page");
        return "cheese/login";
    }

    @RequestMapping(value = "account")
    public String accountInfo(Model model) {
        model.addAttribute("title", "Account Page");
        return "cheese/account";
    }

    @GetMapping("signup")
    public String displaySignUpForm(Model model) {
        model.addAttribute("title", "Sign Up");
        model.addAttribute("customer", new Customer());
        return "cheese/signup";
    }

    @PostMapping(value = "signup")
    public String processSignUp(Model model, @ModelAttribute Customer customer, Errors errors) {

        if (errors.hasErrors()) {
            return "cheese/signup";
        }

        customerDao.save(customer);
        return "cheese/success";
    }
}

的login.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org"
      xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
      layout:decorate="~{fragments/main_layout}">

<head th:replace="fragments :: head"></head>
<body>

<nav th:replace="fragments :: navigation"></nav>

<div th:if="${param.error}">
    Invalid username and password.
</div>

<div th:if="${param.logout}">
    You have been logged out.
</div>

<div class="mx-auto" style="width: 400px;">

    <br>

    <h1 th:text="${title}">Log in</h1>

    <br>
    <br>


    <form th:action="@{/cheese/login}" method="post">

        <div class="form-group">
            <label for="email" class="form-control-label">Email</label> <input
                type="text" class="form-control"  id="email" name="username" />

        </div>
        <div class="form-group">
            <label for="password" class="form-control-label">Password</label> <input
                type="password" class="form-control"
                id="password"  name="password"/>
        </div>

    <button type="submit" class="btn btn-primary">Submit</button>

</form>

</div>

</body>
</html>

的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.example</groupId>
    <artifactId>test2</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>test2</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </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-security</artifactId>
        </dependency>

        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>bootstrap</artifactId>
        </dependency>

        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>jquery</artifactId>
        </dependency>

        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>webjars-locator</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <addResources>true</addResources>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

0 个答案:

没有答案