DB 기반 로그인 검증
이번에는 DB에서 AuthenticationManager까지 로직을 구현한다.
구현은 UserDetails, UserDetailsService, UserRepository의 회원 조회 메소드를 진행한다.
UserRepository
- UserRepository
package com.example.springjwt.repository;
import com.example.springjwt.entity.UserEntity;
import org.springframework.data.jpa.repository.JpaRepository;
public interface UserRepository extends JpaRepository<UserEntity, Integer> {
Boolean existsByUsername(String username);
// username을 받아 DB 테이블에서 회원을 조회하는 메소드 작성
UserEntity findByUsername(String username);
}
UserDetailsService 커스텀 구현
- CustomUserDetailsService
package com.example.springjwt.service;
import com.example.springjwt.dto.CustomUserDetails;
import com.example.springjwt.entity.UserEntity;
import com.example.springjwt.repository.UserRepository;
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 CustomUserDetailsService implements UserDetailsService {
private final UserRepository userRepository;
public CustomUserDetailsService(UserRepository userRepository) {
this.userRepository = userRepository;
}
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
// DB에서 조회
UserEntity userData = userRepository.findByUsername(username);
if (userData != null) {
// UserDetails에 담아서 return 하면 AuthenticationManager가 검증 함
return new CustomUserDetails(userData);
}
return null;
}
}
UserDetails 커스텀 구현
- CustomUserDetails
package com.example.springjwt.dto;
import com.example.springjwt.entity.UserEntity;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
public class CustomUserDetails implements UserDetails {
private final UserEntity userEntity;
public CustomUserDetails(UserEntity userEntity) {
this.userEntity = userEntity;
}
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
Collection<GrantedAuthority> collection = new ArrayList<>();
collection.add(new GrantedAuthority() {
@Override
public String getAuthority() {
return userEntity.getRole();
}
});
return collection;
}
@Override
public String getPassword() {
return userEntity.getPassword();
}
@Override
public String getUsername() {
return userEntity.getUsername();
}
@Override
public boolean isAccountNonExpired() {
return true;
}
@Override
public boolean isAccountNonLocked() {
return true;
}
@Override
public boolean isCredentialsNonExpired() {
return true;
}
@Override
public boolean isEnabled() {
return true;
}
}
참고
https://www.devyummi.com/page?id=668d58ba680e8f4b44fc981d
'Spring > Spring Security - JWT' 카테고리의 다른 글
Spring Security JWT 10 : 로그인 성공 JWT 발급 (0) | 2024.12.21 |
---|---|
Spring Security JWT 9 : 발급 및 검증 클래스 (0) | 2024.12.21 |
Spring Security JWT 7 : 로그인 필터 구현 (0) | 2024.12.21 |
Spring Security JWT 5 : DB 연결 및 Entity 작성 (0) | 2024.12.20 |
Spring Security JWT 4 : POSTMAN 설치 (0) | 2024.12.20 |