상세 컨텐츠

본문 제목

UserDetailsService와 UserDetailsManager

Spring Security

by Wanderer Kim 2025. 4. 27. 11:09

본문

728x90

spring security에서 사용자 관리를 위해 사용하는 UserDetailsService와 UserDetailsManager에 대해서 알아보자.

  • UserDetailsService : 사용자 이름으로 사용자를 검색하는 역할을 한다.
  • UserDetailsManager : 대부분의 애플리케이션에서 필요한 사용자 추가, 수정,  삭제 작업을 수행한다.

spring security에서는 두 개의 인터페이스를 분리하여 앱에 필요 없는 도작을 구현하도록 강제하지 않는다. 이렇게 함으로써 유연성이 향상된다.

 

아래는 UserDetailsService와 UserDetailsManager 인터페이스의 코드이다.

public interface UserDetailsService {

	/**
	 * Locates the user based on the username. In the actual implementation, the search
	 * may possibly be case sensitive, or case insensitive depending on how the
	 * implementation instance is configured. In this case, the <code>UserDetails</code>
	 * object that comes back may have a username that is of a different case than what
	 * was actually requested..
	 * @param username the username identifying the user whose data is required.
	 * @return a fully populated user record (never <code>null</code>)
	 * @throws UsernameNotFoundException if the user could not be found or the user has no
	 * GrantedAuthority
	 */
	UserDetails loadUserByUsername(String username) throws UsernameNotFoundException;

}

 

public interface UserDetailsManager extends UserDetailsService {

	/**
	 * Create a new user with the supplied details.
	 */
	void createUser(UserDetails user);

	/**
	 * Update the specified user.
	 */
	void updateUser(UserDetails user);

	/**
	 * Remove the user with the given login name from the system.
	 */
	void deleteUser(String username);

	/**
	 * Modify the current user's password. This should change the user's password in the
	 * persistent user repository (database, LDAP etc).
	 * @param oldPassword current password (for re-authentication if required)
	 * @param newPassword the password to change to
	 */
	void changePassword(String oldPassword, String newPassword);

	/**
	 * Check if a user with the supplied login name exists in the system.
	 */
	boolean userExists(String username);

}

 

개발자가 유저 정보를 가져오는 로직만 필요하다면 UserDetailsService만 상속받아서 구현하면 되고,  유저 정보 조회 외에 유저 생성/수정/삭제 및 패스워드 변경 같은 더 많은 로직이 필요하마녀 UserDetailsManager를 상속받아서 구현하면 된다.

 

UserDetails

UserDetailsService와 UserDetailsManager 인터페이스를 보면 UserDetails를 사용하는 부분이 보이는데, spring security에서는 UserDetails를 구현해서 spring security가 이해할 수 있게 사용자를 기술해야 한다.

spring security에서 사용자는 사용자가 수행할 수 있는 작업을 나타내는 이용 권리의 집합을 나타낸다.

 

사용자 권리를 수행하는 구성 요소 간의 종속성

  • UserDetailsService는 UserDetails를 이용한다.
  • UserDetails는 하나 이상의 GrantedAuthority를 가진다. 여기서 GrantedAuthority는 사용자의 권한을 나타낸다.
public interface GrantedAuthority extends Serializable {

	/**
	 * If the <code>GrantedAuthority</code> can be represented as a <code>String</code>
	 * and that <code>String</code> is sufficient in precision to be relied upon for an
	 * access control decision by an {@link AuthorizationManager} (or delegate), this
	 * method should return such a <code>String</code>.
	 * <p>
	 * If the <code>GrantedAuthority</code> cannot be expressed with sufficient precision
	 * as a <code>String</code>, <code>null</code> should be returned. Returning
	 * <code>null</code> will require an <code>AccessDecisionManager</code> (or delegate)
	 * to specifically support the <code>GrantedAuthority</code> implementation, so
	 * returning <code>null</code> should be avoided unless actually required.
	 * @return a representation of the granted authority (or <code>null</code> if the
	 * granted authority cannot be expressed as a <code>String</code> with sufficient
	 * precision).
	 */
	String getAuthority();

}

위 코드는 GrantedAuthority interface의 코드이다. 해당 코드를 보면 GrantedAuthority interface는 사용자의 권한을 조회하는 역할만 한다는 것을 알 수 있다.

반응형

관련글 더보기

댓글 영역