springjwt

Generating JWT token with JwtUtil class in Spring Boot resulting in NullPointerException


main dashboard main dashboardmain dashboardmain dashboardmain dashboardmain dashboardmain dashboard

main dashboard main dashboardmain dashboardmain dashboardmain dashboardmain dashboardmain dashboard

main dashboard main dashboardmain dashboardmain dashboardmain dashboardmain dashboardmain dashboard main dashboard main dashboardmain dashboardmain dashboardmain dashboardmain dashboardmain dashboard


Solution

  • in class: public class MySecurityConfig extends WebSecurityConfigurerAdapter the WebSecurityConfigurerAdapter is deprecated, instead you can create class like this:

    @Configuration
    @EnableWebSecurity
    public class WebSecurityConfig {
        
        @Bean
        BCryptPasswordEncoder bCryptPasswordEncoder() {
            return new BCryptPasswordEncoder();
        }
        
        @Bean
        SecurityFilterChain filterChain(HttpSecurity http)throws Exception{
            http.authorizeHttpRequests(authz -> authz
                    .requestMatchers(HttpMethod.GET,"/testAPI").permitAll()
                    .requestMatchers("").permitAll()
                    .anyRequest().authenticated())
            .httpBasic(Customizer.withDefaults());
            return http.build();
        }
        
        @Bean
        UserDetailsService userDetailsService() {
            InMemoryUserDetailsManager userDetailsService = new 
        InMemoryUserDetailsManager();
            UserDetails userDetails = User
                    .withUsername("vinod")
                    .password(bCryptPasswordEncoder().encode("singh"))
                    .authorities("read").build();
            userDetailsService.createUser(userDetails);
            return userDetailsService;
        }
    
    }