500 Internal Server Error - Spring JWT

I have a problem which is I can’t login even I authorize login path in webConfiguration so please can someone help me to solve this issue and thank you.
User.Controller

@PostMapping("/login")
    public ResponseEntity login(@Valid @RequestBody LoginRequest data) {
        try {
            String username = data.getUsername();
            authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(username, data.getPassword()));
            String token = jwtTokenProvider.createToken(username, this.utilisateurRepository.findByUsername(username).getRoles());
            Map<Object, Object> model = new HashMap<>();
            model.put("username", username);
            model.put("token", token);
            return ok(model);
        } catch (AuthenticationException e) {
            throw new BadCredentialsException("Invalid email/password supplied");
        }

WebSecurityConfigure

@Override
    protected void configure(HttpSecurity http) throws Exception {
        http.cors().and()
                .csrf().disable()
                .exceptionHandling().authenticationEntryPoint(unauthorizedHandler)
                .and()
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                .authorizeRequests()
                .antMatchers( "/user/login/**","/","/signup/**","/logout/**")
                .permitAll()
                .antMatchers("/facture/all/**").permitAll()
                .antMatchers("/user/add/**").permitAll()
                .antMatchers("/user/all/**").hasAuthority("ADMIN")
                .anyRequest().authenticated()
                .and()
                .addFilter(new JWTAuthenticationFilter(authenticationManager()))
                .addFilterBefore(new JWTAuthorizationFilter(), UsernamePasswordAuthenticationFilter.class);
    }

POST.method

{

"username":"admin",
"password":"1100"

}

Postman.result
{
“timestamp”: “2020-04-09T00:29:58.982+0000”,
“status”: 500,
“error”: “Internal Server Error”,
“message”: “No message available”,
“trace”: "java.lang.StackOverflowError\r\n\tat org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter$UserDetailsServiceDelegator.loadUserByUsername(WebSecurityConfigurerAdapter.java:449)\r\n\tat org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter$UserDetailsServiceDelegator.loadUserByUsername(WebSecurityConfigurerAdapter.java:449)\r\n\tat org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter$UserDetailsServiceDelegator.loadUserByUsername(WebSecurityConfigurerAdapter.java:449)\r\n\tat org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter$UserDetailsServiceDelegator. “path”: “/user/login”
}

Hi there @kindtaher, I apologize for the delay in response!

Can you share a bit more about the stack you are using and if it’s a public repo or not? have you ever been able to login successfully or did it recently stop working? Do you have a HAR file capture because they would be helpful to walk through the auth flow. If you are willing to record one and direct message it over please be sure to select “Preserve log” to catch redirects and scrub the file of user passwords before passing, thanks!

Hi @James.Morrison I solved the problem I define a @Bean ( PasswordEncoder) in my @Configuration class then define it in my controller.
Thank you

1 Like

Sounds great @kindtaher, I’m glad it all came together!

1 Like

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.