我正在开发一个带有spring boot和spring security以及jwt的应用程序,但是当我运行我的应用程序时,我遇到了以下无法解决的错误:
java.lang.illegalstateexception:未能在org.springframework.boot.springapplication.callrunner(springapplication)上执行commandlinerunner。java:807)[Spring Boot-2.4.1。jar:2.4.1]在org.springframework.boot.springapplication.callrunners(springapplication。java:788)[Spring Boot-2.4.1。jar:2.4.1]在org.springframework.boot.springapplication.run(springapplication。java:333)[Spring Boot-2.4.1。jar:2.4.1]在org.springframework.boot.springapplication.run(springapplication。java:1309)[Spring Boot-2.4.1。jar:2.4.1]在org.springframework.boot.springapplication.run(springapplication。java:1298)[Spring Boot-2.4.1。jar:2.4.1]在courtjwtudemy.jwtproject.jwtprojectapplication.main(jwtprojectapplication。java:30)[classes/:na]位于sun.reflect.nativemethodaccessorimpl.invoke0(本机方法)~[na:1.8.0111]位于sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl)。java:62)~(na:1.8.0μ111)在sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl。java:43)~[na:1.8.0\u 111]位于java.lang.reflect.method.invoke(method。java:498)~[na:1.8.0\u111]位于org.springframework.boot.devtools.restart.restartlauncher.run(restartlauncher。java:49)[spring-boot-devtools-2.4.1。jar:2.4.1]
这是我的主要 Spring 靴类:
package courtjwtudemy.jwtproject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import courtjwtudemy.jwtproject.dao.MyUserReporisitory;
import courtjwtudemy.jwtproject.dao.ProductReporisitory;
import courtjwtudemy.jwtproject.entity.MyRole;
import courtjwtudemy.jwtproject.entity.Product;
import courtjwtudemy.jwtproject.service.AccountService;
@SpringBootApplication
public class JwtProjectApplication implements CommandLineRunner{
@Autowired
private ProductReporisitory prodRepository;
@Autowired
private MyUserReporisitory myUserReporisitory;
@Autowired
private AccountService accountService;
public static void main(String[] args) {
SpringApplication.run(JwtProjectApplication.class, args);
}
@Bean
BCryptPasswordEncoder getBCPE() {
return new BCryptPasswordEncoder();
}
@Override
public void run(String... args) throws Exception {
// TODO Auto-generated method stub
Product p = new Product();
p.setName("chargeur de soleil");
p.setDescription("hazar");
p.setPrice(45);
p.setPhoto("https://cdn3.lingerie-sipp.com/36357-large_default/debardeur-homme-isolant-innovation-20-noir.jpg");
p.setPromotion(false);
prodRepository.save(p);
// add user
accountService.saveRole(new MyRole(null, "User"));
accountService.saveRole(new MyRole(null, "Admin"));
accountService.saveUser("nawfal", "bgr", "bgr");
accountService.saveUser("admin", "bgr", "bgr");
accountService.saveUser("user", "bgr", "bgr");
myUserReporisitory.findAll().forEach(u -> {
System.out.println("user :" + u.getUsername() + "password :" + u.getPassword());
});
}
}
下面是我的实体用户类:
package courtjwtudemy.jwtproject.entity;
import java.util.ArrayList;
import java.util.Collection;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import com.fasterxml.jackson.annotation.JsonProperty;
@Entity
public class MyUser {
@Id
@GeneratedValue (strategy = GenerationType.IDENTITY)
private Long id;
@Column(unique = true)
private String username;
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
private String password;
@ManyToMany(fetch = FetchType.EAGER)
private Collection<MyRole> roles = new ArrayList<>();
public MyUser() {
super();
}
public MyUser(Long id, String username, String password, Collection<MyRole> roles) {
super();
this.id = id;
this.username = username;
this.password = password;
this.roles = roles;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Collection<MyRole> getRoles() {
return roles;
}
public void setRoles(Collection<MyRole> roles) {
this.roles = roles;
}
}
下面是我的实体角色类:
package courtjwtudemy.jwtproject.entity;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class MyRole {
@Id
@GeneratedValue (strategy = GenerationType.IDENTITY)
private Long id;
private String roleName;
public MyRole(Long id, String roleName) {
super();
this.id = id;
this.roleName = roleName;
}
public MyRole() {
super();
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getRoleName() {
return roleName;
}
public void setRoleName(String roleName) {
this.roleName = roleName;
}
}
有人能帮我吗?
1条答案
按热度按时间bf1o4zei1#
您可以尝试将所有业务逻辑移动到一个用控制器或服务注解的新类。
在主类中只保留public静态方法。