ralscha / blog

Source code for my blog
https://golb.hplar.ch/
MIT License
222 stars 246 forks source link

The code in blog -> jwt (Spring) doesn't work #6

Closed ghost closed 6 years ago

ghost commented 6 years ago

Hi !

Thanks you for sharing on web spring, ionic, ... with your blog !

I have a problem with https://golb.hplar.ch/p/JWT-Authentication-with-Ionic-2-and-Spring-Boot and I have downloaded the zip on github, created a new projet with folder jwt, opened in editor and compiled with JDK 1.8.0_131

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::  (v2.0.0.BUILD-SNAPSHOT)

2017-10-16 19:42:11.656  INFO 14146 --- [           main] main.java.ch.rasc.jwt.Application        : Starting Application on iMac-de-Stephane-Bressani.local with PID 14146 (/Users/stephanebressani/IdeaProjects/essai_ralscha_blog/target/classes started by stephanebressani in /Users/stephanebressani/IdeaProjects/essai_ralscha_blog)
2017-10-16 19:42:11.664  INFO 14146 --- [           main] main.java.ch.rasc.jwt.Application        : No active profile set, falling back to default profiles: default
2017-10-16 19:42:11.773  INFO 14146 --- [           main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@4562e04d: startup date [Mon Oct 16 19:42:11 PDT 2017]; root of context hierarchy
2017-10-16 19:42:15.076  INFO 14146 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2017-10-16 19:42:15.106  INFO 14146 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2017-10-16 19:42:15.108  INFO 14146 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.23
2017-10-16 19:42:15.353  INFO 14146 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2017-10-16 19:42:15.354  INFO 14146 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 3587 ms
2017-10-16 19:42:15.691  INFO 14146 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2017-10-16 19:42:15.692  INFO 14146 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2017-10-16 19:42:15.692  INFO 14146 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2017-10-16 19:42:15.692  INFO 14146 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
2017-10-16 19:42:15.693  INFO 14146 --- [ost-startStop-1] .s.DelegatingFilterProxyRegistrationBean : Mapping filter: 'springSecurityFilterChain' to: [/*]
2017-10-16 19:42:15.694  INFO 14146 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Mapping servlet: 'dispatcherServlet' to [/]
2017-10-16 19:42:15.926  INFO 14146 --- [           main] b.a.s.AuthenticationManagerConfiguration : 

Using default security password: 64e9d193-95d4-4944-a70c-febf29000cfd

2017-10-16 19:42:15.944  WARN 14146 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'authController' defined in file [/Users/stephanebressani/IdeaProjects/essai_ralscha_blog/target/classes/main/java/ch/rasc/jwt/AuthController.class]: Unsatisfied dependency expressed through constructor parameter 3; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.security.authentication.AuthenticationManager' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
2017-10-16 19:42:15.949  INFO 14146 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2017-10-16 19:42:15.987  INFO 14146 --- [           main] utoConfigurationReportLoggingInitializer : 

Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
2017-10-16 19:42:16.244 ERROR 14146 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 3 of constructor in main.java.ch.rasc.jwt.AuthController required a bean of type 'org.springframework.security.authentication.AuthenticationManager' that could not be found.

Action:

Consider defining a bean of type 'org.springframework.security.authentication.AuthenticationManager' in your configuration.

Process finished with exit code 1
ralscha commented 6 years ago

Thanks for the feedback. I forgot to update the code in the blog. I will do that tomorrow. The code on GitHub should work.

You have to expose the AuthenticationManager as a bean. https://github.com/ralscha/blog/blob/master/jwt/server/src/main/java/ch/rasc/jwt/security/SecurityConfig.java#L22-L26

ghost commented 6 years ago

Thanks for your reply.

i have downloaded the zip from your github, this code (with Bean and Override) is in the zip and it doesn't work

it is a compatibility problem version in your pom.xml ?

ralscha commented 6 years ago

Maybe the problem is that I use SNAPSHOT releases.

Can you try the M5 release

       <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.0.M5</version>
    </parent>
ghost commented 6 years ago

i tried 1.5.1, 1.5.2, 1.5.3, 1.5.4, 1.5.5, 1.5.6, snapshot, 2.0.0.M3 and 2.0.0.M5 and the same error is still there

Parameter 1 of constructor in main.java.ch.rasc.jwt.security.jwt.TokenProvider required a bean of type 'org.springframework.security.core.userdetails.UserDetailsService' that could not be found.
ralscha commented 6 years ago

Unfortunately I cannot reproduce the error.

This works fine on my computer.

git clone https://github.com/ralscha/blog.git
cd blog/jwt/server
./mvnw.sh spring-boot:run
ghost commented 6 years ago

Thank you, it works with mvn spring-boot:run, no idea because it didn't work yesterday on a other git clone of your project

ralscha commented 6 years ago

Good to hear that it work.

I updated the blog post this morning, with the latest source code.