Skip to content

Commit

Permalink
feat: security user details & service 정의
Browse files Browse the repository at this point in the history
  • Loading branch information
psychology50 committed Mar 26, 2024
1 parent 114a59d commit 9e6263b
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 0 deletions.
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();
}
}
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;
}
}
}

0 comments on commit 9e6263b

Please sign in to comment.