Skip to content

Commit

Permalink
feat: frontend의 host에 대하여 CORS 설정
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin0o0 committed Nov 9, 2023
1 parent 64d8a9f commit 850a1ef
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class SwaggerConfiguration {
@Bean
public OpenAPI openAPI(){
Server server = new Server();
server.setUrl("http://localhost:8080");
server.setUrl("https://khumon-edu.kro.kr");
List<Server> servers = new ArrayList<>();
servers.add(server);
return new OpenAPI()
Expand Down
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")
.allowedOrigins("http://localhost:3000", "http://localhost:8080", "http://facerain-dev.iptime.org:5000", "https://khumon-study.kro.kr", "https://khumon-edu.kro.kr")
.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"));
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.addAllowedOriginPattern("*");
configuration.addAllowedMethod("*");

Expand Down
Original file line number Diff line number Diff line change
@@ -1,34 +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
@Component
public 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;@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)); }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,8 @@ private String extractUsername(Authentication authentication) {
private URI createURI() throws UnsupportedEncodingException {
return UriComponentsBuilder
.newInstance()
.scheme("http")
.host("localhost")
.port(3000)
.scheme("https")
.host("khumon-study.kro.kr")
.build()
.toUri();
}
Expand Down

0 comments on commit 850a1ef

Please sign in to comment.