[java web spring] [Spring] web.xml - Filter url 제외시키기
2021.05.14 12:30
[java web spring] [Spring] web.xml - Filter url 제외시키기
Filter URL Exclude
얼마 전 2차 인증을 구현해야 하는 업무 도중
Spring Security를 사용하다가, 로그인 부분에 덕지덕지 붙이는 것 같아
분명 빠른 길이 있는데, 먼 길을 돌아가는 것 같았다.
굳이 로그인 이후에, 추가 인증을 하고 싶다면
그리고 그 인증의 대상이 사용자마다 상이하다면
Servlet에서 Filter을 시켜주는 것도 괜찮았기에
남기는 글입니다.
Web.xml을 조금 수정하자
서블릿에 커스텀 필터를 달아주고, 이를 초기화 해줄 값을 넣어줍니다.
|
1
2
3
4
5
6
7
8
|
<filter>
<filter-name>CustomAuthenticationFilter</filter-name>
<filter-class>com.test.gate.filter.AuthenticationFilter/filter-class>
<init-param>
<param-name>excludedUrls</param-name>
<param-value>/admin/secondary/login.do,/admin/secondary/authentication.do</param-value>
</init-param>
</filter>
|
cs |
커스텀 필터를 만들어보자
이제 위에서 입력한 경로
com.text.gate.filter.AuthenticationFilter를 생성합니다.
(Filter에 대해서 아직 모르신다면 조금 검색하면 알 수 있습니다.)
|
1
2
3
4
5
6
7
|
public class AuthenticationFilter implements Filter{
private List<String> excludeUrls;
...
}
|
cs |
이때 몇 가지 메소드를 오버라이드 해줍니다.
이제 URL를 제외시키기 위해서
딱 두가지 부분만 수정하겠습니다.
FilterConfig를 사용해서 ',' (콤마) 로 구별한 URL를 가져오고
리스트에 넣어줍니다.
|
1
2
3
4
|
public void init(FilterConfig filterConfig) throws ServletException {
String excludePattern = filterConfig.getInitParameter("excludedUrls");
excludedUrls = Arrays.asList(excludePattern.split(","));
}
|
cs |
이제 필터링을 해줍니다.
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
public void doFilter(ServletRequest request, ServletResponse resp, FilterChain chain)
throws IOException, ServletException {
String path = ((HttpServletRequest) request).getServletPath();
if(!excludedUrls.contains(path))
{
// 제외하지 않는 경우
// 로직을 타게 합니다.
}
// 제외하는 경우 흘려줍니다
chain.doFilter(req, resp);
}
|
cs |
마무리
생각나는대로 써서 부족한 점이 많습니다.
그나저나 서블릿도 재미있네요.
다음은 서블릿에서 Bean 주입이 되지 않는 경우
어떻게 주입을 시켜주면 좋을지 글을 써야겠습니다.
출처: https://redcoder.tistory.com/194 [로재의 개발 일기]
본 웹사이트는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 웹사이트 서버의 유지 및 관리, 그리고 기술 콘텐츠 향상을 위해 쓰여집니다.
광고 클릭에서 발생하는 수익금은 모두 웹사이트 서버의 유지 및 관리, 그리고 기술 콘텐츠 향상을 위해 쓰여집니다.

