h2はクラス・ユーザーのプロパティーのみを格納しますが、プロパティーのユーザー名、パスワード、および権限をSpring Securityのユーザー・クラスから保管することはできません。 DBエントリのH2はユーザー名、パスワード、ロールを格納していません
package com.netcetera.videoverification.service;
import com.netcetera.videoverification.persistence.model.User;
import com.netcetera.videoverification.persistence.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import java.util.Collection;
import java.util.HashSet;
@Service
public class UserService {
@Autowired
private UserRepository userRepository;
public boolean create(String username, String email, String password) {
String passwordHash = new BCryptPasswordEncoder().encode(password);
Collection<GrantedAuthority> authorities = new HashSet<>();
authorities.add(new SimpleGrantedAuthority("ROLE_ADMIN"));
User user = new User(username, passwordHash, authorities, email, "First", "Last");
this.userRepository.save(user);
return true;
}
}
スクリーンショット:これはサービスである
package com.netcetera.videoverification.persistence.repository;
import com.netcetera.videoverification.persistence.model.User;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface UserRepository extends CrudRepository<User,Long> {
}
:
package com.netcetera.videoverification.persistence.model;
import lombok.Getter;
import lombok.Setter;
import org.springframework.security.core.GrantedAuthority;
import javax.persistence.*;
import java.util.Collection;
@Entity
@Table(name = "user")
@Setter
@Getter
public class User extends org.springframework.security.core.userdetails.User {
@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
private Long id;
private String email;
private String firstName;
private String lastName;
public User(String username, String password, Collection<? extends GrantedAuthority> authorities) {
super(username, password, authorities);
}
public User(String username, String password, Collection<? extends GrantedAuthority> authorities, String email,
String firstName, String lastName) {
super(username, password, authorities);
this.email = email;
this.firstName = firstName;
this.lastName = lastName;
}
}
リポジトリのためのコード:ここで
は、ユーザーのための私のクラスである DB entries screenshot