I am newbie in Java Spring Boot and try to learn Spring Security. I have developed a project in this and encounter with this error when login: class com.sun.proxy.$Proxy107 cannot be cast to class com.security.domain.User (com.sun.proxy.$Proxy107 and com.security.domain.User are in unnamed module of loader 'app') My UserService Class :
@Service
@RequiredArgsConstructor
public class UserServiceImpl implements UserService {
private final UserRepository userRepository;
private final PasswordEncoder passwordEncoder;
@Override
public void add(User user) {
user.setPassword(passwordEncoder.encode(user.getPassword()));
user.getRoles().add(Role.USER);
userRepository.save(user);
}
@Override
public List<User> findAll() {
return userRepository.findAll();
}
@Override
public void deleteById(Integer userId) {
userRepository.deleteById(userId);
}
and my config class:
@Configuration
@EnableWebSecurity
@RequiredArgsConstructor
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {
private final UserDetailsService userDetailsService;
@Bean
public PasswordEncoder passwordEncoder(){
//NoOpPasswordEncoder.getInstance();
return new BCryptPasswordEncoder();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth
.userDetailsService(userDetailsService)
.passwordEncoder(passwordEncoder());
}
@Override
public void configure(WebSecurity web) throws Exception {
web
.debug(true)
.ignoring()
.antMatchers("/h2-console/**", "/css", "/swagger");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf()
.disable()
.authorizeRequests()
.antMatchers("/register/**")
.permitAll()
.antMatchers("/")
.authenticated()
.antMatchers("/delete/**")
.hasAuthority(Role.ADMIN.getAuthority())
.anyRequest()
.authenticated()
.and()
.formLogin();
}
}
This error appear when My username is correct. How can I solve this problem?