用户位于java spring boot中加载程序“app”的未命名模块中

r6hnlfcb  于 2021-07-06  发布在  Java
关注(0)|答案(0)|浏览(224)

我是JavaSpringBoot的新手,尝试学习SpringSecurity。我在此开发了一个项目,在登录时遇到此错误:class com.sun.proxy.$proxy107不能强制转换到class com.security.domain.user(com.sun.proxy.$proxy107和com.security.domain.user位于加载程序“app”的未命名模块中)我的userservice类:

@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);
    }

以及我的配置类:

@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();
    }
}

当我的用户名正确时会出现此错误。我怎样才能解决这个问题?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题