diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..bb91921 Binary files /dev/null and b/.DS_Store differ diff --git a/build.gradle b/build.gradle index fa99fb7..9e6c099 100644 --- a/build.gradle +++ b/build.gradle @@ -45,6 +45,9 @@ dependencies { //jwt implementation 'io.jsonwebtoken:jjwt:0.9.1' + //thymeleaf + implementation 'org.springframework.boot:spring-boot-starter-thymeleaf' + } diff --git a/src/.DS_Store b/src/.DS_Store new file mode 100644 index 0000000..0f989cb Binary files /dev/null and b/src/.DS_Store differ diff --git a/src/main/.DS_Store b/src/main/.DS_Store new file mode 100644 index 0000000..60811ad Binary files /dev/null and b/src/main/.DS_Store differ diff --git a/src/main/java/com/umc/DongnaeFriend/KakaoTokenController.java b/src/main/java/com/umc/DongnaeFriend/KakaoTokenController.java new file mode 100644 index 0000000..4998903 --- /dev/null +++ b/src/main/java/com/umc/DongnaeFriend/KakaoTokenController.java @@ -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 jsonMap = objectMapper.readValue(result, new TypeReference>() { + }); + + String accessToken = (String) jsonMap.get("access_token"); + + //-------------------------------------------------서버 로그인---------------------------------------------------- + + HashMap userInfo = kakaoService.getUserInfo(accessToken); + UserDto.Response response = userService.userValidation(userInfo); + + model.addAttribute("token","Bearer "+ response.getAccessToken()); + + return "html/token"; + } + +} diff --git a/src/main/java/com/umc/DongnaeFriend/config/SecurityConfig.java b/src/main/java/com/umc/DongnaeFriend/config/SecurityConfig.java index 865fbbf..0f9a562 100644 --- a/src/main/java/com/umc/DongnaeFriend/config/SecurityConfig.java +++ b/src/main/java/com/umc/DongnaeFriend/config/SecurityConfig.java @@ -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); } diff --git a/src/main/java/com/umc/DongnaeFriend/global/security/JwtTokenFilter.java b/src/main/java/com/umc/DongnaeFriend/global/security/JwtTokenFilter.java index 6c0cb55..edb3311 100644 --- a/src/main/java/com/umc/DongnaeFriend/global/security/JwtTokenFilter.java +++ b/src/main/java/com/umc/DongnaeFriend/global/security/JwtTokenFilter.java @@ -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); diff --git a/src/main/java/com/umc/DongnaeFriend/global/util/JwtTokenProvider.java b/src/main/java/com/umc/DongnaeFriend/global/util/JwtTokenProvider.java index 344ba00..af5c405 100644 --- a/src/main/java/com/umc/DongnaeFriend/global/util/JwtTokenProvider.java +++ b/src/main/java/com/umc/DongnaeFriend/global/util/JwtTokenProvider.java @@ -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) diff --git a/src/main/resources/.DS_Store b/src/main/resources/.DS_Store new file mode 100644 index 0000000..aec3550 Binary files /dev/null and b/src/main/resources/.DS_Store differ diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 6b69a6d..a2271fb 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -8,8 +8,8 @@ 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 @@ -17,6 +17,8 @@ spring: hibernate: format_sql: true show_sql: true + thymeleaf: + cache: false jwt: secret-key: 6B64DCA4EA2F53EDIKU9AAB215FE7 diff --git a/src/main/resources/templates/.DS_Store b/src/main/resources/templates/.DS_Store new file mode 100644 index 0000000..3c68b2b Binary files /dev/null and b/src/main/resources/templates/.DS_Store differ diff --git a/src/main/resources/templates/html/.DS_Store b/src/main/resources/templates/html/.DS_Store new file mode 100644 index 0000000..5008ddf Binary files /dev/null and b/src/main/resources/templates/html/.DS_Store differ diff --git a/src/main/resources/templates/html/index.html b/src/main/resources/templates/html/index.html new file mode 100644 index 0000000..26d4b25 --- /dev/null +++ b/src/main/resources/templates/html/index.html @@ -0,0 +1,15 @@ + + + + My Page + + +
+

동네친구 카카오 로그인

+ + kakoLogin + + +
+ + diff --git a/src/main/resources/templates/html/kakao_login_large_wide.png b/src/main/resources/templates/html/kakao_login_large_wide.png new file mode 100644 index 0000000..c0c1856 Binary files /dev/null and b/src/main/resources/templates/html/kakao_login_large_wide.png differ diff --git a/src/main/resources/templates/html/token.html b/src/main/resources/templates/html/token.html new file mode 100644 index 0000000..10b3429 --- /dev/null +++ b/src/main/resources/templates/html/token.html @@ -0,0 +1,10 @@ + + + + + Access Token + + +

ERROR...

+ + \ No newline at end of file