Skip to content

Commit

Permalink
fix: cors 재설정
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin0o0 committed Nov 16, 2023
1 parent deb3cc5 commit aa28ae0
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 6 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,5 @@ out/
.vscode/

src/main/resources/application-dev.yml
src/main/resources/application-test.yml
src/main/resources/application-test.yml
src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class WebConfiguration implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("http://localhost:3000", "http://localhost:8080", "http://facerain-dev.iptime.org:5000", "https://khumon-study.kro.kr", "https://khumon-edu.kro.kr")
.allowedOrigins("http://localhost:3000", "http://localhost:8080", "http://facerain-dev.iptime.org:5000", "https://khumon-study.kro.kr", "https://khumon-edu.kro.kr", "https://khumon-edu.kro.kr:3000", "https://khumon-edu.kro:80")
.allowedHeaders("authorization", "User-Agent", "Cache-Control", "Content-Type")
.exposedHeaders("authorization", "User-Agent", "Cache-Control", "Content-Type")
.allowedMethods("*");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public WebSecurityCustomizer webSecurityCustomizer() {
public CorsConfigurationSource corsConfigurationSource() {
CorsConfiguration configuration = new CorsConfiguration();

configuration.setAllowedOrigins(List.of("http://localhost:3000", "http://localhost:8080", "http://facerain-dev.iptime.org:5000", "https://khumon-study.kro.kr", "https://khumon-edu.kro.kr"));
configuration.setAllowedOrigins(List.of("http://localhost:3000", "http://localhost:8080", "http://facerain-dev.iptime.org:5000", "https://khumon-study.kro.kr", "https://khumon-edu.kro.kr", "https://khumon-edu.kro.kr:3000", "https://khumon-edu.kro:80"));
configuration.addAllowedOriginPattern("*");
configuration.addAllowedMethod("*");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Component
@Slf4j
Expand All @@ -21,13 +23,16 @@ public void handle(HttpServletRequest request, HttpServletResponse response, Acc
throws IOException {
ObjectMapper objectMapper = new ObjectMapper();
response.setContentType("application/json;charset=UTF-8");
JSONObject responseJson = new JSONObject();
Map<String, String> responseJson = new HashMap<>();
response.setStatus(HttpServletResponse.SC_FORBIDDEN);
try {
responseJson.put("message", "엑세스 권한이 없습니다.");
} catch (JSONException e) {
throw new RuntimeException(e);
}
response.getWriter().write(objectMapper.writeValueAsString(responseJson));
response.getWriter().flush();
response.getWriter().close();

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Component
Expand All @@ -22,7 +24,7 @@ public void commence(HttpServletRequest request, HttpServletResponse response, A

ObjectMapper objectMapper = new ObjectMapper();
response.setContentType("application/json;charset=UTF-8");
JSONObject responseJson = new JSONObject();
Map<String, String> responseJson = new HashMap<>();
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
log.info("인증 실패");
try {
Expand All @@ -31,6 +33,9 @@ public void commence(HttpServletRequest request, HttpServletResponse response, A
throw new RuntimeException(e);
}
response.getWriter().write(objectMapper.writeValueAsString(responseJson));
response.getWriter().flush();
response.getWriter().close();

}

}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
package com.teamh.khumon.security;import com.fasterxml.jackson.databind.ObjectMapper;import jakarta.servlet.ServletException;import jakarta.servlet.http.HttpServletRequest;import jakarta.servlet.http.HttpServletResponse;import lombok.extern.slf4j.Slf4j;import org.json.JSONException;import org.json.JSONObject;import org.springframework.security.core.AuthenticationException;import org.springframework.security.web.authentication.AuthenticationFailureHandler;import org.springframework.stereotype.Component;import java.io.IOException;@Slf4j@Componentpublic class OAuth2LoginFailureHandler implements AuthenticationFailureHandler { @Override public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException { ObjectMapper objectMapper = new ObjectMapper(); response.setContentType("application/json;charset=UTF-8"); response.setStatus(HttpServletResponse.SC_BAD_REQUEST); JSONObject jsonObject = new JSONObject(); log.info("소셜 로그인 실패"); try { jsonObject.put("message", "소셜 로그인 실패"); } catch (JSONException e) { throw new RuntimeException(e); } response.getWriter().write(objectMapper.writeValueAsString(jsonObject)); }}
package com.teamh.khumon.security;import com.fasterxml.jackson.databind.ObjectMapper;import jakarta.servlet.ServletException;import jakarta.servlet.http.HttpServletRequest;import jakarta.servlet.http.HttpServletResponse;import lombok.extern.slf4j.Slf4j;import org.json.JSONException;import org.json.JSONObject;import org.springframework.security.core.AuthenticationException;import org.springframework.security.web.authentication.AuthenticationFailureHandler;import org.springframework.stereotype.Component;import java.io.IOException;import java.util.HashMap;import java.util.Map;@Slf4j@Componentpublic class OAuth2LoginFailureHandler implements AuthenticationFailureHandler { @Override public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException { ObjectMapper objectMapper = new ObjectMapper(); response.setContentType("application/json;charset=UTF-8"); response.setStatus(HttpServletResponse.SC_BAD_REQUEST); Map<String, String> jsonObject = new HashMap<>(); log.info("소셜 로그인 실패"); try { jsonObject.put("message", "소셜 로그인 실패"); } catch (JSONException e) { throw new RuntimeException(e); } response.getWriter().write(objectMapper.writeValueAsString(jsonObject)); response.getWriter().flush(); response.getWriter().close(); }}
Expand Down

0 comments on commit aa28ae0

Please sign in to comment.