3
respostas

erro ao tentar criar o usuário no banco e compilar a aplicação

Minha aplicação está com 2 problemas:

O primeiro, no trecho:

@Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();

    UserDetails user =
            User.builder()
                .username("joao")
                .password(encoder.encode("joão"))
                .roles("ADM")
                .build();

o Eclipse acusa erro no builder (do User.builder()), informando que o método builder is undefined for the type User....

O segundo erro: ao tentar compilar, mesmo após apagar todo o trecho: UserDetails user = User.builder() .username("joao") .password(encoder.encode("joão")) .roles("ADM") .build();

Com o eclipse sem apontar nenhum problema, ao tentar compilar a aplicação não sobe, apresentando no console a seguinte mensagem:

Consider defining a bean of type 'javax.activation.DataSource' in your configuration.

3 respostas

20:26:51.392 [Thread-0] DEBUG org.springframework.boot.devtools.restart.classloader.RestartClassLoader - Created RestartClassLoader org.springframework.boot.devtools.restart.classloader.RestartClassLoader@21d45ad7

. __ _ __ _ _ /\ / _'_ _ _ _()_ _ __ _ \ \ \ ( ( )___ | ' | '| | ' / ` | \ \ \ \/ ___)| |)| | | | | || (| | ) ) ) ) ' |__| .|| ||_| |_, | / / / / =========||==============|__/=//// :: Spring Boot :: (v2.6.7)

2022-05-10 20:26:52.029 INFO 14812 --- [ restartedMain] br.com.alura.mvc.mudi.MudiApplication : Starting MudiApplication using Java 18 on NTLTJNBSIDPS01 with PID 14812 (C:\Users\Usuário.DESKTOP-IEOL75E\eclipse-workspace\mudi\target\classes started by Usuário in C:\Users\Usuário.DESKTOP-IEOL75E\eclipse-workspace\mudi) 2022-05-10 20:26:52.032 INFO 14812 --- [ restartedMain] br.com.alura.mvc.mudi.MudiApplication : No active profile set, falling back to 1 default profile: "default" 2022-05-10 20:26:52.127 INFO 14812 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable 2022-05-10 20:26:52.127 INFO 14812 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG' 2022-05-10 20:26:53.420 INFO 14812 --- [ restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode. 2022-05-10 20:26:53.529 INFO 14812 --- [ restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 80 ms. Found 1 JPA repository interfaces. 2022-05-10 20:26:54.621 INFO 14812 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http) 2022-05-10 20:26:54.638 INFO 14812 --- [ restartedMain] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2022-05-10 20:26:54.639 INFO 14812 --- [ restartedMain] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.62] 2022-05-10 20:26:54.793 INFO 14812 --- [ restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2022-05-10 20:26:54.793 INFO 14812 --- [ restartedMain] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 2664 ms 2022-05-10 20:26:55.102 INFO 14812 --- [ restartedMain] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [name: default] 2022-05-10 20:26:55.178 INFO 14812 --- [ restartedMain] org.hibernate.Version : HHH000412: Hibernate ORM core version 5.6.8.Final 2022-05-10 20:26:55.408 INFO 14812 --- [ restartedMain] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.1.2.Final} 2022-05-10 20:26:55.538 INFO 14812 --- [ restartedMain] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting... 2022-05-10 20:26:55.733 INFO 14812 --- [ restartedMain] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed. 2022-05-10 20:26:55.781 INFO 14812 --- [ restartedMain] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.MySQL55Dialect 2022-05-10 20:26:56.573 INFO 14812 --- [ restartedMain] o.h.e.t.j.p.i.JtaPlatformInitiator : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform] 2022-05-10 20:26:56.585 INFO 14812 --- [ restartedMain] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'

2022-05-10 20:26:56.660 WARN 14812 --- [ restartedMain] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning 2022-05-10 20:26:56.679 WARN 14812 --- [ restartedMain] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'webSecurityConfig': Unsatisfied dependency expressed through field 'dataSource'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'javax.activation.DataSource' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} 2022-05-10 20:26:56.679 INFO 14812 --- [ restartedMain] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default' 2022-05-10 20:26:56.683 INFO 14812 --- [ restartedMain] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown initiated... 2022-05-10 20:26:56.693 INFO 14812 --- [ restartedMain] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown completed. 2022-05-10 20:26:56.696 INFO 14812 --- [ restartedMain] o.apache.catalina.core.StandardService : Stopping service [Tomcat] 2022-05-10 20:26:56.709 INFO 14812 --- [ restartedMain] ConditionEvaluationReportLoggingListener :

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled. 2022-05-10 20:26:56.740 ERROR 14812 --- [ restartedMain] o.s.b.d.LoggingFailureAnalysisReporter :


APPLICATION FAILED TO START


Description:

Field dataSource in br.com.alura.mvc.mudi.WebSecurityConfig required a bean of type 'javax.activation.DataSource' that could not be found.

The injection point has the following annotations:

- @org.springframework.beans.factory.annotation.Autowired(required=true)

Action:

Consider defining a bean of type 'javax.activation.DataSource' in your configuration.

Olá, Aarão!

No primeiro erro, o problema pode estar relacionado à importação do User. Verifique se você está importando o User correto. O correto seria: org.springframework.security.core.userdetails.User.

No segundo erro, pode ser que você esteja utilizando uma classe que depende de uma instância de DataSource, mas não definiu essa instância em sua configuração. Verifique se você possui a dependência do javax.activation.DataSource em seu projeto e se está definindo um bean desse tipo em sua classe de configuração.

Espero ter ajudado e bons estudos!