Skip to content

Commit

Permalink
Merge pull request #175 from SPACE-FOR-SPACE/fix/#135
Browse files Browse the repository at this point in the history
배포 서버 소셜 로그인 쿠키 반환 에러 해결
  • Loading branch information
YunChan-Oh authored Nov 26, 2024
2 parents 0664d89 + 3cc9954 commit c975788
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 4 deletions.
44 changes: 41 additions & 3 deletions src/main/java/com/space/server/common/config/SecurityConfig.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.space.server.common.config;

import jakarta.servlet.http.Cookie;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.space.server.common.exception.ErrorResponse;
import com.space.server.common.exception.security.SpaceSecurityException;
import com.space.server.domain.auth.domain.repository.RefreshRepository;
import com.space.server.common.exception.security.SpaceSecurityExceptionFilter;
import com.space.server.common.jwt.exception.CustomAccessDeniedException;
Expand All @@ -12,9 +15,11 @@
import com.space.server.domain.oauth.handler.CustomSuccessHandler;
import com.space.server.domain.oauth.service.CustomOAuth2UserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
Expand All @@ -26,10 +31,13 @@
import org.springframework.security.web.authentication.logout.LogoutFilter;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.filter.CorsFilter;

import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

@Slf4j
@Configuration
@EnableWebSecurity(debug = true)
@RequiredArgsConstructor
Expand Down Expand Up @@ -91,7 +99,39 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
.oauth2Login((oauth2) -> oauth2
.userInfoEndpoint((userInfoEndpointConfig) -> userInfoEndpointConfig
.userService(customOAuth2UserService))
.successHandler(customSuccessHandler));
.successHandler(customSuccessHandler)
.failureHandler((request, response, exception) -> {
if (exception instanceof SpaceSecurityException) {
SpaceSecurityException e = (SpaceSecurityException) exception;
response.setStatus(e.getStatus().value());
response.setContentType(MediaType.APPLICATION_JSON_VALUE);
response.setCharacterEncoding(StandardCharsets.UTF_8.name());

ErrorResponse errorResponse = ErrorResponse.from(
e.getStatus().value(),
e.getErrorCode(),
e.getMessage()
);
log.warn("소셜 스페이스 익셉션 동작");
response.getWriter().write(objectMapper.writeValueAsString(errorResponse));
} else {
response.sendRedirect("/login?error");
}
Cookie[] cookies = request.getCookies();
if (cookies != null) {
for (Cookie cookie : cookies) {
log.warn("쿠키 탐색 : "+cookie.getName());
if ("JSESSIONID".equals(cookie.getName())) {
cookie.setValue("");
cookie.setPath("/");
cookie.setMaxAge(0);
response.addCookie(cookie);
log.warn("JSESSIONID 발견");
break;
}
}
}
}));

http
.logout((auth) -> auth.disable());
Expand Down Expand Up @@ -130,8 +170,6 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
.sessionManagement((session) -> session
.sessionCreationPolicy(SessionCreationPolicy.STATELESS));



return http.build();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.space.server.domain.oauth.exception;

import com.space.server.common.exception.security.SpaceSecurityException;
import org.springframework.http.HttpStatus;

public class SocialUserExistedException extends SpaceSecurityException {
public SocialUserExistedException() {
super(HttpStatus.CONFLICT, "USER_EXISTED", "유저가 이미 존재합니다.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,13 @@ public void onAuthenticationSuccess(HttpServletRequest request, HttpServletRespo
Cookie[] cookies = request.getCookies();
if (cookies != null) {
for (Cookie cookie : cookies) {
log.warn("쿠키 탐색 : "+cookie.getName());
if ("JSESSIONID".equals(cookie.getName())) {
cookie.setValue("");
cookie.setPath("/");
cookie.setMaxAge(0);
response.addCookie(cookie);
log.warn("JSESSIONID 발견");
break;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.space.server.domain.oauth.service;

import com.space.server.domain.oauth.exception.SocialUserExistedException;
import com.space.server.domain.oauth.service.dto.*;
import com.space.server.domain.user.domain.Users;
import com.space.server.domain.user.domain.repository.UserRepository;
Expand Down Expand Up @@ -64,7 +65,7 @@ else if (registrationId.equals("kakao")){

if (existData.getType().equals("normal")) {
log.warn("이미 존재합니다.");
throw new OAuth2AuthenticationException("Normal user already exists");
throw new SocialUserExistedException();
}

existData.updateSocial(oAuth2Response.getEmail(), type);
Expand Down

0 comments on commit c975788

Please sign in to comment.