Java SpringBoot UserDetailsService loadUserByUsername方法从未使用自定义WebSecurity配置适配器调用

Java SpringBoot UserDetailsService loadUserByUsername方法从未使用自定义WebSecurity配置适配器调用,java,spring-boot,userdetailsservice,Java,Spring Boot,Userdetailsservice,以下是本视频中的教程: 我的密码是7:30。无论出于何种原因,我似乎无法让securityconfig.java类识别名为myuserdetailssservice.java的自定义UserDetailsService类 预期的行为是,当我转到我在HelloResource.Java类中指定的/hello端点时,应该生成一个登录页面。我应该能够使用loadUserByUsername方法的硬编码返回用户名和密码为“foo”的用户值登录到网页 不管我做了什么更改,似乎总是没有调用loadUserB

以下是本视频中的教程:

我的密码是7:30。无论出于何种原因,我似乎无法让
securityconfig.java
类识别名为
myuserdetailssservice.java
的自定义UserDetailsService类

预期的行为是,当我转到我在
HelloResource.Java
类中指定的/hello端点时,应该生成一个登录页面。我应该能够使用
loadUserByUsername
方法的硬编码返回用户名和密码为“foo”的用户值登录到网页

不管我做了什么更改,似乎总是没有调用
loadUserByUsername
方法(我已经用断点/打印语句证明了这一点)。这让我相信可能有一些组件扫描问题,vut我还没有看到任何工作

我已经在以下链接中尝试了解决方案: 再加上一些。以下是我的相关档案:

JWTMain.java

package com.JWTTest;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@ComponentScan("{com.JWTTest}")
public class JwtMain {

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

package com.JWTTest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HelloResource {

    @RequestMapping("/hello")
    public String HelloString(){
        return "Hello";
    }

}
MyUserDetailsService.java

package com.JWTTest;

import org.jvnet.hk2.annotations.Service;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import java.util.ArrayList;

@Service
public class MyUserDetailsService implements UserDetailsService {

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        return new User("foo", "foo", true, true, true, true, new ArrayList<>());
    }
}
应用程序属性

spring.datasource.url=jdbc:mysql://localhost:3306/spring_security
spring.jpa.properties.hibernate.default_schema=users
spring.datasource.username=root
spring.datasource.password=password
spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.naming-strategy=org.hibernate.cfg.ImprovedNamingStrategy
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
格雷德尔先生

plugins {
    id 'org.springframework.boot' version '2.2.1.RELEASE'
    id 'io.spring.dependency-management' version '1.0.8.RELEASE'
    id 'java'
}

group = 'com.JWTTest'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'

repositories {
    mavenCentral()
}


dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-hateoas'
    implementation 'org.springframework.boot:spring-boot-starter-jersey'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.boot:spring-boot-starter-web-services'
    implementation 'org.springframework.boot:spring-boot-starter-webflux'
    implementation 'org.springframework.data:spring-data-rest-hal-browser'
    implementation 'org.springframework.session:spring-session-core'


    implementation 'org.springframework.boot:spring-boot-starter-security'

    compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa', version: '2.2.1.RELEASE'
    compile group: 'mysql', name: 'mysql-connector-java', version: '8.0.15'




    compileOnly 'org.projectlombok:lombok'

    compile("org.springframework.boot:spring-boot-starter-web")
    compile("org.springframework.boot:spring-boot-starter-thymeleaf")
    compile("org.springframework.boot:spring-boot-devtools")

    annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'
    annotationProcessor 'org.projectlombok:lombok'
    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }
    testImplementation 'io.projectreactor:reactor-test'

    implementation 'org.springframework.boot:spring-boot-starter-cache'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'

}
test {
    useJUnitPlatform()
}
除此之外,我对正在发生的事情一无所知。让我知道,谢谢

解决方案:

@SpringBootApplication
@ComponentScan 
应该是主要的方法。只要@EnableWebSecurity在SecurityConfigure类中,Springboot就知道有人试图覆盖Springboot提供的安全类。我的主要方法的问题是Springboot没有正确扫描我的类

我还了解到,通过将此注释置于主类之上,可以排除Springboot提供的安全类:

@SpringBootApplication(exclude = { SecurityAutoConfiguration.class })

MyUserDetailsService.java
中,您正在导入
org.jvnet.hk2.annotations.Service
,而正确的是
org.springframework.stereotype.Service

@SpringBootApplication(exclude = { SecurityAutoConfiguration.class })