Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: 허용되지 않은 요청 필터링 #74

Merged
merged 1 commit into from
Nov 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions src/main/java/com/verby/indp/global/filter/ResourceFilter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.verby.indp.global.filter;

import jakarta.servlet.Filter;
import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import org.springframework.stereotype.Component;

@Component
public class ResourceFilter implements Filter {

private static final List<String> ALLOWED_PATHS = Arrays.asList(
"/api/music",
"/api/stores",
"/api/main",
"/api/contacts"
);

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
HttpServletRequest httpRequest = (HttpServletRequest) request;
String requestURI = httpRequest.getRequestURI();

boolean isAllowed = ALLOWED_PATHS.stream().anyMatch(requestURI::startsWith);

if (isAllowed) {
chain.doFilter(request, response);
} else {
response.getWriter().write("Access Denied");
response.setContentType("text/plain");
response.setCharacterEncoding("UTF-8");

((HttpServletResponse) response).setStatus(HttpServletResponse.SC_FORBIDDEN);
}
}
}
Loading