Java Spring 2.19使用自定义身份验证登录失败
我遵循一个教程,当我尝试使用foo作为用户名和密码登录时,我得到了无效的用户名和密码。实际上,我不认为需要输入foo值,但我遵循教程Java Spring 2.19使用自定义身份验证登录失败,java,spring-boot,java-security,Java,Spring Boot,Java Security,我遵循一个教程,当我尝试使用foo作为用户名和密码登录时,我得到了无效的用户名和密码。实际上,我不认为需要输入foo值,但我遵循教程 package com.dac.imoveis.controllers; import com.dac.imoveis.controllers.MyUserDetailsService; import org.springframework.beans.factory.annotation.Autowired; import org.springframewor
package com.dac.imoveis.controllers;
import com.dac.imoveis.controllers.MyUserDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
@EnableWebSecurity
public class SecurityConfigurer extends WebSecurityConfigurerAdapter {
@Autowired
private MyUserDetailsService myUserDetailsService;
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(myUserDetailsService);
}
@Bean
public PasswordEncoder passwordEncoder(){
return NoOpPasswordEncoder.getInstance();
}
}
用户详细信息服务
package com.dac.imoveis.controllers;
import java.util.ArrayList;
import org.springframework.context.annotation.Bean;
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 org.springframework.security.crypto.password.NoOpPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
@Service
class MyUserDetailsService implements UserDetailsService {
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
return new User("foo", "foo", new ArrayList<>());
}
}
我正在学习一个教程,那是哪个教程?因为我非常确定您的SecurityConfigure类缺少@Configuration注释,没有它,Spring框架将忽略该类。如果您正在使用的教程也缺少注释,那么您应该停止使用有缺陷的教程。否则,如果您遵循某个教程,请确保正确地遵循该教程。本教程我添加了@Configuration,但结果相同。由于我们看不到您的其余代码,我们只能想知道您还犯了什么错误,使其无法工作。我将添加更多代码。
@SpringBootApplication
@ComponentScan(basePackages={"com.dac.imoveis.controllers"})
public class ImoveisApplication {
public static void main(String[] args) {
SpringApplication.run(ImoveisApplication.class, args);
}
}