-
spring boot 어노테이션 FilterJAVA/Spring 2018. 5. 12. 23:26
spring boot 에서 어노테이션으로 filter 를 지정할 수 있다.
사용법
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.web.bind.annotation.CrossOrigin;
@SpringBootApplication
/**
* @WebFilter 를 사용하기 위한 어노테이션
*/
@ServletComponentScan
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
@CrossOrigin
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebFilter(urlPatterns = {"/api/*"}, description = "인증 필터")
@Slf4j
public class ApiFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
log.info("filter => API Token Filter");
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;
log.info("req header => {}", request.getHeader("x-auth-token"));
if ( request.getHeader("x-auth-token") == null ) {
response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "인증오류");
}
chain.doFilter(req, res);
}
@Override
public void destroy() {
}
}'JAVA > Spring' 카테고리의 다른 글
tomcat ssl letsencrypt 적용 (0) 2018.06.01 spring boot + tomcat hikariDatasource null (0) 2018.05.31 리눅스 maven 설치 (0) 2018.05.09 Spring + boot + thymeleaf 네임스페이스 모음 (0) 2018.05.07 jpa querydsl 표현식 (0) 2018.04.19