spring Sping Boot 错误:启动ApplicationContext时出错

mzillmmw  于 2024-01-05  发布在  Spring
关注(0)|答案(2)|浏览(308)

我在个人项目中遇到了SpringBoot的问题,我正在Visual Studio Code中进行,以便能够学习语言,错误是在实现安全部分时,由于某种原因,此错误阻止了应用程序的部署,因此我无法看到登录,该错误发生在SecurityConfiguration中。出现的错误是这样的:

  1. Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled.
  2. 08:24 ERROR 18740 --- [ restartedMain] o.s.b.d.LoggingFailureAnalysisReporter :
  3. ***************************
  4. APPLICATION FAILED TO START
  5. ***************************
  6. Description:
  7. Field usuarioServicio in com.example.demo4.security.SecurityConfiguration required a bean of type 'com.example.demo4.service.UsuarioServiceImpl' that could not be found.
  8. The injection point has the following annotations:
  9. - @org.springframework.beans.factory.annotation.Autowired(required=true)

字符串
我的类SecurityConfiguration:

  1. @Configuration
  2. @EnableWebSecurity
  3. public class SecurityConfiguration {
  4. @Autowired
  5. private UsuarioServiceImpl usuarioServicio;
  6. PasswordEncoder passwordEncoder;
  7. AuthenticationManager authenticationManager;
  8. @Bean
  9. public SecurityFilterChain filterChain (HttpSecurity http) throws Exception{
  10. AuthenticationManagerBuilder builder =http.getSharedObject(AuthenticationManagerBuilder.class);
  11. builder.userDetailsService(usuarioServicio).passwordEncoder(passwordEncoder);
  12. authenticationManager = builder.build();
  13. http.authenticationManager(authenticationManager);
  14. http.csrf(csrf -> csrf.disable());
  15. http.cors(Customizer.withDefaults());
  16. http.sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS));
  17. http.authorizeHttpRequests(auth -> auth.requestMatchers("/registro", "/js/","/css/", "/img/")
  18. .permitAll()
  19. .anyRequest()
  20. .authenticated());
  21. //http.exceptionHandling(exc -> exc.authenticationEntryPoint(null))
  22. return http.build();
  23. }
  24. @Bean
  25. public BCryptPasswordEncoder passwordEncoder(){
  26. return new BCryptPasswordEncoder();
  27. }
  28. @Bean
  29. public DaoAuthenticationProvider authenticationProvider(){
  30. DaoAuthenticationProvider auth = new DaoAuthenticationProvider();
  31. auth.setUserDetailsService(usuarioServicio);
  32. auth.setPasswordEncoder(passwordEncoder());
  33. return auth;
  34. }
  35. }


我的班级:

  1. @Service
  2. public class UsuarioServiceImpl implements UsuarioService {
  3. @Autowired
  4. private BCryptPasswordEncoder passwordEncoder;
  5. @Autowired
  6. private UsuarioDao usuarioDao;
  7. @Override
  8. public Usuario guardar(UsuarioRegistroDTO registroDTO) {
  9. Usuario usuario = new Usuario(registroDTO.getNombre(),
  10. registroDTO.getApellido(), registroDTO.getEmail(),
  11. passwordEncoder.encode(registroDTO.getPassword()),
  12. Arrays.asList(new Rol("ROLE_USER")));
  13. return usuarioDao.save(usuario);
  14. }
  15. @Override
  16. public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
  17. Usuario usuario = usuarioDao.findByEmail(username);
  18. if(usuario == null) {
  19. throw new UsernameNotFoundException("Usuario o password invalidos");
  20. }
  21. return new User(usuario.getEmail(), usuario.getPassword(), mapearAutoridadesRoles(usuario.getRoles()));
  22. }
  23. private Collection<? extends GrantedAuthority> mapearAutoridadesRoles(Collection<Rol> roles) {
  24. return roles.stream().map(role -> new SimpleGrantedAuthority(role.getNombre())).collect(Collectors.toList());
  25. }
  26. }


我的类目录服务:

  1. public interface UsuarioService extends UserDetailsService{
  2. public Usuario guardar(UsuarioRegistroDTO registroDTO);
  3. }


My class注册表控制器:

  1. @Controller
  2. @RequestMapping("/registro")
  3. public class RegistroUsuarioControlador {
  4. @Autowired
  5. private UsuarioServiceImpl usuarioServicio;
  6. @ModelAttribute("usuario")
  7. public UsuarioRegistroDTO retornarNuevoUsuarioRegistroDTO() {
  8. return new UsuarioRegistroDTO();
  9. }
  10. @GetMapping
  11. public String mostrarFormularioDeRegistro(){
  12. return "registro";
  13. }
  14. @PostMapping
  15. public String registrarCuentaDeUsuario(@ModelAttribute("usuario") UsuarioRegistroDTO registroDTO){
  16. usuarioServicio.guardar(registroDTO);
  17. return "redirect:/registro?exito";
  18. }
  19. }


