SpringBoot入門


5-2 掲示板への認証機能の追加

④認証ユーザアクセスクラスを追加します。

LoginUserDetailsService.java

package com.example.demo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.core.userdetails .UsernameNotFoundException; import org.springframework.stereotype.Service; @Service public class LoginUserDetailsService implements UserDetailsService { @Autowired UserRepository userRepository; @Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { User user = userRepository.findByUsername(username); if (user == null) { throw new UsernameNotFoundException("user not found"); } return new LoginUserDetails(user); } }