Compare commits
2 Commits
31aed4bda0
...
b0398fccee
Author | SHA1 | Date | |
---|---|---|---|
b0398fccee | |||
92be6caf80 |
@@ -69,7 +69,6 @@
|
|||||||
oid bigint not null,
|
oid bigint not null,
|
||||||
updated_at timestamp(6) not null,
|
updated_at timestamp(6) not null,
|
||||||
updated_oid bigint,
|
updated_oid bigint,
|
||||||
role varchar(40) not null check (role in ('MEMBER','ADMIN','SYSTEM_ADMIN')),
|
|
||||||
login_ip varchar(45),
|
login_ip varchar(45),
|
||||||
name varchar(100) not null,
|
name varchar(100) not null,
|
||||||
password varchar(100) not null,
|
password varchar(100) not null,
|
||||||
|
@@ -14,6 +14,5 @@ import java.time.LocalDateTime;
|
|||||||
public class LoginResponseDto {
|
public class LoginResponseDto {
|
||||||
|
|
||||||
private String userId;
|
private String userId;
|
||||||
private String role;
|
|
||||||
private LocalDateTime lastLoginAt;
|
private LocalDateTime lastLoginAt;
|
||||||
}
|
}
|
||||||
|
@@ -1,6 +1,5 @@
|
|||||||
package com.bio.bio_backend.domain.base.member.dto;
|
package com.bio.bio_backend.domain.base.member.dto;
|
||||||
|
|
||||||
import com.bio.bio_backend.domain.base.member.enums.MemberRole;
|
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Builder;
|
import lombok.Builder;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
@@ -24,7 +23,6 @@ public class MemberDto implements UserDetails {
|
|||||||
private String password;
|
private String password;
|
||||||
private String name;
|
private String name;
|
||||||
private String email;
|
private String email;
|
||||||
private MemberRole role;
|
|
||||||
private Boolean useFlag;
|
private Boolean useFlag;
|
||||||
private String refreshToken;
|
private String refreshToken;
|
||||||
private String loginIp;
|
private String loginIp;
|
||||||
@@ -34,7 +32,7 @@ public class MemberDto implements UserDetails {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Collection<? extends GrantedAuthority> getAuthorities() {
|
public Collection<? extends GrantedAuthority> getAuthorities() {
|
||||||
return Collections.singletonList(new SimpleGrantedAuthority("ROLE_" + this.role.getValue()));
|
return Collections.singletonList(new SimpleGrantedAuthority("ROLE_USER"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@@ -1,6 +1,5 @@
|
|||||||
package com.bio.bio_backend.domain.base.member.entity;
|
package com.bio.bio_backend.domain.base.member.entity;
|
||||||
|
|
||||||
import com.bio.bio_backend.domain.base.member.enums.MemberRole;
|
|
||||||
import com.bio.bio_backend.global.constants.AppConstants;
|
import com.bio.bio_backend.global.constants.AppConstants;
|
||||||
import com.bio.bio_backend.global.entity.BaseEntity;
|
import com.bio.bio_backend.global.entity.BaseEntity;
|
||||||
import jakarta.persistence.*;
|
import jakarta.persistence.*;
|
||||||
@@ -37,9 +36,7 @@ public class Member extends BaseEntity {
|
|||||||
@Column(name = "email", nullable = false, length = 255)
|
@Column(name = "email", nullable = false, length = 255)
|
||||||
private String email;
|
private String email;
|
||||||
|
|
||||||
@Enumerated(EnumType.STRING)
|
|
||||||
@Column(name = "role", nullable = false, length = 40)
|
|
||||||
private MemberRole role;
|
|
||||||
|
|
||||||
@Column(name = "use_flag", nullable = false)
|
@Column(name = "use_flag", nullable = false)
|
||||||
@Builder.Default
|
@Builder.Default
|
||||||
|
@@ -1,38 +0,0 @@
|
|||||||
package com.bio.bio_backend.domain.base.member.enums;
|
|
||||||
|
|
||||||
import lombok.Getter;
|
|
||||||
import lombok.RequiredArgsConstructor;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 회원 역할을 정의하는 Enum
|
|
||||||
*/
|
|
||||||
@Getter
|
|
||||||
@RequiredArgsConstructor
|
|
||||||
public enum MemberRole {
|
|
||||||
|
|
||||||
MEMBER("MEMBER", "일반 회원"),
|
|
||||||
ADMIN("ADMIN", "관리자"),
|
|
||||||
SYSTEM_ADMIN("SYSTEM_ADMIN", "시스템 관리자");
|
|
||||||
|
|
||||||
private final String value;
|
|
||||||
private final String description;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 문자열 값으로부터 MemberRole을 찾는 메서드
|
|
||||||
*/
|
|
||||||
public static MemberRole fromValue(String value) {
|
|
||||||
for (MemberRole role : values()) {
|
|
||||||
if (role.value.equals(value)) {
|
|
||||||
return role;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
throw new IllegalArgumentException("Unknown MemberRole value: " + value);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 기본 역할 반환
|
|
||||||
*/
|
|
||||||
public static MemberRole getDefault() {
|
|
||||||
return MEMBER;
|
|
||||||
}
|
|
||||||
}
|
|
@@ -16,10 +16,9 @@ public interface MemberMapper {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* CreateMemberRequestDto를 MemberDto로 변환
|
* CreateMemberRequestDto를 MemberDto로 변환
|
||||||
* 기본값 설정: role = MemberRole.MEMBER, useFlag = true
|
* 기본값 설정: useFlag = true
|
||||||
*/
|
*/
|
||||||
@Mapping(target = "oid", ignore = true)
|
@Mapping(target = "oid", ignore = true)
|
||||||
@Mapping(target = "role", expression = "java(com.bio.bio_backend.domain.base.member.enums.MemberRole.getDefault())")
|
|
||||||
@Mapping(target = "useFlag", constant = "true")
|
@Mapping(target = "useFlag", constant = "true")
|
||||||
@Mapping(target = "refreshToken", ignore = true)
|
@Mapping(target = "refreshToken", ignore = true)
|
||||||
@Mapping(target = "loginIp", ignore = true)
|
@Mapping(target = "loginIp", ignore = true)
|
||||||
|
@@ -2,7 +2,6 @@ package com.bio.bio_backend.domain.base.member.service;
|
|||||||
|
|
||||||
import com.bio.bio_backend.domain.base.member.dto.MemberDto;
|
import com.bio.bio_backend.domain.base.member.dto.MemberDto;
|
||||||
import com.bio.bio_backend.domain.base.member.entity.Member;
|
import com.bio.bio_backend.domain.base.member.entity.Member;
|
||||||
import com.bio.bio_backend.domain.base.member.enums.MemberRole;
|
|
||||||
import com.bio.bio_backend.domain.base.member.mapper.MemberMapper;
|
import com.bio.bio_backend.domain.base.member.mapper.MemberMapper;
|
||||||
import com.bio.bio_backend.domain.base.member.repository.MemberRepository;
|
import com.bio.bio_backend.domain.base.member.repository.MemberRepository;
|
||||||
import com.bio.bio_backend.global.exception.ApiException;
|
import com.bio.bio_backend.global.exception.ApiException;
|
||||||
@@ -50,7 +49,6 @@ public class MemberServiceImpl implements MemberService {
|
|||||||
.password(bCryptPasswordEncoder.encode(memberDto.getPassword()))
|
.password(bCryptPasswordEncoder.encode(memberDto.getPassword()))
|
||||||
.name(memberDto.getName())
|
.name(memberDto.getName())
|
||||||
.email(memberDto.getEmail())
|
.email(memberDto.getEmail())
|
||||||
.role(MemberRole.getDefault())
|
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
member.setCreatedOid(AppConstants.ADMIN_OID);
|
member.setCreatedOid(AppConstants.ADMIN_OID);
|
||||||
|
@@ -64,8 +64,8 @@ public class JwtTokenIssuanceFilter extends UsernamePasswordAuthenticationFilter
|
|||||||
MemberDto member = (MemberDto) userDetails;
|
MemberDto member = (MemberDto) userDetails;
|
||||||
|
|
||||||
// 토큰 생성
|
// 토큰 생성
|
||||||
String accessToken = jwtUtils.createAccessToken(member.getUserId(), member.getRole().getValue());
|
String accessToken = jwtUtils.createAccessToken(member.getUserId());
|
||||||
String refreshToken = jwtUtils.createRefreshToken(member.getUserId(), member.getRole().getValue(), httpUtils.getClientIp());
|
String refreshToken = jwtUtils.createRefreshToken(member.getUserId(), httpUtils.getClientIp());
|
||||||
|
|
||||||
member.setRefreshToken(refreshToken);
|
member.setRefreshToken(refreshToken);
|
||||||
member.setLoginIp(httpUtils.getClientIp());
|
member.setLoginIp(httpUtils.getClientIp());
|
||||||
@@ -85,7 +85,6 @@ public class JwtTokenIssuanceFilter extends UsernamePasswordAuthenticationFilter
|
|||||||
|
|
||||||
LoginResponseDto memberData = new LoginResponseDto();
|
LoginResponseDto memberData = new LoginResponseDto();
|
||||||
memberData.setUserId(member.getUserId());
|
memberData.setUserId(member.getUserId());
|
||||||
memberData.setRole(member.getRole().getValue());
|
|
||||||
memberData.setLastLoginAt(member.getLastLoginAt());
|
memberData.setLastLoginAt(member.getLastLoginAt());
|
||||||
|
|
||||||
// login 성공 메시지 전송
|
// login 성공 메시지 전송
|
||||||
|
@@ -42,23 +42,4 @@ public class HttpUtils {
|
|||||||
|
|
||||||
return ip;
|
return ip;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getUri() {
|
|
||||||
HttpServletRequest request =
|
|
||||||
((ServletRequestAttributes)RequestContextHolder.currentRequestAttributes()).getRequest();
|
|
||||||
|
|
||||||
return request.getRequestURI();
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getResponseType(String contentType) {
|
|
||||||
if(contentType == null) {
|
|
||||||
return "";
|
|
||||||
} else if(contentType.contains("text/html")) {
|
|
||||||
return "PAGE";
|
|
||||||
} else if (contentType.contains("application/json")) {
|
|
||||||
return "API";
|
|
||||||
};
|
|
||||||
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@@ -36,10 +36,9 @@ public class JwtUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Token 생성
|
// Token 생성
|
||||||
public String generateToken(String username, String role, long expirationTime) {
|
public String generateToken(String username, long expirationTime) {
|
||||||
return Jwts.builder()
|
return Jwts.builder()
|
||||||
.subject(username)
|
.subject(username)
|
||||||
.claim("role", role)
|
|
||||||
.issuedAt(new Date(System.currentTimeMillis()))
|
.issuedAt(new Date(System.currentTimeMillis()))
|
||||||
.expiration(new Date(System.currentTimeMillis() + expirationTime))
|
.expiration(new Date(System.currentTimeMillis() + expirationTime))
|
||||||
.signWith(getSigningKey())
|
.signWith(getSigningKey())
|
||||||
@@ -47,10 +46,9 @@ public class JwtUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Token 생성(IP 정보 포함)
|
// Token 생성(IP 정보 포함)
|
||||||
public String generateToken(String username, String role, String clientIp, long expirationTime) {
|
public String generateToken(String username, String clientIp, long expirationTime) {
|
||||||
return Jwts.builder()
|
return Jwts.builder()
|
||||||
.subject(username)
|
.subject(username)
|
||||||
.claim("role", role)
|
|
||||||
.claim("ip", clientIp) // IP 정보 추가
|
.claim("ip", clientIp) // IP 정보 추가
|
||||||
.issuedAt(new Date(System.currentTimeMillis()))
|
.issuedAt(new Date(System.currentTimeMillis()))
|
||||||
.expiration(new Date(System.currentTimeMillis() + expirationTime))
|
.expiration(new Date(System.currentTimeMillis() + expirationTime))
|
||||||
@@ -59,15 +57,15 @@ public class JwtUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Access Token 생성
|
// Access Token 생성
|
||||||
public String createAccessToken(String username, String role) {
|
public String createAccessToken(String username) {
|
||||||
long expirationTime = Long.parseLong(Objects.requireNonNull(env.getProperty("token.expiration_time_access")));
|
long expirationTime = Long.parseLong(Objects.requireNonNull(env.getProperty("token.expiration_time_access")));
|
||||||
return generateToken(username, role, expirationTime);
|
return generateToken(username, expirationTime);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Refresh Token 생성 시 IP 정보 포함
|
// Refresh Token 생성 시 IP 정보 포함
|
||||||
public String createRefreshToken(String username, String role, String clientIp) {
|
public String createRefreshToken(String username, String clientIp) {
|
||||||
long expirationTime = Long.parseLong(Objects.requireNonNull(env.getProperty("token.expiration_time_refresh")));
|
long expirationTime = Long.parseLong(Objects.requireNonNull(env.getProperty("token.expiration_time_refresh")));
|
||||||
return generateToken(username, role, clientIp, expirationTime);
|
return generateToken(username, clientIp, expirationTime);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Token 검증
|
// Token 검증
|
||||||
@@ -126,11 +124,7 @@ public class JwtUtils {
|
|||||||
return extractAllClaims(token).getSubject();
|
return extractAllClaims(token).getSubject();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Role 정보 추출
|
|
||||||
public String extractRole(String token) {
|
|
||||||
Claims claims = extractAllClaims(token);
|
|
||||||
return claims.get("role", String.class);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Claims extractAllClaims(String token) {
|
public Claims extractAllClaims(String token) {
|
||||||
return Jwts.parser()
|
return Jwts.parser()
|
||||||
|
@@ -57,12 +57,5 @@ public class SecurityUtils {
|
|||||||
return authentication != null && authentication.isAuthenticated();
|
return authentication != null && authentication.isAuthenticated();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 현재 인증된 사용자의 역할을 반환합니다.
|
|
||||||
* @return 역할 문자열
|
|
||||||
*/
|
|
||||||
public static String getCurrentUserRole() {
|
|
||||||
MemberDto member = getCurrentMember();
|
|
||||||
return member != null ? member.getRole().name() : null;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user