Skip to Content
Suffering builds character

6.UserDetails

스프링 시큐리티에서 사용자의 정보와 관련된 부분은 UserDetails 인터페이스에서 담당

UserDetailsService는 DB에서 조회된 사용자 정보를 UserDetails에 담아 AuthenticationProvider에게 반환함

AuthenticationProviderUserDetailsService로부터 전달 받은 UserDetails에 담겨 있는 정보를 토대로 남은 인증 작업을 수행,

인증이 성공하면 Authentication 객체를 생성하여 AuthenticationManager에게 Authentication 객체를 반환함

1. UserDetails

사용자에 관한 정보를 담고 있는 인터페이스

사용자의 이름(getUsername())과 암호(getPassword()), 권한 정보(getAuthorities()) 등을 취득하기 위한 메서드를 제공하며, 사용자 계정을 만료, 잠금, 비활성화 등을 적용할 수 있게 제공하는 인터페이스

단순하게 사용자 정보를 보관하고 있다가 이후 Authentication 객체로 캡슐화되며, UserDetailsService가 사용자 정보를 조회 후 반환하는 타입으로 이용됨

UserDetails.java
public interface UserDetails extends Serializable { /** * Returns the authorities granted to the user. Cannot return <code>null</code>. * @return the authorities, sorted by natural key (never <code>null</code>) */ Collection<? extends GrantedAuthority> getAuthorities(); // 사용자가 수행할 수 있는 작업을 GrantedAuthority 인스턴스의 컬렉션으로 반환 /** * Returns the password used to authenticate the user. * @return the password */ String getPassword(); // 사용자 암호 반환 /** * Returns the username used to authenticate the user. Cannot return * <code>null</code>. * @return the username (never <code>null</code>) */ String getUsername(); // 사용자 이름 반환 /** * Indicates whether the user's account has expired. An expired account cannot be * authenticated. * @return <code>true</code> if the user's account is valid (ie non-expired), * <code>false</code> if no longer valid (ie expired) */ boolean isAccountNonExpired(); // 계정 만료 여부 /** * Indicates whether the user is locked or unlocked. A locked user cannot be * authenticated. * @return <code>true</code> if the user is not locked, <code>false</code> otherwise */ boolean isAccountNonLocked(); // 계정 잠금 여부 /** * Indicates whether the user's credentials (password) has expired. Expired * credentials prevent authentication. * @return <code>true</code> if the user's credentials are valid (ie non-expired), * <code>false</code> if no longer valid (ie expired) */ boolean isCredentialsNonExpired(); // 자격증명 만료 여부 /** * Indicates whether the user is enabled or disabled. A disabled user cannot be * authenticated. * @return <code>true</code> if the user is enabled, <code>false</code> otherwise */ boolean isEnabled(); // 계정 활성화/비활성화 여부 }

getPassword(), getUsername()을 제외한 나머지 5개의 메서드는 사용자가 리소스에 접근할 수 있도록 권한을 부여하기 위한 메서드(필요에 따라 사용자 계정을 활성화/비활성화)

2. User

스프링 시큐리티에서 제공하는 대표적인 구현체로는 User 클래스가 있음

User.java
package org.springframework.security.core.userdetails; public class User implements UserDetails, CredentialsContainer { private static final long serialVersionUID = SpringSecurityCoreVersion.SERIAL_VERSION_UID; private static final Log logger = LogFactory.getLog(User.class); private String password; private final String username; private final Set<GrantedAuthority> authorities; private final boolean accountNonExpired; private final boolean accountNonLocked; private final boolean credentialsNonExpired; private final boolean enabled; // ... }
Last updated on