From ee5322112eea019cd92d7415c324872d88963dbb Mon Sep 17 00:00:00 2001 From: HeeSung98 Date: Mon, 27 Nov 2023 10:15:40 +0900 Subject: [PATCH] =?UTF-8?q?jwt=20=ED=86=A0=ED=81=B0=20=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../weather/user/config/SecurityConfig.java | 8 ++++- .../user/security/filter/ApiLoginFilter.java | 2 ++ .../security/handler/LoginSuccessHandler.java | 27 ++++++++-------- .../java/com/weather/user/util/JWTUtil.java | 23 +++++++++++++ .../com/weather/user/security/JWTTest.java | 32 +++++++++++++++++++ .../weather/user/service/UserServiceTest.java | 12 ------- 6 files changed, 78 insertions(+), 26 deletions(-) create mode 100644 user/src/test/java/com/weather/user/security/JWTTest.java diff --git a/user/src/main/java/com/weather/user/config/SecurityConfig.java b/user/src/main/java/com/weather/user/config/SecurityConfig.java index 985d900..8f6609c 100644 --- a/user/src/main/java/com/weather/user/config/SecurityConfig.java +++ b/user/src/main/java/com/weather/user/config/SecurityConfig.java @@ -5,6 +5,7 @@ import com.weather.user.security.handler.LoginFailureHandler; import com.weather.user.security.handler.LoginSuccessHandler; import com.weather.user.security.service.AuthUserDetailsService; +import com.weather.user.util.JWTUtil; import lombok.RequiredArgsConstructor; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -48,7 +49,7 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception{ @Bean public LoginSuccessHandler loginSuccessHandler() { - return new LoginSuccessHandler(passwordEncoder()); + return new LoginSuccessHandler(jwtUtil()); } @Bean @@ -75,4 +76,9 @@ public ApiLoginFilter apiLoginFilter(AuthenticationManager authenticationManager return apiLoginFilter; } + + @Bean + public JWTUtil jwtUtil() { + return new JWTUtil(); + } } diff --git a/user/src/main/java/com/weather/user/security/filter/ApiLoginFilter.java b/user/src/main/java/com/weather/user/security/filter/ApiLoginFilter.java index 4b4f491..37f98ea 100644 --- a/user/src/main/java/com/weather/user/security/filter/ApiLoginFilter.java +++ b/user/src/main/java/com/weather/user/security/filter/ApiLoginFilter.java @@ -1,6 +1,7 @@ package com.weather.user.security.filter; import com.weather.user.security.handler.LoginSuccessHandler; +import com.weather.user.util.JWTUtil; import jakarta.servlet.ServletException; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; @@ -16,6 +17,7 @@ @Log4j2 public class ApiLoginFilter extends AbstractAuthenticationProcessingFilter { + public ApiLoginFilter(String defaultFilterProcessUrl) { super(defaultFilterProcessUrl); } diff --git a/user/src/main/java/com/weather/user/security/handler/LoginSuccessHandler.java b/user/src/main/java/com/weather/user/security/handler/LoginSuccessHandler.java index 7142a2e..9583f27 100644 --- a/user/src/main/java/com/weather/user/security/handler/LoginSuccessHandler.java +++ b/user/src/main/java/com/weather/user/security/handler/LoginSuccessHandler.java @@ -2,28 +2,25 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.weather.user.security.dto.AuthUserDTO; +import com.weather.user.util.JWTUtil; import jakarta.servlet.FilterChain; import jakarta.servlet.ServletException; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import lombok.extern.log4j.Log4j2; -import net.minidev.json.JSONObject; import org.springframework.security.core.Authentication; import org.springframework.security.crypto.password.PasswordEncoder; -import org.springframework.security.web.DefaultRedirectStrategy; -import org.springframework.security.web.RedirectStrategy; import org.springframework.security.web.authentication.AuthenticationSuccessHandler; import java.io.IOException; @Log4j2 public class LoginSuccessHandler implements AuthenticationSuccessHandler { - private RedirectStrategy redirectStrategy = new DefaultRedirectStrategy(); private ObjectMapper objectMapper = new ObjectMapper(); - private PasswordEncoder passwordEncoder; + private JWTUtil jwtUtil; - public LoginSuccessHandler(PasswordEncoder passwordEncoder) { - this.passwordEncoder = passwordEncoder; + public LoginSuccessHandler(JWTUtil jwtUtil) { + this.jwtUtil = jwtUtil; } @Override @@ -35,13 +32,17 @@ public void onAuthenticationSuccess(HttpServletRequest request, HttpServletRespo log.info(authentication); AuthUserDTO authUserDTO = (AuthUserDTO) authentication.getPrincipal(); - boolean fromSocial = authUserDTO.isFromSocial(); - String name = authUserDTO.getName(); - log.info(authUserDTO); - String result = objectMapper.writeValueAsString(authUserDTO); - response.setContentType("application/json;charset=utf-8"); - response.getWriter().write(result); + try { + String token = jwtUtil.generateToken(authUserDTO.getEmail()); + + String result = objectMapper.writeValueAsString(authUserDTO); + result = result.replace("}", ", \"token\": \"" + token + "\"}"); + response.setContentType("application/json;charset=utf-8"); + response.getWriter().write(result); + } catch (Exception e) { + e.printStackTrace(); + } } } diff --git a/user/src/main/java/com/weather/user/util/JWTUtil.java b/user/src/main/java/com/weather/user/util/JWTUtil.java index 0e31e3e..48909e9 100644 --- a/user/src/main/java/com/weather/user/util/JWTUtil.java +++ b/user/src/main/java/com/weather/user/util/JWTUtil.java @@ -2,6 +2,8 @@ import io.jsonwebtoken.Jwts; import io.jsonwebtoken.SignatureAlgorithm; +import io.jsonwebtoken.impl.DefaultClaims; +import io.jsonwebtoken.impl.DefaultJws; import lombok.extern.log4j.Log4j2; import java.time.ZonedDateTime; @@ -21,4 +23,25 @@ public String generateToken(String text) throws Exception{ .signWith(SignatureAlgorithm.HS256, secretKey.getBytes("UTF-8")) .compact(); } + + public String validateAndExtract(String stringToken) throws Exception { + String value = null; + + try { + DefaultJws defaultJws = (DefaultJws) Jwts.parser() + .setSigningKey(secretKey.getBytes("UTF-8")) + .parseClaimsJws(stringToken); + + log.info(defaultJws); + log.info(defaultJws.getBody().getClass()); + + DefaultClaims claims = (DefaultClaims) defaultJws.getBody(); + + value = claims.getSubject(); + } catch (Exception e) { + e.printStackTrace(); + } + + return value; + } } diff --git a/user/src/test/java/com/weather/user/security/JWTTest.java b/user/src/test/java/com/weather/user/security/JWTTest.java new file mode 100644 index 0000000..22fde63 --- /dev/null +++ b/user/src/test/java/com/weather/user/security/JWTTest.java @@ -0,0 +1,32 @@ +package com.weather.user.security; + +import com.weather.user.util.JWTUtil; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +public class JWTTest { + private JWTUtil jwtUtil; + + @BeforeEach + public void testBefore() { + System.out.println("testBefore................."); + jwtUtil = new JWTUtil(); + } + + @Test + public void testEncode() throws Exception { + String email = "user95@test.com"; + + String str = jwtUtil.generateToken(email); + + System.out.println(str); + + Thread.sleep(5000); + + String resultEmail = jwtUtil.validateAndExtract(str); + + System.out.println(resultEmail); + } +} diff --git a/user/src/test/java/com/weather/user/service/UserServiceTest.java b/user/src/test/java/com/weather/user/service/UserServiceTest.java index 715190f..c2233c0 100644 --- a/user/src/test/java/com/weather/user/service/UserServiceTest.java +++ b/user/src/test/java/com/weather/user/service/UserServiceTest.java @@ -30,16 +30,4 @@ void signupTest() { userService.signup(userDTO); } - - @Test - void signinTest() { - UserDTO userDTO = UserDTO.builder() - .email("servicetester1@test.com") - .password("1234") - .fromSocial(true) - .build(); - - Optional result = userService.signin(userDTO.getEmail(), userDTO.getPassword(), userDTO.isFromSocial()); - System.out.println("result: " + result); - } }