Skip to content

Commit 60ee051

Browse files
committed
[CICD] securityConfig 수정
1 parent e13fee7 commit 60ee051

3 files changed

Lines changed: 28 additions & 55 deletions

File tree

src/main/java/fitfit/domain/token/filter/JwtAuthenticationFilter.java

Lines changed: 24 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,7 @@
2323
@Component
2424
@RequiredArgsConstructor
2525
public class JwtAuthenticationFilter extends OncePerRequestFilter {
26-
27-
private final JwtProvider jwtProvider;
28-
private final UserDetailsService userDetailsService;
29-
private final AntPathMatcher antPathMatcher = new AntPathMatcher();
30-
26+
// 토큰 없이도 가능하게
3127
private static final List<String> WHITE_LIST = List.of(
3228
"/swagger-ui/**",
3329
"/v3/api-docs/**",
@@ -38,46 +34,35 @@ public class JwtAuthenticationFilter extends OncePerRequestFilter {
3834
"/auth/refresh"
3935
);
4036

41-
@Override
42-
protected boolean shouldNotFilter(HttpServletRequest request) {
43-
if (request.getMethod().equalsIgnoreCase(HttpMethod.OPTIONS.name())) {
44-
return true;
45-
}
46-
return WHITE_LIST.stream().anyMatch(white -> antPathMatcher.match(white, request.getRequestURI()));
47-
}
37+
private final JwtProvider jwtProvider;
38+
private final UserDetailsService userDetailsService;
39+
private final AntPathMatcher antPathMatcher = new AntPathMatcher();
4840

4941
@Override
50-
protected void doFilterInternal(
51-
@NonNull HttpServletRequest request,
52-
@NonNull HttpServletResponse response,
53-
@NonNull FilterChain filterChain
54-
) throws ServletException, IOException {
55-
56-
final String authHeader = request.getHeader("Authorization");
57-
58-
if (authHeader == null || !authHeader.startsWith("Bearer ")) {
59-
filterChain.doFilter(request, response);
60-
return;
61-
}
62-
42+
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
6343
try {
64-
Long memberId = jwtProvider.getMemberIdAndValidateToken(authHeader);
65-
if (SecurityContextHolder.getContext().getAuthentication() == null) {
66-
UserDetails userDetails = this.userDetailsService.loadUserByUsername(memberId.toString());
67-
UsernamePasswordAuthenticationToken authToken = new UsernamePasswordAuthenticationToken(
68-
userDetails,
69-
null,
70-
userDetails.getAuthorities()
71-
);
72-
authToken.setDetails(
73-
new WebAuthenticationDetailsSource().buildDetails(request)
74-
);
75-
SecurityContextHolder.getContext().setAuthentication(authToken);
76-
}
44+
Long memberId = jwtProvider.getMemberIdAndValidateToken(request.getHeader("Authorization"));
45+
46+
UserDetails userDetails = userDetailsService.loadUserByUsername(memberId.toString());
47+
UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(
48+
userDetails,
49+
null,
50+
userDetails.getAuthorities()
51+
);
52+
authentication.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));
53+
SecurityContextHolder.getContext().setAuthentication(authentication);
7754
} catch (Exception e) {
78-
SecurityContextHolder.clearContext();
55+
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
56+
response.getWriter().write("{\"error\": \"" + e.getMessage() + "\"}");
57+
return;
7958
}
8059

8160
filterChain.doFilter(request, response);
8261
}
62+
63+
@Override
64+
protected boolean shouldNotFilter(HttpServletRequest request) throws ServletException {
65+
// 더 안정적인 AntPathMatcher.match() 로 수정
66+
return WHITE_LIST.stream().anyMatch(white -> antPathMatcher.match(white, request.getRequestURI()));
67+
}
8368
}

src/main/java/fitfit/global/config/WebConfig.java

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/main/java/fitfit/global/security/config/SecurityConfig.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,8 @@ public SecurityFilterChain filterChain (HttpSecurity http) throws Exception {
4040
"/swagger-ui/**",
4141
"/v3/api-docs/**",
4242
"/swagger-resources/**",
43-
"/webjars/**",
44-
"/auth/**",
4543
"/api/members/auth/kko",
46-
"/auth/refresh"
44+
"/api/members/auth/refresh"
4745
).permitAll()
4846
.anyRequest().authenticated()
4947
)
@@ -52,15 +50,15 @@ public SecurityFilterChain filterChain (HttpSecurity http) throws Exception {
5250
}
5351

5452
/**
55-
* Cors 설정 - IOS 앱에서 API 호출 허용
53+
* Cors 설정 - API 호출 허용
5654
*/
5755
@Bean
5856
public CorsConfigurationSource corsConfigurationSource() {
5957
CorsConfiguration config = new CorsConfiguration();
60-
config.setAllowedOrigins(List.of("https://d3trbid3w75opm.cloudfront.net", "http://localhost:3000", "https://fitfit.site", "http://localhost:5173"));
58+
config.setAllowedOrigins(List.of("*"));
6159
config.setAllowedMethods(List.of("GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS"));
6260
config.setAllowedHeaders(List.of("*"));
63-
config.setAllowCredentials(true);
61+
config.setAllowCredentials(false);
6462
config.setExposedHeaders(List.of("Authorization"));
6563

6664
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();

0 commit comments

Comments
 (0)