あなたはUserDetailsServiceインターフェイスと別のクラスでUserDetailsインターフェイスを実装します。たとえば、次のように
CustomUserDetailsService:
@Service("customUserDetailsService")
public class CustomUserDetailsService implements UserDetailsService {
// your UserRepository for your user
private final UserRepository userRepository;
@Autowired
public CustomUserDetailsService(UserRepository userRepository) {
this.userRepository = userRepository;
}
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
User user = userRepository.findByUsername(username);
if (null == user || ! user.getUsername().equals(username)) {
throw new UsernameNotFoundException("No user present with username: " + username);
} else {
return new CustomUserDetails(user);
}
}
}
CustomUserDetails:あなたのUser
クラスで
// You want to extend your User class here
public class CustomUserDetails extends User implements UserDetails {
private static final long serialVersionUID = 1L;
private Users user;
public CustomUserDetails(User user) {
super(user);
this.user = user;
}
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
// You don't talk about UserRoles, so return ADMIN for everybody or implement roles.
return AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_ADMIN");
}
@Override
public boolean isAccountNonExpired() {
return true;
}
@Override
public boolean isAccountNonLocked() {
return true;
}
@Override
public boolean isCredentialsNonExpired() {
return true;
}
@Override
public boolean isEnabled() {
// just for example
return this.user.getActive() == true;
}
@Override
public String getUsername() {
return this.user.getUsername();
}
@Override
public String getPassword() {
return this.user.getPassword();
}
// Just an example to put some addititional Data to your logged in user
public String getUserDatabase() {
return "usertable" + Integer.toString(1 + this.user.getUserId());
}
}
、休止状態のための空のコンストラクタを作成し、ユーザーインスタンスを取る1:
@Entity
@Table(name = "users")
public class User{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@Column(name = "name")
private String name;
@Column(name = "login")
private String login;
@Column(name = "password")
private String password;
public User() {}
public User(User user) {
this.id = user.getId();
this.name = user.getName();
// … the same for all properties.
}
}
WebSecurityConfigに@Autowire
CustomUserDetailsService
を入力して認証フローに注入してください:
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
private final
UserDetailsService service;
@Autowired
public WebSecurityConfig(UserDetailsService service) {
this.service = service;
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(service).passwordEncoder(passwordEncoder());
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@Override
protected void configure(HttpSecurity http) throws Exception {
//left out because not related here
}
}
これだけです。認証されたプリンシパルをすべてのコントローラまたはプロバイダのCustomUserDetailsにキャストできるようになりました:
CustomUserDetails userDetails =
(CustomUserDetails) SecurityContextHolder
.getContext()
.getAuthentication()
.getPrincipal();
私は混乱します。私の質問に新しい情報を追加しました。 – Sergei
「プライベート最終UserRepository userRepository」とは何ですか? – Sergei
これはあなたのUserRepositoryです。あなたのデータベースに問い合わせるインターフェイスは – baao