我正试图从内存数据库中的H2迁移到mysql,并在删除H2依赖项后将compile("mysql:mysql-connector-java:5.1.6")
合并到我的build.gradle中。我还在我的application.properties中添加了以下内容:
spring.datasource.url=jdbc:mysql://localhost:3306/mutibodb
spring.datasource.username=xxx
spring.datasource.password=xxx
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.hibernate.ddl-auto: create
然而,当我重建时,我收到此错误:http://pastebin.com/4cS9Dk0U 如果我没有放置mysql或h2依赖项,这就是我得到的相同错误,这意味着它根本不接受它。
我在H2数据库中使用的完整代码位于:https://github.com/devdeep1987/MutiboProject/tree/master/MutiboServer
如果我错过了一个步骤,请你告诉我。
更新 我的CustomUserDetailsService类如下:
@Service
public class CustomUserDetailsService implements UserDetailsService {
private UserRepository repository;
private static final Logger logger = LoggerFactory.getLogger(CustomUserDetailsService.class);
@Autowired
public CustomUserDetailsService(UserRepository repo) {
this.repository = repo;
}
@Override
public UserDetails loadUserByUsername(String name) throws UsernameNotFoundException {
logger.info("username:"+name);
User u = repository.findByUsername(name);
if (u == null)
throw new UsernameNotFoundException("User details not found with this username: " + name);
String username = u.getUsername();
String password = u.getPassword();
List authList = new ArrayList();
authList.add(new SimpleGrantedAuthority("USER"));
//get the encoded password
//String encodedPassword = passwordEncoder.encode(password);
org.springframework.security.core.userdetails.User user = new org.springframework.security.core.userdetails.User(username, password, authList);
return user;
}
public boolean createUser(String username, String password) {
User existing = repository.findByUsername(username);
if(existing!=null)
return false;
User u = new User();
u.setUsername(username);
u.setPassword(password);
repository.save(u);
return true;
}
private List getAuthorities(String role) {
List authList = new ArrayList();
authList.add(new SimpleGrantedAuthority("USER"));
//you can also add different roles here
//for example, the user is also an admin of the site, then you can add ROLE_ADMIN
//so that he can view pages that are ROLE_ADMIN specific
if (role != null && role.trim().length() > 0) {
if (role.equals("admin")) {
authList.add(new SimpleGrantedAuthority("ROLE_ADMIN"));
}
}
return authList;
}
}
我的UserRepository类是:
@Repository
public interface UserRepository extends CrudRepository<User, Long>{
User findByUsername(String username);
}
答案 0 :(得分:0)
将spring-boot-gradle-plugin的版本从1.0.2.RELEASE更改为1.1.4.RELEASE修复此问题。不知道为什么会这样。