Skip to content

Commit

Permalink
[refactor] : 패키징 구조 변경, 패키지 import
Browse files Browse the repository at this point in the history
  • Loading branch information
TaetaetaE01 committed Oct 3, 2024
1 parent 3eb18e3 commit 73d6e67
Show file tree
Hide file tree
Showing 15 changed files with 65 additions and 48 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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분
Expand All @@ -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);

Expand All @@ -63,33 +65,21 @@ 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);
if (claims.get("auth") == null) {
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)
Expand Down Expand Up @@ -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();
Expand All @@ -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) {

Expand All @@ -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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

import java.util.List;

import static com.example.bigbrotherbe.common.exception.enums.ErrorCode.*;

@Service
@RequiredArgsConstructor
public class EventServiceImpl implements EventService {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

import java.util.List;

import static com.example.bigbrotherbe.common.exception.enums.ErrorCode.*;

@Service
@RequiredArgsConstructor
public class FAQServiceImpl implements FAQService {
Expand Down Expand Up @@ -70,7 +72,7 @@ public void register(FAQRegisterRequest faqRegisterRequest, List<MultipartFile>
@Transactional(rollbackFor = Exception.class)
public void modify(Long faqId, FAQModifyRequest faqModifyRequest, List<MultipartFile> 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);
Expand All @@ -94,7 +96,7 @@ public void modify(Long faqId, FAQModifyRequest faqModifyRequest, List<Multipart
@Transactional(rollbackFor = Exception.class)
public void delete(Long faqId) {
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

import java.util.List;

import static com.example.bigbrotherbe.common.exception.enums.ErrorCode.*;

@Service
@RequiredArgsConstructor
public class MeetingsServiceImpl implements MeetingsService {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

import java.util.List;

import static com.example.bigbrotherbe.common.exception.enums.ErrorCode.*;

@Service
@RequiredArgsConstructor
public class NoticeServiceImpl implements NoticeService {
Expand Down Expand Up @@ -72,7 +74,7 @@ public void register(NoticeRegisterRequest noticeRegisterRequest, List<Multipart
@Transactional(rollbackFor = Exception.class)
public void modify(Long noticeId, NoticeModifyRequest noticeModifyRequest, List<MultipartFile> 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);
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

import java.util.List;

import static com.example.bigbrotherbe.common.exception.enums.ErrorCode.*;

@Service
@RequiredArgsConstructor
public class RuleServiceImpl implements RuleService {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import java.util.List;
import java.util.stream.Collectors;

import static com.example.bigbrotherbe.common.exception.enums.ErrorCode.*;


@Service
@RequiredArgsConstructor
Expand Down

0 comments on commit 73d6e67

Please sign in to comment.