diff --git a/src/main/java/com/example/bigbrotherbe/auth/jwt/entity/JwtToken.java b/src/main/java/com/example/bigbrotherbe/auth/jwt/dto/response/JwtToken.java similarity index 80% rename from src/main/java/com/example/bigbrotherbe/auth/jwt/entity/JwtToken.java rename to src/main/java/com/example/bigbrotherbe/auth/jwt/dto/response/JwtToken.java index f50ef86..226a284 100644 --- a/src/main/java/com/example/bigbrotherbe/auth/jwt/entity/JwtToken.java +++ b/src/main/java/com/example/bigbrotherbe/auth/jwt/dto/response/JwtToken.java @@ -1,4 +1,4 @@ -package com.example.bigbrotherbe.auth.jwt.entity; +package com.example.bigbrotherbe.auth.jwt.dto.response; import lombok.AllArgsConstructor; import lombok.Builder; diff --git a/src/main/java/com/example/bigbrotherbe/auth/jwt/service/JwtTokenService.java b/src/main/java/com/example/bigbrotherbe/auth/jwt/service/JwtTokenService.java index 8182306..96c5001 100644 --- a/src/main/java/com/example/bigbrotherbe/auth/jwt/service/JwtTokenService.java +++ b/src/main/java/com/example/bigbrotherbe/auth/jwt/service/JwtTokenService.java @@ -7,7 +7,7 @@ import com.example.bigbrotherbe.common.exception.BusinessException; import com.example.bigbrotherbe.common.exception.enums.ErrorCode; -import com.example.bigbrotherbe.auth.jwt.entity.JwtToken; +import com.example.bigbrotherbe.auth.jwt.dto.response.JwtToken; import com.example.bigbrotherbe.auth.jwt.dto.TokenDto; import com.example.bigbrotherbe.auth.custom.CustomUserDetailsService; import io.jsonwebtoken.Claims; @@ -36,6 +36,7 @@ @Slf4j @Component public class JwtTokenService { + private final Key key; private static final long ACCESS_TIME = 10 * 60 * 1000L; // 10분 private static final long REFRESH_TIME = 30 * 60 * 1000L; //30분 @@ -51,8 +52,9 @@ public JwtTokenService(@Value("${jwt.secret}") String secretKey, CustomUserDetai // Member 정보를 가지고 Access Token, RefreshToken을 생성하는 메서드 public JwtToken generateToken(Authentication authentication) { // 권한 가져오기 - String authorities = authentication.getAuthorities().stream().map(GrantedAuthority::getAuthority).collect( - Collectors.joining(",")); + String authorities = authentication.getAuthorities().stream() + .map(GrantedAuthority::getAuthority) + .collect(Collectors.joining(",")); TokenDto tokenDto = createAllToken(authentication.getName(), authorities); @@ -63,18 +65,6 @@ public JwtToken generateToken(Authentication authentication) { .build(); } - private String createToken(String email, String authorities, String type) { - long now = new Date().getTime(); - long expiration = type.equals("Access") ? ACCESS_TIME : REFRESH_TIME; - return Jwts.builder() - .setSubject(email) - .claim("auth", authorities) - .claim("type", type) - .setExpiration(new Date(now + expiration)) - .signWith(key, SignatureAlgorithm.HS256) - .compact(); - } - public Authentication getAuthentication(String token) { Claims claims = parseClaims(token); log.info("Parsed claims: {}", claims); @@ -82,14 +72,14 @@ public Authentication getAuthentication(String token) { throw new RuntimeException("권한 정보가 없는 토큰입니다."); } UserDetails principal = customerDetailsService.loadUserByUsername( - claims.getSubject()); + claims.getSubject()); return new UsernamePasswordAuthenticationToken(principal, "", principal.getAuthorities()); } public boolean validateToken(String token) { try { - Jwts.parserBuilder() + Jwts.parserBuilder() .setSigningKey(key) .build() .parseClaimsJws(token) @@ -123,18 +113,6 @@ public boolean checkTokenType(String token) { } - private Claims parseClaims(String accessToken) { - try { - return Jwts.parserBuilder() - .setSigningKey(key) - .build() - .parseClaimsJws(accessToken) - .getBody(); - } catch (ExpiredJwtException e) { - return e.getClaims(); - } - } - public String createTokenByRefreshToken(String refreshToken) { Claims claims = getAllClaimsFromToken(refreshToken); Date now = new Date(); @@ -152,13 +130,6 @@ public TokenDto createAllToken(String email, String role) { return new TokenDto(createToken(email, role, ACCESS_TOKEN), createToken(email, role, REFRESH_TOKEN)); } - private Claims getAllClaimsFromToken(String token) { - return Jwts.parserBuilder() - .setSigningKey(key) - .build() - .parseClaimsJws(token) - .getBody(); - } public TokenDto refreshAccessToken(String refreshToken) { @@ -184,4 +155,36 @@ private String resolveToken(String token) { throw new BusinessException(ErrorCode.ILLEGAL_HEADER_PATTERN); } + private String createToken(String email, String authorities, String type) { + long now = new Date().getTime(); + long expiration = type.equals("Access") ? ACCESS_TIME : REFRESH_TIME; + + return Jwts.builder() + .setSubject(email) + .claim("auth", authorities) + .claim("type", type) + .setExpiration(new Date(now + expiration)) + .signWith(key, SignatureAlgorithm.HS256) + .compact(); + } + + private Claims getAllClaimsFromToken(String token) { + return Jwts.parserBuilder() + .setSigningKey(key) + .build() + .parseClaimsJws(token) + .getBody(); + } + + private Claims parseClaims(String accessToken) { + try { + return Jwts.parserBuilder() + .setSigningKey(key) + .build() + .parseClaimsJws(accessToken) + .getBody(); + } catch (ExpiredJwtException e) { + return e.getClaims(); + } + } } diff --git a/src/main/java/com/example/bigbrotherbe/auth/util/AuthUtil.java b/src/main/java/com/example/bigbrotherbe/auth/util/AuthUtil.java index 01f1d3c..a0ea986 100644 --- a/src/main/java/com/example/bigbrotherbe/auth/util/AuthUtil.java +++ b/src/main/java/com/example/bigbrotherbe/auth/util/AuthUtil.java @@ -5,7 +5,7 @@ import com.example.bigbrotherbe.domain.member.repository.AffiliationMemberRepository; import com.example.bigbrotherbe.domain.member.repository.AffiliationRepository; import com.example.bigbrotherbe.domain.member.repository.MemberRepository; -import com.example.bigbrotherbe.auth.jwt.entity.JwtToken; +import com.example.bigbrotherbe.auth.jwt.dto.response.JwtToken; import java.util.List; @@ -90,8 +90,8 @@ public Long getAffiliationIdByMemberId(Long memberId, String affiliation) { public JwtToken createAuthenticationToken(String email, String password) { - UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(email, password); try { + UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(email, password); Authentication authentication = authenticationManagerBuilder.getObject() .authenticate(authenticationToken); return jwtTokenService.generateToken(authentication); diff --git a/src/main/java/com/example/bigbrotherbe/domain/campusNotice/service/CampusNoticeServiceImpl.java b/src/main/java/com/example/bigbrotherbe/domain/campusNotice/service/CampusNoticeServiceImpl.java index 74f03f8..afa1377 100644 --- a/src/main/java/com/example/bigbrotherbe/domain/campusNotice/service/CampusNoticeServiceImpl.java +++ b/src/main/java/com/example/bigbrotherbe/domain/campusNotice/service/CampusNoticeServiceImpl.java @@ -23,6 +23,8 @@ import java.util.ArrayList; import java.util.List; +import static com.example.bigbrotherbe.common.exception.enums.ErrorCode.*; + @Service @RequiredArgsConstructor public class CampusNoticeServiceImpl implements CampusNoticeService { diff --git a/src/main/java/com/example/bigbrotherbe/domain/event/service/EventServiceImpl.java b/src/main/java/com/example/bigbrotherbe/domain/event/service/EventServiceImpl.java index 9710f49..5ac3a99 100644 --- a/src/main/java/com/example/bigbrotherbe/domain/event/service/EventServiceImpl.java +++ b/src/main/java/com/example/bigbrotherbe/domain/event/service/EventServiceImpl.java @@ -26,6 +26,8 @@ import java.util.List; +import static com.example.bigbrotherbe.common.exception.enums.ErrorCode.*; + @Service @RequiredArgsConstructor public class EventServiceImpl implements EventService { diff --git a/src/main/java/com/example/bigbrotherbe/domain/faq/service/FAQServiceImpl.java b/src/main/java/com/example/bigbrotherbe/domain/faq/service/FAQServiceImpl.java index 6f83170..24268f8 100644 --- a/src/main/java/com/example/bigbrotherbe/domain/faq/service/FAQServiceImpl.java +++ b/src/main/java/com/example/bigbrotherbe/domain/faq/service/FAQServiceImpl.java @@ -25,6 +25,8 @@ import java.util.List; +import static com.example.bigbrotherbe.common.exception.enums.ErrorCode.*; + @Service @RequiredArgsConstructor public class FAQServiceImpl implements FAQService { @@ -70,7 +72,7 @@ public void register(FAQRegisterRequest faqRegisterRequest, List @Transactional(rollbackFor = Exception.class) public void modify(Long faqId, FAQModifyRequest faqModifyRequest, List multipartFiles) { FAQ faq = faqRepository.findById(faqId) - .orElseThrow(() -> new BusinessException(ErrorCode.NO_EXIST_FAQ)); + .orElseThrow(() -> new BusinessException(NO_EXIST_FAQ)); if (authUtil.checkCouncilRole(faq.getAffiliationId())) { throw new BusinessException(NOT_COUNCIL_MEMBER); @@ -94,7 +96,7 @@ public void modify(Long faqId, FAQModifyRequest faqModifyRequest, List new BusinessException(ErrorCode.NO_EXIST_FAQ)); + .orElseThrow(() -> new BusinessException(NO_EXIST_FAQ)); if (authUtil.checkCouncilRole(faq.getAffiliationId())) { throw new BusinessException(NOT_COUNCIL_MEMBER); diff --git a/src/main/java/com/example/bigbrotherbe/domain/meetings/service/MeetingsServiceImpl.java b/src/main/java/com/example/bigbrotherbe/domain/meetings/service/MeetingsServiceImpl.java index 1afed48..a933000 100644 --- a/src/main/java/com/example/bigbrotherbe/domain/meetings/service/MeetingsServiceImpl.java +++ b/src/main/java/com/example/bigbrotherbe/domain/meetings/service/MeetingsServiceImpl.java @@ -26,6 +26,8 @@ import java.util.List; +import static com.example.bigbrotherbe.common.exception.enums.ErrorCode.*; + @Service @RequiredArgsConstructor public class MeetingsServiceImpl implements MeetingsService { diff --git a/src/main/java/com/example/bigbrotherbe/domain/member/component/MemberManager.java b/src/main/java/com/example/bigbrotherbe/domain/member/component/MemberManager.java index c384cc5..b05b362 100644 --- a/src/main/java/com/example/bigbrotherbe/domain/member/component/MemberManager.java +++ b/src/main/java/com/example/bigbrotherbe/domain/member/component/MemberManager.java @@ -10,7 +10,7 @@ import com.example.bigbrotherbe.domain.member.dto.AffiliationListDto; import com.example.bigbrotherbe.domain.member.entity.AffiliationMember; import com.example.bigbrotherbe.auth.util.AuthUtil; -import com.example.bigbrotherbe.auth.jwt.entity.JwtToken; +import com.example.bigbrotherbe.auth.jwt.dto.response.JwtToken; import java.util.List; import java.util.stream.Collectors; diff --git a/src/main/java/com/example/bigbrotherbe/domain/member/controller/MemberController.java b/src/main/java/com/example/bigbrotherbe/domain/member/controller/MemberController.java index 7c5c49d..7555e5a 100644 --- a/src/main/java/com/example/bigbrotherbe/domain/member/controller/MemberController.java +++ b/src/main/java/com/example/bigbrotherbe/domain/member/controller/MemberController.java @@ -10,7 +10,7 @@ import com.example.bigbrotherbe.domain.member.dto.AffiliationListDto; import com.example.bigbrotherbe.email.entity.EmailRequest; import com.example.bigbrotherbe.email.entity.EmailVerificationResult; -import com.example.bigbrotherbe.auth.jwt.entity.JwtToken; +import com.example.bigbrotherbe.auth.jwt.dto.response.JwtToken; import com.example.bigbrotherbe.auth.jwt.dto.TokenDto; import com.example.bigbrotherbe.common.config.SecurityConfig; import io.swagger.v3.oas.annotations.Operation; diff --git a/src/main/java/com/example/bigbrotherbe/domain/member/controller/MemberControllerImpl.java b/src/main/java/com/example/bigbrotherbe/domain/member/controller/MemberControllerImpl.java index b27fe97..36c81e8 100644 --- a/src/main/java/com/example/bigbrotherbe/domain/member/controller/MemberControllerImpl.java +++ b/src/main/java/com/example/bigbrotherbe/domain/member/controller/MemberControllerImpl.java @@ -13,7 +13,7 @@ import com.example.bigbrotherbe.email.entity.EmailVerificationResult; import com.example.bigbrotherbe.email.component.MailService; import com.example.bigbrotherbe.common.exception.response.ApiResponse; -import com.example.bigbrotherbe.auth.jwt.entity.JwtToken; +import com.example.bigbrotherbe.auth.jwt.dto.response.JwtToken; import com.example.bigbrotherbe.domain.member.service.MemberService; import com.example.bigbrotherbe.auth.jwt.service.JwtTokenService; diff --git a/src/main/java/com/example/bigbrotherbe/domain/member/service/MemberService.java b/src/main/java/com/example/bigbrotherbe/domain/member/service/MemberService.java index cdc2d54..def7fe3 100644 --- a/src/main/java/com/example/bigbrotherbe/domain/member/service/MemberService.java +++ b/src/main/java/com/example/bigbrotherbe/domain/member/service/MemberService.java @@ -4,7 +4,7 @@ import com.example.bigbrotherbe.domain.member.dto.response.MemberInfoResponse; import com.example.bigbrotherbe.domain.member.dto.response.MemberResponse; import com.example.bigbrotherbe.domain.member.dto.AffiliationListDto; -import com.example.bigbrotherbe.auth.jwt.entity.JwtToken; +import com.example.bigbrotherbe.auth.jwt.dto.response.JwtToken; import java.util.List; diff --git a/src/main/java/com/example/bigbrotherbe/domain/member/service/MemberServiceImpl.java b/src/main/java/com/example/bigbrotherbe/domain/member/service/MemberServiceImpl.java index bbae49f..ce825d9 100644 --- a/src/main/java/com/example/bigbrotherbe/domain/member/service/MemberServiceImpl.java +++ b/src/main/java/com/example/bigbrotherbe/domain/member/service/MemberServiceImpl.java @@ -6,7 +6,7 @@ import com.example.bigbrotherbe.domain.member.dto.response.MemberInfoResponse; import com.example.bigbrotherbe.domain.member.dto.response.MemberResponse; import com.example.bigbrotherbe.domain.member.dto.AffiliationListDto; -import com.example.bigbrotherbe.auth.jwt.entity.JwtToken; +import com.example.bigbrotherbe.auth.jwt.dto.response.JwtToken; import com.example.bigbrotherbe.domain.member.entity.Member; diff --git a/src/main/java/com/example/bigbrotherbe/domain/notice/service/NoticeServiceImpl.java b/src/main/java/com/example/bigbrotherbe/domain/notice/service/NoticeServiceImpl.java index 2e889ec..dd11918 100644 --- a/src/main/java/com/example/bigbrotherbe/domain/notice/service/NoticeServiceImpl.java +++ b/src/main/java/com/example/bigbrotherbe/domain/notice/service/NoticeServiceImpl.java @@ -26,6 +26,8 @@ import java.util.List; +import static com.example.bigbrotherbe.common.exception.enums.ErrorCode.*; + @Service @RequiredArgsConstructor public class NoticeServiceImpl implements NoticeService { @@ -72,7 +74,7 @@ public void register(NoticeRegisterRequest noticeRegisterRequest, List multipartFiles) { Notice notice = noticeRepository.findById(noticeId) - .orElseThrow(() -> new BusinessException(ErrorCode.NO_EXIST_NOTICE)); + .orElseThrow(() -> new BusinessException(NO_EXIST_NOTICE)); if (authUtil.checkCouncilRole(notice.getAffiliationId())) { throw new BusinessException(NOT_COUNCIL_MEMBER); @@ -96,7 +98,7 @@ public void modify(Long noticeId, NoticeModifyRequest noticeModifyRequest, List< @Transactional(rollbackFor = Exception.class) public void delete(Long noticeId) { Notice notice = noticeRepository.findById(noticeId) - .orElseThrow(() -> new BusinessException(ErrorCode.NO_EXIST_NOTICE)); + .orElseThrow(() -> new BusinessException(NO_EXIST_NOTICE)); if (authUtil.checkCouncilRole(notice.getAffiliationId())) { throw new BusinessException(NOT_COUNCIL_MEMBER); diff --git a/src/main/java/com/example/bigbrotherbe/domain/rule/service/RuleServiceImpl.java b/src/main/java/com/example/bigbrotherbe/domain/rule/service/RuleServiceImpl.java index 1f8cdda..98ab31a 100644 --- a/src/main/java/com/example/bigbrotherbe/domain/rule/service/RuleServiceImpl.java +++ b/src/main/java/com/example/bigbrotherbe/domain/rule/service/RuleServiceImpl.java @@ -26,6 +26,8 @@ import java.util.List; +import static com.example.bigbrotherbe.common.exception.enums.ErrorCode.*; + @Service @RequiredArgsConstructor public class RuleServiceImpl implements RuleService { diff --git a/src/main/java/com/example/bigbrotherbe/domain/transactions/service/TransactionsServiceImpl.java b/src/main/java/com/example/bigbrotherbe/domain/transactions/service/TransactionsServiceImpl.java index d60b4d5..e7504e8 100644 --- a/src/main/java/com/example/bigbrotherbe/domain/transactions/service/TransactionsServiceImpl.java +++ b/src/main/java/com/example/bigbrotherbe/domain/transactions/service/TransactionsServiceImpl.java @@ -26,6 +26,8 @@ import java.util.List; import java.util.stream.Collectors; +import static com.example.bigbrotherbe.common.exception.enums.ErrorCode.*; + @Service @RequiredArgsConstructor