【问题标题】:User entitiy - cannot find symbol [duplicate]用户实体 - 找不到符号 [重复]
【发布时间】:2019-06-27 10:44:27
【问题描述】:

我正在尝试使用 OAuth 设置用户和身份验证。 我所有的源代码都在这里:https://github.com/Incybro/Spring-Blog

我在 Intellij IDEA 中安装了 lombok 插件,重启了 IDE 但出现了一些错误:

C:\Users\Admin\Desktop\Spring-Blog\src\main\java\com\github\Spring\Blog\config\ResourceServerConfig.java
Error:(12, 12) java: class com.github.Spring.Blog.config.ResourceServerConfig is already defined in package com.github.Spring.Blog.config
C:\Users\Admin\Desktop\Spring-Blog\src\main\java\com\github\Spring\Blog\services\UserService.java
Error:(20, 53) java: cannot find symbol
  symbol:   method getPassword()
  location: variable user of type com.github.Spring.Blog.entities.User
C:\Users\Admin\Desktop\Spring-Blog\src\main\java\com\github\Spring\Blog\SpringBlogApplication.java
Error:(27, 55) java: constructor Role in class com.github.Spring.Blog.entities.Role cannot be applied to given types;
  required: no arguments
  found: java.lang.String
  reason: actual and formal argument lists differ in length
Error:(27, 73) java: constructor Role in class com.github.Spring.Blog.entities.Role cannot be applied to given types;
  required: no arguments
  found: java.lang.String
  reason: actual and formal argument lists differ in length
C:\Users\Admin\Desktop\Spring-Blog\src\main\java\com\github\Spring\Blog\services\CustomUserDetailsService.java
Error:(29, 26) java: cannot find symbol
  symbol:   method getUsername()
  location: variable u of type com.github.Spring.Blog.entities.User
Error:(30, 26) java: cannot find symbol
  symbol:   method getPassword()
  location: variable u of type com.github.Spring.Blog.entities.User
Error:(31, 26) java: cannot find symbol
  symbol:   method isActive()
  location: variable u of type com.github.Spring.Blog.entities.User
Error:(32, 26) java: cannot find symbol
  symbol:   method isActive()
  location: variable u of type com.github.Spring.Blog.entities.User
Error:(33, 26) java: cannot find symbol
  symbol:   method isActive()
  location: variable u of type com.github.Spring.Blog.entities.User
Error:(34, 26) java: cannot find symbol
  symbol:   method isActive()
  location: variable u of type com.github.Spring.Blog.entities.User
Error:(36, 34) java: cannot find symbol
  symbol:   method getRoles()
  location: variable u of type com.github.Spring.Blog.entities.User

在这行中:

public class ResourceServerConfig extends ResourceServerConfigurerAdapter {
public void save(User user){
    user.setPassword(passwordEncoder.encode(user.getPassword()));
    repo.save(user);
}
@Bean
public CommandLineRunner setupDefaultUser(UserService service) {
    return args -> {
        service.save(new User(
                "user", //username
                "user", //password
                Arrays.asList(new Role("USER"), new Role("ACTUATOR")),//roles
                true//Active
        ));
    };
}
  @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        return repo
                .findByUsername(username)
                .map(u -> new org.springframework.security.core.userdetails.User(
                        u.getUsername(),
                        u.getPassword(),
                        u.isActive(),
                        u.isActive(),
                        u.isActive(),
                        u.isActive(),
                        AuthorityUtils.createAuthorityList(
                                u.getRoles()
                                        .stream()
                                        .map(r -> "ROLE_" + r.getName().toUpperCase())
                                        .collect(Collectors.toList())
                                        .toArray(new String[]{}))))
                .orElseThrow(() -> new UsernameNotFoundException("No user with "
                        + "the name " + username + "was found in the database"));
    }

}

没有任何东西被标记为红色。我无法启动我的应用程序。源码复制自https://www.youtube.com/watch?v=IOgCMtYMr2Q&list=PLcoE64orFoVsxAam_BuQBrNC8IO238SwH&index=2

【问题讨论】:

  • 第一个错误Error:(12, 12) java: class com.github.Spring.Blog.config.ResourceServerConfig is already defined in package com.github.Spring.Blog.config可以通过删除com.github.Spring.Blog.config.ResourceServerConfig的外部类来解决,这样文件中就只剩下带注释的类了。
  • 对于缺少的 getter:您是否也在设置中启用了注释处理?

标签: java spring maven intellij-idea oauth-2.0


【解决方案1】:

修复您的 ResourceServerConfig 类。使用下面的代码

@Configuration
@EnableResourceServer
public class ResourceServerConfig extends ResourceServerConfigurerAdapter {

而不是

public class ResourceServerConfig {

@Configuration
@EnableResourceServer
public class ResourceServerConfig extends ResourceServerConfigurerAdapter {

【讨论】:

    猜你喜欢
    • 2010-10-12
    • 2019-05-31
    • 2015-06-07
    • 2020-06-13
    • 2012-09-09
    • 2011-10-29
    • 2015-12-15
    相关资源
    最近更新 更多