我在个人项目中遇到了SpringBoot的问题,我正在Visual Studio Code中进行,以便能够学习语言,错误是在实现安全部分时,由于某种原因,此错误阻止了应用程序的部署,因此我无法看到登录,该错误发生在SecurityConfiguration中。出现的错误是这样的:
Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled.
08:24 ERROR 18740 --- [ restartedMain] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
Field usuarioServicio in com.example.demo4.security.SecurityConfiguration required a bean of type 'com.example.demo4.service.UsuarioServiceImpl' that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
字符串
我的类SecurityConfiguration:
@Configuration
@EnableWebSecurity
public class SecurityConfiguration {
@Autowired
private UsuarioServiceImpl usuarioServicio;
PasswordEncoder passwordEncoder;
AuthenticationManager authenticationManager;
@Bean
public SecurityFilterChain filterChain (HttpSecurity http) throws Exception{
AuthenticationManagerBuilder builder =http.getSharedObject(AuthenticationManagerBuilder.class);
builder.userDetailsService(usuarioServicio).passwordEncoder(passwordEncoder);
authenticationManager = builder.build();
http.authenticationManager(authenticationManager);
http.csrf(csrf -> csrf.disable());
http.cors(Customizer.withDefaults());
http.sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS));
http.authorizeHttpRequests(auth -> auth.requestMatchers("/registro", "/js/","/css/", "/img/")
.permitAll()
.anyRequest()
.authenticated());
//http.exceptionHandling(exc -> exc.authenticationEntryPoint(null))
return http.build();
}
@Bean
public BCryptPasswordEncoder passwordEncoder(){
return new BCryptPasswordEncoder();
}
@Bean
public DaoAuthenticationProvider authenticationProvider(){
DaoAuthenticationProvider auth = new DaoAuthenticationProvider();
auth.setUserDetailsService(usuarioServicio);
auth.setPasswordEncoder(passwordEncoder());
return auth;
}
}
型
我的班级:
@Service
public class UsuarioServiceImpl implements UsuarioService {
@Autowired
private BCryptPasswordEncoder passwordEncoder;
@Autowired
private UsuarioDao usuarioDao;
@Override
public Usuario guardar(UsuarioRegistroDTO registroDTO) {
Usuario usuario = new Usuario(registroDTO.getNombre(),
registroDTO.getApellido(), registroDTO.getEmail(),
passwordEncoder.encode(registroDTO.getPassword()),
Arrays.asList(new Rol("ROLE_USER")));
return usuarioDao.save(usuario);
}
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
Usuario usuario = usuarioDao.findByEmail(username);
if(usuario == null) {
throw new UsernameNotFoundException("Usuario o password invalidos");
}
return new User(usuario.getEmail(), usuario.getPassword(), mapearAutoridadesRoles(usuario.getRoles()));
}
private Collection<? extends GrantedAuthority> mapearAutoridadesRoles(Collection<Rol> roles) {
return roles.stream().map(role -> new SimpleGrantedAuthority(role.getNombre())).collect(Collectors.toList());
}
}
型
我的类目录服务:
public interface UsuarioService extends UserDetailsService{
public Usuario guardar(UsuarioRegistroDTO registroDTO);
}
型
My class注册表控制器:
@Controller
@RequestMapping("/registro")
public class RegistroUsuarioControlador {
@Autowired
private UsuarioServiceImpl usuarioServicio;
@ModelAttribute("usuario")
public UsuarioRegistroDTO retornarNuevoUsuarioRegistroDTO() {
return new UsuarioRegistroDTO();
}
@GetMapping
public String mostrarFormularioDeRegistro(){
return "registro";
}
@PostMapping
public String registrarCuentaDeUsuario(@ModelAttribute("usuario") UsuarioRegistroDTO registroDTO){
usuarioServicio.guardar(registroDTO);
return "redirect:/registro?exito";
}
}
型
我的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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo4</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo4</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jersey</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<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>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity6</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</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>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.9.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.security/spring-security-rsa -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-rsa</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
</plugins>
</build>
</project>
型
我的Demo 4应用程序:
package com.example.demo4;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Demo4Application {
public static void main(String[] args) {
SpringApplication.run(Demo4Application.class, args);
}
}
型
aplication.properties:
server.port=8082
logging.pattern.dateformat=hh:mm
spring.main.banner-mode=off
spring.thymeleaf.cache=false
#Mysql conexion
spring.datasource.url=jdbc:mysql://localhost/dbd?useSSL=false&serverTimeZone=UTC&allowPublicKeyRetrieval=true
spring.datasource.username=root
spring.datasource.password=pswd
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.database-platform=org.hibernate.dialect.MySQL8Dialect
#Mostrar SQL
spring.jpa.properties.hibernate.forma_sql=true
logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type-descriptor.sql.BasicBinder=TRACE
#crear bdd
spring.jpa.hibernate.ddl-auto= create
# security
spring.security.user.name=usuario
spring.security.user.password=psw
型
我一直试图纠正它与一些类似的问题的答案在这里,但例如,通过把类与标签@Autowired(required=true)在类型UserServiceImpl的变量userService我一直无法修复它,事实上,我的错误也复制在RegistryUserController. Java。
我也看到,它很可能是文件夹分布没有做好,但我不知道会是什么错在我的情况下.如果你需要更多的信息或我为您提供更多的代码,我可以附上它.
我从这里开始学习教程,尽管我更新了它,因为许多库已经过时了(教程是西班牙语的):https://www.youtube.com/watch?v=0wTsLRxS3gA
有人能帮我解决这个错误吗?
先谢了。
2条答案
按热度按时间zpf6vheq1#
字符串
在Sping Boot 应用程序中,依赖注入由IOC容器执行。
要将Java类注解为Spring been,需要使用适当的注解来注解接口或类。
qnzebej02#
也许你应该列出类所在的文件夹的路径,我的意思是arioServiceImpl,它可能没有被spring扫描过。
另外,一门好的课程可以大大提高学习的效率,也许你应该花更多的时间去寻找一门好的课程,而不是花更多的时间去学习一门相对较老的课程。这只是我个人的建议,祝你学习顺利。