我的pom.xml:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>3.2.0</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.example</groupId>
  12. <artifactId>demo4</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>demo4</name>
  15. <description>Demo project for Spring Boot</description>
  16. <properties>
  17. <java.version>17</java.version>
  18. </properties>
  19. <dependencies>
  20. <dependency>
  21. <groupId>org.springframework.boot</groupId>
  22. <artifactId>spring-boot-starter-data-jpa</artifactId>
  23. </dependency>
  24. <dependency>
  25. <groupId>org.springframework.boot</groupId>
  26. <artifactId>spring-boot-starter-data-rest</artifactId>
  27. </dependency>
  28. <dependency>
  29. <groupId>org.springframework.boot</groupId>
  30. <artifactId>spring-boot-starter-jersey</artifactId>
  31. </dependency>
  32. <dependency>
  33. <groupId>org.springframework.boot</groupId>
  34. <artifactId>spring-boot-starter-security</artifactId>
  35. </dependency>
  36. <dependency>
  37. <groupId>org.springframework.boot</groupId>
  38. <artifactId>spring-boot-starter-thymeleaf</artifactId>
  39. </dependency>
  40. <dependency>
  41. <groupId>org.springframework.boot</groupId>
  42. <artifactId>spring-boot-starter-web</artifactId>
  43. </dependency>
  44. <dependency>
  45. <groupId>org.springframework.boot</groupId>
  46. <artifactId>spring-boot-starter-web-services</artifactId>
  47. </dependency>
  48. <dependency>
  49. <groupId>org.springframework.boot</groupId>
  50. <artifactId>spring-boot-starter-websocket</artifactId>
  51. </dependency>
  52. <dependency>
  53. <groupId>org.thymeleaf.extras</groupId>
  54. <artifactId>thymeleaf-extras-springsecurity6</artifactId>
  55. </dependency>
  56. <dependency>
  57. <groupId>org.springframework.boot</groupId>
  58. <artifactId>spring-boot-devtools</artifactId>
  59. <scope>runtime</scope>
  60. <optional>true</optional>
  61. </dependency>
  62. <dependency>
  63. <groupId>com.mysql</groupId>
  64. <artifactId>mysql-connector-j</artifactId>
  65. <scope>runtime</scope>
  66. </dependency>
  67. <dependency>
  68. <groupId>org.projectlombok</groupId>
  69. <artifactId>lombok</artifactId>
  70. <optional>true</optional>
  71. </dependency>
  72. <dependency>
  73. <groupId>org.springframework.boot</groupId>
  74. <artifactId>spring-boot-starter-test</artifactId>
  75. <scope>test</scope>
  76. </dependency>
  77. <dependency>
  78. <groupId>org.springframework.security</groupId>
  79. <artifactId>spring-security-test</artifactId>
  80. <scope>test</scope>
  81. </dependency>
  82. <dependency>
  83. <groupId>io.jsonwebtoken</groupId>
  84. <artifactId>jjwt</artifactId>
  85. <version>0.9.1</version>
  86. </dependency>
  87. <!-- https://mvnrepository.com/artifact/org.springframework.security/spring-security-rsa -->
  88. <dependency>
  89. <groupId>org.springframework.security</groupId>
  90. <artifactId>spring-security-rsa</artifactId>
  91. <version>1.1.1</version>
  92. </dependency>
  93. </dependencies>
  94. <build>
  95. <plugins>
  96. <plugin>
  97. <groupId>org.springframework.boot</groupId>
  98. <artifactId>spring-boot-maven-plugin</artifactId>
  99. <configuration>
  100. <excludes>
  101. <exclude>
  102. <groupId>org.projectlombok</groupId>
  103. <artifactId>lombok</artifactId>
  104. </exclude>
  105. </excludes>
  106. </configuration>
  107. </plugin>
  108. <plugin>
  109. <artifactId>maven-clean-plugin</artifactId>
  110. <version>3.1.0</version>
  111. </plugin>
  112. </plugins>
  113. </build>
  114. </project>


我的Demo 4应用程序:

  1. package com.example.demo4;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. @SpringBootApplication
  5. public class Demo4Application {
  6. public static void main(String[] args) {
  7. SpringApplication.run(Demo4Application.class, args);
  8. }
  9. }


aplication.properties:

  1. server.port=8082
  2. logging.pattern.dateformat=hh:mm
  3. spring.main.banner-mode=off
  4. spring.thymeleaf.cache=false
  5. #Mysql conexion
  6. spring.datasource.url=jdbc:mysql://localhost/dbd?useSSL=false&serverTimeZone=UTC&allowPublicKeyRetrieval=true
  7. spring.datasource.username=root
  8. spring.datasource.password=pswd
  9. spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
  10. spring.jpa.database-platform=org.hibernate.dialect.MySQL8Dialect
  11. #Mostrar SQL
  12. spring.jpa.properties.hibernate.forma_sql=true
  13. logging.level.org.hibernate.SQL=DEBUG
  14. logging.level.org.hibernate.type-descriptor.sql.BasicBinder=TRACE
  15. #crear bdd
  16. spring.jpa.hibernate.ddl-auto= create
  17. # security
  18. spring.security.user.name=usuario
  19. spring.security.user.password=psw


我一直试图纠正它与一些类似的问题的答案在这里,但例如,通过把类与标签@Autowired(required=true)在类型UserServiceImpl的变量userService我一直无法修复它,事实上,我的错误也复制在RegistryUserController. Java。
我也看到,它很可能是文件夹分布没有做好,但我不知道会是什么错在我的情况下.如果你需要更多的信息或我为您提供更多的代码,我可以附上它.
我从这里开始学习教程,尽管我更新了它,因为许多库已经过时了(教程是西班牙语的):https://www.youtube.com/watch?v=0wTsLRxS3gA

有人能帮我解决这个错误吗?

先谢了。

zpf6vheq

zpf6vheq1#

  1. @Service
  2. public interface UsuarioService extends UserDetailsService{
  3. public Usuario guardar(UsuarioRegistroDTO registroDTO);
  4. }

字符串
在Sping Boot 应用程序中,依赖注入由IOC容器执行。
要将Java类注解为Spring been,需要使用适当的注解来注解接口或类。

qnzebej0

qnzebej02#

也许你应该列出类所在的文件夹的路径,我的意思是arioServiceImpl,它可能没有被spring扫描过。
另外,一门好的课程可以大大提高学习的效率,也许你应该花更多的时间去寻找一门好的课程,而不是花更多的时间去学习一门相对较老的课程。这只是我个人的建议,祝你学习顺利。

相关问题