Skip to content

Commit

Permalink
🛠️Feat #39: [임시] 토큰 추출기
Browse files Browse the repository at this point in the history
🛠️Feat #39: [임시] 토큰 추출기
  • Loading branch information
DDonghyeo authored Jul 28, 2023
2 parents 430d5b1 + 93d3152 commit 19cd7d2
Show file tree
Hide file tree
Showing 15 changed files with 114 additions and 6 deletions.
Binary file added .DS_Store
Binary file not shown.
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ dependencies {
//jwt
implementation 'io.jsonwebtoken:jjwt:0.9.1'

//thymeleaf
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'


}

Expand Down
Binary file added src/.DS_Store
Binary file not shown.
Binary file added src/main/.DS_Store
Binary file not shown.
79 changes: 79 additions & 0 deletions src/main/java/com/umc/DongnaeFriend/KakaoTokenController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package com.umc.DongnaeFriend;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.umc.DongnaeFriend.domain.user.dto.UserDto;
import com.umc.DongnaeFriend.domain.user.service.KakaoService;
import com.umc.DongnaeFriend.domain.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Controller
@RequestMapping("")
public class KakaoTokenController {

@Autowired
private UserService userService;

@Autowired
private KakaoService kakaoService;

@GetMapping("/kakao")
public String kakologin(Model model, HttpServletResponse response) {
response.setContentType(MediaType.TEXT_HTML_VALUE);

return "html/index";
}

@GetMapping("/callback")
public String callback(Model model, @RequestParam("code") String code) throws IOException {

//------kakao POST 요청------
String reqURL = "https://kauth.kakao.com/oauth/token?grant_type=authorization_code&client_id=8427ba9114a5ecb09621710469748441&code=" + code;
URL url = new URL(reqURL);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");


BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));

String line = "";
String result = "";

while ((line = br.readLine()) != null) {
result += line;
}

ObjectMapper objectMapper = new ObjectMapper();
Map<String, Object> jsonMap = objectMapper.readValue(result, new TypeReference<Map<String, Object>>() {
});

String accessToken = (String) jsonMap.get("access_token");

//-------------------------------------------------서버 로그인----------------------------------------------------

HashMap<String, Object> userInfo = kakaoService.getUserInfo(accessToken);
UserDto.Response response = userService.userValidation(userInfo);

model.addAttribute("token","Bearer "+ response.getAccessToken());

return "html/token";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ protected void configure(HttpSecurity http) throws Exception {
.authorizeRequests()
.antMatchers("/user/login").permitAll() // 인증 없이 접근 허용하는 URL
.antMatchers("/user/reissuance").permitAll() // 인증 없이 접근 허용하는 URL
.antMatchers("/kakao").permitAll() // 카카오 토큰 추출(임시)
.antMatchers("/callback").permitAll() // 카카오 토큰 추출(임시)
.anyRequest().authenticated(); // 그 외의 URL은 인증 필요
http.addFilterBefore(jwtTokenFilter, UsernamePasswordAuthenticationFilter.class);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse

log.info("JwtTokenFilter 진입");

if (request.getServletPath().contains("/user/login")) {
log.info("/user/login 진입");
}

// Request Header에서 JWT 토큰 가져오기
String authorizationHeader = request.getHeader("Authorization");
log.info("authorizationHeader : {}",authorizationHeader);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public String createAccessToken(Long userId) {
// claims.put("userId", user.getId()); // 사용자 아이디
// claims.put("email", user.getEmail()); // 사용자 이메일


return Jwts.builder()
.signWith(SignatureAlgorithm.HS512, String.valueOf(jwtConfig.SECRET_KEY))
.claim("userId", userId)
Expand Down
Binary file added src/main/resources/.DS_Store
Binary file not shown.
6 changes: 4 additions & 2 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@ logging:
spring:
datasource:
url: jdbc:mysql://localhost:3306/dongnae?characterEncoding=UTF-8&serverTimezone=UTC&useLegacyDatetimeCode=false
username: dongnae
password: Tnqls9004^^
username: root
password: qwe335577!
driver-class-name: com.mysql.cj.jdbc.Driver
jpa:
hibernate.ddl-auto: update
properties:
hibernate:
format_sql: true
show_sql: true
thymeleaf:
cache: false

jwt:
secret-key: 6B64DCA4EA2F53EDIKU9AAB215FE7
Binary file added src/main/resources/templates/.DS_Store
Binary file not shown.
Binary file added src/main/resources/templates/html/.DS_Store
Binary file not shown.
15 changes: 15 additions & 0 deletions src/main/resources/templates/html/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!DOCTYPE html>
<html>
<head>
<title>My Page</title>
</head>
<body>
<div class="wrapper" style="display: flex; flex-direction: column">
<h1>동네친구 카카오 로그인</h1>
<a href="https://kauth.kakao.com/oauth/authorize?response_type=code&client_id=8427ba9114a5ecb09621710469748441&redirect_uri=http://localhost:8080/callback">
<img src="https://developers.kakao.com/docs/static/image/ko/pc/kakaologin.png" alt="kakoLogin" style="cursor: pointer; width: 400px; height: 200px;">
</a>

</div>
</body>
</html>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions src/main/resources/templates/html/token.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Access Token</title>
</head>
<body>
<h1 th:text="${token}">ERROR...</h1>
</body>
</html>

0 comments on commit 19cd7d2

Please sign in to comment.