Skip to content

Commit

Permalink
Merge pull request #47 from dear-santa/feature/fix-cors
Browse files Browse the repository at this point in the history
Feature/fix cors Spring Legacy CORS 설정 FIlter 추가
  • Loading branch information
skybluelion authored Jan 1, 2024
2 parents 5d83c44 + 994d499 commit 1d3ea89
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 10 deletions.
8 changes: 0 additions & 8 deletions src/main/java/com/dearsanta/app/config/WebMvcConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
Expand All @@ -22,13 +21,6 @@ public class WebMvcConfig implements WebMvcConfigurer {
@Qualifier("jwtInterceptor")
private JwtInterceptor jwtInterceptor;

@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("http://dearsanta.shop", "https://dearsanta.shop", "http://www.dearsanta.shop", "https://www.dearsanta.shop", properties.getFRONT_LOCAL_URL() )
.allowedMethods("OPTIONS","GET","POST","PUT","DELETE");
}

@Override
public void addInterceptors(InterceptorRegistry registry) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class JwtInterceptor extends HandlerInterceptorAdapter {

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

log.info("JwtInterceptor - preHandle(): 로그인 시도");
String accessToken = request.getHeader("Authorization");
String memberId = getMemberIdByAccessToken(accessToken);

Expand Down
14 changes: 13 additions & 1 deletion src/main/java/com/dearsanta/app/security/KakaoOauthClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.HttpServerErrorException;
import org.springframework.web.client.RestTemplate;

import java.util.NoSuchElementException;
Expand Down Expand Up @@ -60,7 +62,17 @@ private KakaoOauthTokenDto getKakaoAccessTokenByAuthorazationCode(String authori
body.add("code", authorizeCode);

HttpEntity<?> request = new HttpEntity<>(body, headers);
return restTemplate.postForObject(TOKEN_URI, request, KakaoOauthTokenDto.class);
KakaoOauthTokenDto kakaoOauthTokenDto = null;
try {
kakaoOauthTokenDto = restTemplate.postForEntity(TOKEN_URI, request, KakaoOauthTokenDto.class).getBody();
} catch(HttpClientErrorException e) {
log.error("HttpClientErrorException : " + e.getResponseBodyAsString());
} catch(HttpServerErrorException e) {
log.error("HttpServerErrorException : " + e.getResponseBodyAsString());
} catch(Exception e) {
log.error("Exception : " + e);
}
return kakaoOauthTokenDto;
}

// 2. 토큰으로 사용자 정보 조회하기
Expand Down
34 changes: 34 additions & 0 deletions src/main/webapp/WEB-INF/web.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,40 @@
<url-pattern>/*</url-pattern>
</filter-mapping>

<!-- CORS 설정 -->
<filter>
<filter-name>corsFilter</filter-name>
<filter-class>org.apache.catalina.filters.CorsFilter</filter-class>
<init-param>
<param-name>cors.allowed.origins</param-name>
<param-value>*</param-value>
</init-param>
<init-param>
<param-name>cors.allowed.methods</param-name>
<param-value>GET,HEAD,POST,PUT,DELETE,TRACE,OPTIONS,PATCH</param-value>
</init-param>
<init-param>
<param-name>cors.allowed.headers</param-name>
<param-value>Content-Type,X-Requested-With,accept,Origin,Access-Control-Request-Method,Access-Control-Request-Headers,Authorization</param-value>
</init-param>
<init-param>
<param-name>cors.exposed.headers</param-name>
<param-value>Access-Control-Allow-Origin,Access-Control-Allow-Credentials</param-value>
</init-param>
<init-param>
<param-name>cors.support.credentials</param-name>
<param-value>false</param-value>
</init-param>
<init-param>
<param-name>cors.preflight.maxage</param-name>
<param-value>10</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>corsFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
<param-name>contextConfigLocation</param-name>
Expand Down

0 comments on commit 1d3ea89

Please sign in to comment.