From c6f12cefe2b1a765eb5531795f1b40538a67f3fc Mon Sep 17 00:00:00 2001 From: mr8356 Date: Thu, 21 Nov 2024 21:15:41 +0900 Subject: [PATCH] compl week8 --- .../java/com/kuit/kuit4serverauth/config/WebConfig.java | 3 ++- .../kuit/kuit4serverauth/controller/UserController.java | 7 ++++++- .../java/com/kuit/kuit4serverauth/service/JwtUtil.java | 2 +- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/kuit/kuit4serverauth/config/WebConfig.java b/src/main/java/com/kuit/kuit4serverauth/config/WebConfig.java index a0b47b4..dd758f8 100644 --- a/src/main/java/com/kuit/kuit4serverauth/config/WebConfig.java +++ b/src/main/java/com/kuit/kuit4serverauth/config/WebConfig.java @@ -15,6 +15,7 @@ public WebConfig(AuthInterceptor authInterceptor) { @Override public void addInterceptors(InterceptorRegistry registry) { - // TODO /profile, /admin 앞에 붙이기 + registry.addInterceptor(authInterceptor) + .addPathPatterns("/profile", "/admin"); } } diff --git a/src/main/java/com/kuit/kuit4serverauth/controller/UserController.java b/src/main/java/com/kuit/kuit4serverauth/controller/UserController.java index 18cb7af..1d683de 100644 --- a/src/main/java/com/kuit/kuit4serverauth/controller/UserController.java +++ b/src/main/java/com/kuit/kuit4serverauth/controller/UserController.java @@ -12,12 +12,17 @@ public class UserController { @GetMapping("/profile") public ResponseEntity getProfile(HttpServletRequest request) { // TODO : 로그인 한 사용자면 username 이용해 "Hello, {username}" 반환하기 - return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("Unauthorized"); + String username = (String) request.getAttribute("username"); + return ResponseEntity.ok("Hello, " + username); } @GetMapping("/admin") public ResponseEntity getAdmin(HttpServletRequest request) { // TODO: role이 admin이면 "Hello, admin" 반환하기 + String role = (String) request.getAttribute("role"); + if ("ROLE_ADMIN".equals(role)) { + return ResponseEntity.ok("Hello, admin"); + } return ResponseEntity.status(HttpStatus.FORBIDDEN).body("Forbidden"); } } diff --git a/src/main/java/com/kuit/kuit4serverauth/service/JwtUtil.java b/src/main/java/com/kuit/kuit4serverauth/service/JwtUtil.java index ead240e..9ba1ee3 100644 --- a/src/main/java/com/kuit/kuit4serverauth/service/JwtUtil.java +++ b/src/main/java/com/kuit/kuit4serverauth/service/JwtUtil.java @@ -11,7 +11,7 @@ @Component public class JwtUtil { - private final String secret = "mysecretkey"; + private final String secret = "mysecretkeydkjfdkfjlsdkjflsdkjfsdkjflksdjflskdjflskdjflsdkjflsdkjflsdkjflsdkjflsdkjflsdkjflsdkjflsdkjflsdkjflsdkjflsdkfjskdjflskdjflskdjfsldkjflskdf"; private final long expirationMs = 3600000; // 1 hour public String generateToken(String username, String role) {