-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: security user details & service 정의
- Loading branch information
1 parent
114a59d
commit 9e6263b
Showing
2 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
84 changes: 84 additions & 0 deletions
84
.../src/main/java/kr/co/pennyway/api/common/security/authentication/SecurityUserDetails.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package kr.co.pennyway.api.common.security.authentication; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnore; | ||
import kr.co.pennyway.domain.domains.user.domain.User; | ||
import kr.co.pennyway.domain.domains.user.type.Role; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import org.springframework.security.core.GrantedAuthority; | ||
import org.springframework.security.core.userdetails.UserDetails; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collection; | ||
|
||
@Getter | ||
public class SecurityUserDetails implements UserDetails { | ||
private final Long userId; | ||
private final String username; | ||
private final Collection<? extends GrantedAuthority> authorities; | ||
private final boolean accountNonLocked; | ||
|
||
@JsonIgnore | ||
private boolean enabled; | ||
@JsonIgnore | ||
private String password; | ||
@JsonIgnore | ||
private boolean credentialsNonExpired; | ||
@JsonIgnore | ||
private boolean accountNonExpired; | ||
|
||
@Builder | ||
private SecurityUserDetails(Long userId, String username, Collection<? extends GrantedAuthority> authorities, boolean accountNonLocked) { | ||
this.userId = userId; | ||
this.username = username; | ||
this.authorities = authorities; | ||
this.accountNonLocked = accountNonLocked; | ||
} | ||
|
||
public static UserDetails from(User user) { | ||
return SecurityUserDetails.builder() | ||
.userId(user.getId()) | ||
.username(user.getUsername()) | ||
.authorities(Arrays.stream(Role.values()) | ||
.filter(roleType -> roleType == user.getRole()) | ||
.map(roleType -> (GrantedAuthority) roleType::getType) | ||
.toList()) | ||
.accountNonLocked(user.getLocked()) | ||
.build(); | ||
} | ||
|
||
@Override | ||
public Collection<? extends GrantedAuthority> getAuthorities() { | ||
return authorities; | ||
} | ||
|
||
@Override | ||
public String getPassword() { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public String getUsername() { | ||
return username; | ||
} | ||
|
||
@Override | ||
public boolean isAccountNonExpired() { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public boolean isAccountNonLocked() { | ||
return accountNonLocked; | ||
} | ||
|
||
@Override | ||
public boolean isCredentialsNonExpired() { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public boolean isEnabled() { | ||
throw new UnsupportedOperationException(); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
...rc/main/java/kr/co/pennyway/api/common/security/authentication/UserDetailServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package kr.co.pennyway.api.common.security.authentication; | ||
|
||
import kr.co.pennyway.domain.domains.user.service.UserService; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.cache.annotation.Cacheable; | ||
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 | ||
@RequiredArgsConstructor | ||
public class UserDetailServiceImpl implements UserDetailsService { | ||
private final UserService userService; | ||
|
||
@Override | ||
@Cacheable(value = "securityUser", key = "#userId", unless = "#result == null", cacheManager = "securityUserCacheManager") | ||
public UserDetails loadUserByUsername(String userId) throws UsernameNotFoundException { | ||
try { | ||
return SecurityUserDetails.from(userService.readUser(Long.parseLong(userId))); | ||
} catch (Exception e) { | ||
return null; | ||
} | ||
} | ||
} |