diff --git a/user/build.gradle b/user/build.gradle index 04a6e7f..97bf19e 100644 --- a/user/build.gradle +++ b/user/build.gradle @@ -40,6 +40,9 @@ dependencies { implementation 'mysql:mysql-connector-java:8.0.31' //추가 implementation 'org.springframework.boot:spring-boot-starter-oauth2-client' + //추가 + implementation 'io.jsonwebtoken:jjwt:0.9.1' + implementation 'javax.xml.bind:jaxb-api:2.3.0' } dependencyManagement { diff --git a/user/src/main/java/com/weather/user/controller/UserController.java b/user/src/main/java/com/weather/user/controller/UserController.java index f8bea2d..689f52b 100644 --- a/user/src/main/java/com/weather/user/controller/UserController.java +++ b/user/src/main/java/com/weather/user/controller/UserController.java @@ -18,33 +18,13 @@ public class UserController { private final UserService userService; - @GetMapping("/test") - public String test() { - log.info("테스트 컨트롤러 접근됨?"); - return "test"; - } - - @GetMapping("/hello") - public String hello() { - log.info("헬로우 컨트롤러 접근됨?"); - return "hello"; - } - @GetMapping("/loginTest") - public String helloTest(){ - log.info("ddddd"); + @GetMapping("/social/login/google") + public String googleLogin(){ + log.info("ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ"); return "redirect:/oauth2/authorization/google"; } - @PostMapping("/user/signin") - public ResponseEntity> signin(@RequestBody UserDTO userDTO) { - log.info("userDTO: " + userDTO); - - Optional result = userService.signin(userDTO.getEmail(), userDTO.getPassword(), userDTO.isFromSocial()); - - return new ResponseEntity<>(result, HttpStatus.OK); - } - - @PostMapping("/user/signup") + @PostMapping("/signup") public ResponseEntity signup(@RequestBody UserDTO userDTO) { log.info("userDTO: " + userDTO); @@ -53,7 +33,7 @@ public ResponseEntity signup(@RequestBody UserDTO userDTO) { return new ResponseEntity<>(HttpStatus.OK); } - @PostMapping("/user/profile") + @PostMapping("/profile") public ResponseEntity profile(@RequestBody UserDTO userDTO) { log.info("userDTO: " + userDTO); @@ -62,7 +42,7 @@ public ResponseEntity profile(@RequestBody UserDTO userDTO) { return new ResponseEntity<>(result, HttpStatus.OK); } - @PatchMapping("/user/profile/modify") + @PatchMapping("/profile/modify") public ResponseEntity modify(@RequestBody UserDTO userDTO) { log.info("userDTO: " + userDTO); @@ -71,7 +51,7 @@ public ResponseEntity modify(@RequestBody UserDTO userDTO) { return new ResponseEntity<>(result, HttpStatus.OK); } - @DeleteMapping("/user/profile/remove/{email}") + @DeleteMapping("/profile/remove/{email}") public ResponseEntity remove(@PathVariable String email) { log.info("email: " + email); diff --git a/user/src/main/java/com/weather/user/security/filter/CORSFilter.java b/user/src/main/java/com/weather/user/security/filter/CORSFilter.java new file mode 100644 index 0000000..e82d18a --- /dev/null +++ b/user/src/main/java/com/weather/user/security/filter/CORSFilter.java @@ -0,0 +1,36 @@ +package com.weather.user.security.filter; + +import jakarta.servlet.FilterChain; +import jakarta.servlet.ServletException; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import org.springframework.core.Ordered; +import org.springframework.core.annotation.Order; +import org.springframework.stereotype.Component; +import org.springframework.web.filter.OncePerRequestFilter; + +import java.io.IOException; + +@Component +@Order(Ordered.HIGHEST_PRECEDENCE) +public class CORSFilter extends OncePerRequestFilter { + @Override + protected void doFilterInternal(HttpServletRequest request, + HttpServletResponse response, + FilterChain filterChain) + throws ServletException, IOException { + response.setHeader("Access-Control-Allow-Origin", "*"); + response.setHeader("Access-Control-Allow_Credentials", "true"); + response.setHeader("Access-Control-Max-Age", "3600"); + response.setHeader("Access-Control-Allow-Methods", "*"); + response.setHeader("Access-Control-Allow-Headers", + "Origin, X-Requested-With, Content-Type, Accept, Key, Authorization"); + + if("OPTIONS".equalsIgnoreCase(request.getMethod())) { + response.setStatus(HttpServletResponse.SC_OK); + } + else { + filterChain.doFilter(request, response); + } + } +} diff --git a/user/src/main/java/com/weather/user/service/UserService.java b/user/src/main/java/com/weather/user/service/UserService.java index ceafe7c..18b4e30 100644 --- a/user/src/main/java/com/weather/user/service/UserService.java +++ b/user/src/main/java/com/weather/user/service/UserService.java @@ -11,8 +11,6 @@ public interface UserService { void signup(UserDTO userDTO); - Optional signin(String email, String password, boolean fromSocial); - UserDTO profile(String email); UserDTO modify(UserDTO userDTO); diff --git a/user/src/main/java/com/weather/user/service/UserServiceImpl.java b/user/src/main/java/com/weather/user/service/UserServiceImpl.java index 591746e..46622f7 100644 --- a/user/src/main/java/com/weather/user/service/UserServiceImpl.java +++ b/user/src/main/java/com/weather/user/service/UserServiceImpl.java @@ -34,26 +34,6 @@ public void signup(UserDTO userDTO) { userRepository.save(user); } - @Override - public Optional signin(String email, String password, boolean isFromSocial) { - log.info(email + ", " + password + ", " + isFromSocial); - Optional optionalUser = userRepository.findByEmail(email, isFromSocial); - log.info("optionalUser: " + optionalUser); - - if(optionalUser.isEmpty()) { - throw new Error("존재하지 않는 이메일입니다."); - } - - User user = optionalUser.get(); - - if(passwordEncoder.matches(password, user.getPassword())) { - Optional result = Optional.ofNullable(entityToDTO(user)); - return result; - } else { - throw new Error("비밀번호가 일치하지 않습니다."); - } - } - @Override public UserDTO profile(String email) { Optional optionalUser = userRepository.findByEmail(email); diff --git a/user/src/main/java/com/weather/user/util/JWTUtil.java b/user/src/main/java/com/weather/user/util/JWTUtil.java new file mode 100644 index 0000000..0e31e3e --- /dev/null +++ b/user/src/main/java/com/weather/user/util/JWTUtil.java @@ -0,0 +1,24 @@ +package com.weather.user.util; + +import io.jsonwebtoken.Jwts; +import io.jsonwebtoken.SignatureAlgorithm; +import lombok.extern.log4j.Log4j2; + +import java.time.ZonedDateTime; +import java.util.Date; + +@Log4j2 +public class JWTUtil { + private String secretKey = "kdt8ProjectWeatherfitSecretKeykdt8ProjectWeatherfitSecretKeykdt8ProjectWeatherfitSecretKey"; + + private long expire = 60 * 3; + + public String generateToken(String text) throws Exception{ + return Jwts.builder() + .setIssuedAt(new Date()) + .setExpiration(Date.from(ZonedDateTime.now().plusMinutes(expire).toInstant())) + .claim("sub", text) + .signWith(SignatureAlgorithm.HS256, secretKey.getBytes("UTF-8")) + .compact(); + } +} diff --git a/user/src/main/resources/templates/hello.html b/user/src/main/resources/templates/hello.html deleted file mode 100644 index 28e2aa8..0000000 --- a/user/src/main/resources/templates/hello.html +++ /dev/null @@ -1,10 +0,0 @@ - - - - - Title - - -hello - - \ No newline at end of file diff --git a/user/src/main/resources/templates/test.html b/user/src/main/resources/templates/test.html deleted file mode 100644 index ead2d3b..0000000 --- a/user/src/main/resources/templates/test.html +++ /dev/null @@ -1,11 +0,0 @@ - - - - - Title - - - 으에엥 - - - \ No newline at end of file