【发布时间】:2018-08-31 17:37:11
【问题描述】:
如果我实施得当,我正在尝试使用 Postman 测试 OAuth 2.0 授权。
我正在通过邮递员发送这个作为回报的预期令牌
http://localhost:8080/oauth/token/grant_type=password&username=user&password=user
但我在这部分代码中有错误:
@SpringBootApplication
public class KamehouseApplication {
public static void main(String[] args) {
SpringApplication.run(KamehouseApplication.class, args);
}
@Autowired
public void configure(AuthenticationManagerBuilder auth, UserRepository repo, User user) throws Exception {
if (repo.count() == 0)
user = (new User("user", // username
"user", // password
Arrays.asList(new Role("USER"), new Role("ACTUATOR"))));// roles
repo.save(user);
auth.userDetailsService(s -> new CustomUserDetails(repo.findByUsername(s)));
}
}
红色下划线部分是
Arrays.asList(new Role("USER"), new Role("ACTUATOR"))));
说
The method asList(T...) in the type Arrays is not applicable for the arguments (Role, Role)
这是我的用户类
@Entity
@Table(name = "user")
public class User {
@Id
@GeneratedValue
public int id;
public String firstname;
public String lastname;
public String username;
public String password;
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private List<Role> roles;
public User() {
}
//Getters/Setters
【问题讨论】:
-
您确定要导入
java.util.Arrays而不是其他Arrays类吗?User构造函数的签名是什么? -
java版本是多少?
-
@lealceldeiro java版本“1.8.0_131”
-
也许不是问题,但我没有看到
User(String username, String password, List roles)的任何构造函数...只有User()。 -
如果在另一行你用“Long”代替角色为我做
List<Role> roles = Arrays.asList(new Role("USER"), new Role("ACTUATOR"),你会得到同样的结果吗?您也没有带参数的 User 构造函数。也不需要在user = (new User(...));处使用括号括起来,还从方法签名中删除用户用户,您从未使用过传入的值。