-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
feat: LoginUser 정상 호출
- Loading branch information
Showing
10 changed files
with
214 additions
and
19 deletions.
There are no files selected for viewing
3 changes: 3 additions & 0 deletions
3
src/main/java/com/unit/daybook/domain/auth/service/AuthService.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
3 changes: 3 additions & 0 deletions
3
src/main/java/com/unit/daybook/domain/auth/service/AuthTokensGenerator.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
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
Empty file.
28 changes: 28 additions & 0 deletions
28
src/main/java/com/unit/daybook/domain/member/controller/MemberController.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,28 @@ | ||
package com.unit.daybook.domain.member.controller; | ||
|
||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import com.unit.daybook.domain.common.annotation.LoginUsers; | ||
import com.unit.daybook.domain.member.domain.Member; | ||
import com.unit.daybook.domain.member.service.MemberService; | ||
import com.unit.daybook.global.config.security.CustomUserDetails; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/member") | ||
public class MemberController { | ||
|
||
private final MemberService memberService; | ||
|
||
@GetMapping | ||
public Member memberFindOne( | ||
@LoginUsers CustomUserDetails userDetails | ||
) { | ||
return memberService.findOneMember(userDetails.getMemberId()); | ||
} | ||
|
||
} |
26 changes: 26 additions & 0 deletions
26
src/main/java/com/unit/daybook/domain/member/service/MemberService.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,26 @@ | ||
package com.unit.daybook.domain.member.service; | ||
|
||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import com.unit.daybook.domain.member.domain.Member; | ||
import com.unit.daybook.domain.member.repository.MemberRepository; | ||
import com.unit.daybook.global.config.security.CustomUserDetails; | ||
import com.unit.daybook.global.error.exception.CustomException; | ||
import com.unit.daybook.global.error.exception.ErrorCode; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
@Transactional | ||
public class MemberService { | ||
|
||
private final MemberRepository memberRepository; | ||
|
||
@Transactional(readOnly = true) | ||
public Member findOneMember(Long memberId) { | ||
return memberRepository.findById(memberId) | ||
.orElseThrow(() -> new CustomException(ErrorCode.MEMBER_NOT_FOUND)); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/main/java/com/unit/daybook/global/config/WebMvcConfig.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,36 @@ | ||
package com.unit.daybook.global.config; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.http.HttpMethod; | ||
import org.springframework.web.method.support.HandlerMethodArgumentResolver; | ||
import org.springframework.web.servlet.config.annotation.CorsRegistry; | ||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; | ||
|
||
import com.unit.daybook.global.config.resolver.LoginUserDetailsResolver; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
|
||
@Configuration | ||
@RequiredArgsConstructor | ||
public class WebMvcConfig implements WebMvcConfigurer { | ||
|
||
private final LoginUserDetailsResolver loginUserDetailsResolver; | ||
|
||
@Override | ||
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) { | ||
argumentResolvers.add(loginUserDetailsResolver); | ||
} | ||
|
||
@Override | ||
public void addCorsMappings(CorsRegistry registry) { | ||
registry.addMapping("/**") | ||
.allowedOrigins("*") | ||
.allowedMethods(Arrays.stream(HttpMethod.values()) | ||
.map(HttpMethod::name) | ||
.toArray(String[]::new)) | ||
.allowedHeaders("*"); | ||
} | ||
} |
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
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
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,91 @@ | ||
package com.unit.daybook.global.util; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
import java.security.Key; | ||
import java.util.Base64; | ||
import java.util.Map; | ||
|
||
import javax.crypto.SecretKey; | ||
|
||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Component; | ||
|
||
import io.jsonwebtoken.Claims; | ||
import io.jsonwebtoken.Jws; | ||
import io.jsonwebtoken.JwsHeader; | ||
import io.jsonwebtoken.Jwts; | ||
import io.jsonwebtoken.SignatureAlgorithm; | ||
import io.jsonwebtoken.security.Keys; | ||
|
||
@Component | ||
public class JwtUtil { | ||
|
||
@Value("${util.jwt.secretKey}") | ||
private String jwtSecretKey; | ||
|
||
@Value("${util.jwt.refreshKey}") | ||
private String jwtReFreshKey; | ||
private byte[] secretBytes; | ||
private byte[] refreshBytes; | ||
|
||
/** | ||
* JWT 생성 | ||
* | ||
* @param claims | ||
* @return | ||
*/ | ||
public String generateToken(Map<String, Object> claims) { | ||
return Jwts.builder() | ||
.setClaims(claims) | ||
.signWith(SignatureAlgorithm.HS256, secretBytes) | ||
.compact(); | ||
} | ||
|
||
public String generateRefreshToken(Map<String, Object> claims) { | ||
return Jwts.builder() | ||
.setClaims(claims) | ||
.signWith(SignatureAlgorithm.HS256, refreshBytes) | ||
.compact(); | ||
} | ||
|
||
public String generateToken(Map<String, Object> header, Map<String, Object> claims, Key key) { | ||
return Jwts.builder() | ||
.setHeader(header) | ||
.setClaims(claims) | ||
.signWith(SignatureAlgorithm.valueOf(header.get(JwsHeader.ALGORITHM).toString()), key) | ||
.compact(); | ||
} | ||
|
||
/** | ||
* JWT 검증 및 데이터 가져오기 | ||
* | ||
* @param jwt | ||
* @return | ||
*/ | ||
public Map<String, Object> getClaims(String jwt) { | ||
try { | ||
if (jwt == null || jwt.isEmpty()) { | ||
return null; | ||
} | ||
Claims claims = Jwts.parser() | ||
.setSigningKey(jwtSecretKey) | ||
.parseClaimsJws(jwt) | ||
.getBody(); | ||
return claims; | ||
} catch (Exception e) { | ||
// 예외 처리 | ||
e.printStackTrace(); | ||
return null; | ||
} | ||
} | ||
|
||
public Map<String, Object> getClaimsForReFresh(String jwt) { | ||
if (jwt == null || jwt.isEmpty()) { | ||
return null; | ||
} | ||
return Jwts.parser() | ||
.setSigningKey(jwtReFreshKey) | ||
.parseClaimsJws(jwt) | ||
.getBody(); | ||
} | ||
} |