-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🛠️Feat #23: [Security] User Security 완성
🛠️Feat #23: [Security] User Security 완성
- Loading branch information
Showing
18 changed files
with
633 additions
and
21 deletions.
There are no files selected for viewing
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,12 @@ | ||
package com.umc.DongnaeFriend.config; | ||
|
||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
public class JwtConfig { | ||
|
||
@Value("${jwt.secret-key}") | ||
public String SECRET_KEY; | ||
|
||
} |
33 changes: 33 additions & 0 deletions
33
src/main/java/com/umc/DongnaeFriend/config/SecurityConfig.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,33 @@ | ||
package com.umc.DongnaeFriend.config; | ||
|
||
|
||
import com.umc.DongnaeFriend.global.security.JwtTokenFilter; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.security.config.annotation.web.builders.HttpSecurity; | ||
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; | ||
import org.springframework.security.config.http.SessionCreationPolicy; | ||
|
||
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; | ||
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; | ||
|
||
@EnableWebSecurity | ||
public class SecurityConfig extends WebSecurityConfigurerAdapter { | ||
|
||
@Autowired | ||
private JwtTokenFilter jwtTokenFilter; | ||
|
||
|
||
@Override | ||
protected void configure(HttpSecurity http) throws Exception { | ||
http.csrf().disable() | ||
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS) | ||
.and() | ||
.authorizeRequests() | ||
.antMatchers("/user/login").permitAll() // 인증 없이 접근 허용하는 URL | ||
.antMatchers("/user/reissuance").permitAll() // 인증 없이 접근 허용하는 URL | ||
.anyRequest().authenticated(); // 그 외의 URL은 인증 필요 | ||
http.addFilterBefore(jwtTokenFilter, UsernamePasswordAuthenticationFilter.class); | ||
} | ||
|
||
// 나머지 코드는 이전 예제와 동일 | ||
} |
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
80 changes: 80 additions & 0 deletions
80
src/main/java/com/umc/DongnaeFriend/domain/user/contorller/UserController.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,80 @@ | ||
package com.umc.DongnaeFriend.domain.user.contorller; | ||
|
||
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 com.umc.DongnaeFriend.global.exception.CustomException; | ||
import com.umc.DongnaeFriend.global.exception.ErrorCode; | ||
import com.umc.DongnaeFriend.global.util.JwtTokenProvider; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.data.repository.query.Param; | ||
import org.springframework.http.*; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import java.io.IOException; | ||
import java.util.HashMap; | ||
|
||
@RestController | ||
@RequestMapping("/user") | ||
@Slf4j | ||
public class UserController { | ||
|
||
@Autowired | ||
KakaoService kakaoService; | ||
|
||
@Autowired | ||
UserService userService; | ||
|
||
@Autowired | ||
JwtTokenProvider jwtTokenProvider; | ||
|
||
|
||
|
||
/** | ||
* 유저 로그인 / 회원가입 | ||
* 인증 절차 | ||
*/ | ||
@PostMapping("/login") | ||
public ResponseEntity<?> userLogin(@RequestParam("accessToken") String accessToken, HttpServletRequest request, HttpServletResponse httpServletResponse) { | ||
log.info("LoginController 진입"); | ||
|
||
// if (!type.equals("kakao")) { | ||
// throw new CustomException(ErrorCode.SERVER_ERROR); | ||
// } | ||
|
||
|
||
try { | ||
log.info("userLogin 진입"); | ||
//사용자 정보 가져오기 | ||
HashMap<String, Object> userInfo = kakaoService.getUserInfo(accessToken); | ||
|
||
//사용자 확인 기존 회원 -> 넘어가고, 없는 회원 -> 회원가입 | ||
|
||
UserDto.Response response = userService.userValidation(userInfo); | ||
|
||
return ResponseEntity.ok(response); | ||
|
||
} catch (IOException e) { | ||
throw new CustomException(ErrorCode.INVALID_AUTH_TOKEN); | ||
} | ||
} | ||
|
||
@PostMapping("/user/reissuance") | ||
public ResponseEntity<?> reiussnaceToken(String refreshToken) { | ||
try { | ||
|
||
//토큰 재발급 | ||
String access_token = userService.createAccessTokenFromRefreshToken(refreshToken); | ||
return ResponseEntity.ok(access_token); | ||
} catch (Exception e) { | ||
// RefreshToken만료 | ||
throw new CustomException(ErrorCode.INVALID_REFRESH_TOKEN); | ||
} | ||
} | ||
|
||
|
||
|
||
} |
42 changes: 42 additions & 0 deletions
42
src/main/java/com/umc/DongnaeFriend/domain/user/dto/UserDto.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,42 @@ | ||
package com.umc.DongnaeFriend.domain.user.dto; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
|
||
public class UserDto { | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
public static class Request { | ||
|
||
String accessToken; | ||
|
||
String type; | ||
|
||
} | ||
|
||
@Getter | ||
@Builder | ||
@AllArgsConstructor | ||
public static class Response { | ||
|
||
String accessToken; | ||
|
||
String refreshToken; | ||
|
||
} | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
public static class SignUpDto { | ||
|
||
String nickName; | ||
|
||
String email; | ||
|
||
Long kakaoId; | ||
|
||
} | ||
|
||
} |
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
16 changes: 16 additions & 0 deletions
16
src/main/java/com/umc/DongnaeFriend/domain/user/service/KakaoService.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,16 @@ | ||
package com.umc.DongnaeFriend.domain.user.service; | ||
|
||
|
||
import org.springframework.beans.factory.annotation.Value; | ||
|
||
import java.io.IOException; | ||
import java.util.HashMap; | ||
|
||
public interface KakaoService { | ||
|
||
|
||
@SuppressWarnings("unchecked") | ||
HashMap<String, Object> getUserInfo(String access_Token) throws IOException; | ||
} | ||
|
||
|
81 changes: 81 additions & 0 deletions
81
src/main/java/com/umc/DongnaeFriend/domain/user/service/KakaoServiceimpl.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,81 @@ | ||
package com.umc.DongnaeFriend.domain.user.service; | ||
|
||
import com.fasterxml.jackson.core.type.TypeReference; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Service; | ||
|
||
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 | ||
@Service | ||
public class KakaoServiceimpl implements KakaoService { | ||
|
||
// @Autowired | ||
// public IACDao dao; | ||
|
||
@SuppressWarnings("unchecked") | ||
@Override | ||
public HashMap<String, Object> getUserInfo(String access_Token) throws IOException { | ||
// 클라이언트 요청 정보 | ||
HashMap<String, Object> userInfo = new HashMap<String, Object>(); | ||
|
||
|
||
//------kakao GET 요청------ | ||
String reqURL = "https://kapi.kakao.com/v2/user/me"; | ||
URL url = new URL(reqURL); | ||
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); | ||
conn.setRequestMethod("GET"); | ||
conn.setRequestProperty("Authorization", "Bearer " + access_Token); | ||
|
||
int responseCode = conn.getResponseCode(); | ||
System.out.println("responseCode : " + responseCode); | ||
|
||
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream())); | ||
|
||
String line = ""; | ||
String result = ""; | ||
|
||
while ((line = br.readLine()) != null) { | ||
result += line; | ||
} | ||
System.out.println("response body : " + result); | ||
System.out.println("result type" + result.getClass().getName()); // java.lang.String | ||
|
||
// jackson objectmapper 객체 생성 | ||
ObjectMapper objectMapper = new ObjectMapper(); | ||
// JSON String -> Map | ||
Map<String, Object> jsonMap = objectMapper.readValue(result, new TypeReference<Map<String, Object>>() { | ||
}); | ||
|
||
|
||
System.out.println(jsonMap.get("properties")); | ||
|
||
Long id = (Long) jsonMap.get("id"); | ||
Map<String, Object> properties = (Map<String, Object>) jsonMap.get("properties"); | ||
Map<String, Object> kakao_account = (Map<String, Object>) jsonMap.get("kakao_account"); | ||
Map<String, Object> profile = (Map<String, Object>) kakao_account.get("profile"); | ||
|
||
log.info("profile : " + profile.toString()); | ||
log.info("kakao_acount : " + kakao_account.toString()); | ||
|
||
String nickname = properties.get("nickname").toString(); | ||
String profileImage = properties.get("profile_image").toString(); | ||
String email = kakao_account.get("email").toString(); | ||
|
||
userInfo.put("id", id); | ||
userInfo.put("nickname", nickname); | ||
userInfo.put("profileImage", profileImage); | ||
userInfo.put("email", email); | ||
|
||
|
||
return userInfo; | ||
} | ||
|
||
} |
Oops, something went wrong.