Skip to content
Advertisement

user detail service cannot be cast to user in a unnamed module loader app

I don’t know what I did wrong. I was trying to implement the Jwt token(only post method).it shows the exception that

Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.ClassCastException: class com.vivek.discussion.service.UserDetailServiceImpl$1 cannot be cast to class com.vivek.discussion.model.User (com.vivek.discussion.service.UserDetailServiceImpl$1 and com.vivek.discussion.model.User are in unnamed module of loader ‘app’)] with root cause

java.lang.ClassCastException: class com.vivek.discussion.service.UserDetailServiceImpl$1 cannot be cast to class com.vivek.discussion.model.User (com.vivek.discussion.service.UserDetailServiceImpl$1 and com.vivek.discussion.model.User are in unnamed module of loader ‘app’) at com.vivek.discussion.security.JwtProvider.generateToken(JwtProvider.java:26) ~[classes/:na] at com.vivek.discussion.service.AuthService.login(AuthService.java:85) ~[classes/:na]

I am not sharing DTO classes. I know the problem is in Jwtprovider but no idea why

UserDetailService

JavaScript

AuthService

JavaScript

AuthController

JavaScript

JwtProvider

JavaScript

pom.xml(only jwt dependency)

JavaScript

SecurityConfig

JavaScript

Advertisement

Answer

The problem is that your UserDetailsService implementation is returning something of type UserDetails:

JavaScript

But JwtProvider is asking for something of type User:

JavaScript

Instead of returning an anonymous inner class, consider adding a copy constructor to User and using a static inner class that extends your User class and implements UserDetails:

JavaScript

Then, you can do in UserDetailsServiceImpl:

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement