Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

8주차 미션 / 서버 2조 황정안 #15

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/main/java/com/kuit/kuit4serverauth/config/WebConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public WebConfig(AuthInterceptor authInterceptor) {

@Override
public void addInterceptors(InterceptorRegistry registry) {
// TODO /profile, /admin 앞에 붙이기
registry.addInterceptor(authInterceptor)
.addPathPatterns("/profile","/admin");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
public class AuthController {
private final UserRepository userRepository;
private final JwtUtil jwtUtil;
//todo token 구현하기

public AuthController(UserRepository userRepository, JwtUtil jwtUtil) {
this.userRepository = userRepository;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,33 @@
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

@RestController
public class UserController {

@GetMapping("/profile")
public ResponseEntity<String> getProfile(HttpServletRequest request) {
// TODO : 로그인 한 사용자면 username 이용해 "Hello, {username}" 반환하기
String username = (String) request.getAttribute("username");

if (username != null) {
return ResponseEntity.ok("Hello, " + username);
}
//todo Service layer 로 빼서 리팩토링..
// 요청 응답 dto 감싸기
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("Unauthorized");
}

@GetMapping("/admin")
public ResponseEntity<String> getAdmin(HttpServletRequest request) {
// TODO: role이 admin이면 "Hello, admin" 반환하기
String role = (String) request.getAttribute("role");

if (Objects.equals(role, "ROLE_ADMIN")) {
return ResponseEntity.ok("Hello, admin");
}

return ResponseEntity.status(HttpStatus.FORBIDDEN).body("Forbidden");
}
}
5 changes: 3 additions & 2 deletions src/main/java/com/kuit/kuit4serverauth/service/JwtUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.stereotype.Component;

import java.util.Base64;
import java.util.Date;

@Component
public class JwtUtil {
private final String secret = "mysecretkey";
private final String secret = "wenoqwineofnioweniowfnoqwfenoifnewiofqnoinqo";
private final long expirationMs = 3600000; // 1 hour

public String generateToken(String username, String role) {
Expand All @@ -34,4 +35,4 @@ public Claims validateToken(String token) {
throw new CustomException(ErrorCode.INVALID_TOKEN);
}
}
}
}
2 changes: 1 addition & 1 deletion src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ spring:
path: /h2-console

jwt:
secret: mysecretkey
secret: wenoqwineofnioweniowfnoqwfenoifnewiofqnoinqo
expiration: 3600000