Merge pull request #7794 from tapankavasthi/tavasthi-bael-3223

BAEL-3223: Excluding URLs from a Spring Filter
This commit is contained in:
rpvilao 2019-10-04 10:13:51 +02:00 committed by GitHub
commit 5e6304f837
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 172 additions and 0 deletions

View File

@ -0,0 +1,17 @@
package com.baeldung.exclude_urls_filter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@ComponentScan(basePackages = "com.baeldung.exclude_urls_filter")
@Configuration
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

View File

@ -0,0 +1,32 @@
package com.baeldung.exclude_urls_filter.controller;
import com.baeldung.exclude_urls_filter.service.FAQService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class FAQController {
private final FAQService faqService;
@Autowired
public FAQController(FAQService faqService) {
this.faqService = faqService;
}
@RequestMapping(value = "/faq/helpline", method = RequestMethod.GET)
public ResponseEntity<String> getHelpLineNumber() {
String helplineNumber = faqService.getHelpLineNumber();
if (helplineNumber != null) {
return new ResponseEntity<String>(helplineNumber, HttpStatus.OK);
} else {
return new ResponseEntity<String>("Unavailable", HttpStatus.NOT_FOUND);
}
}
}

View File

@ -0,0 +1,22 @@
package com.baeldung.exclude_urls_filter.controller;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class Ping {
@RequestMapping(value = "/health", method = RequestMethod.GET)
public ResponseEntity<String> pingGet() {
return new ResponseEntity<String>("pong", HttpStatus.OK);
}
@RequestMapping(value = "/health", method = RequestMethod.POST)
public ResponseEntity<String> pingPost() {
return new ResponseEntity<String>("pong", HttpStatus.OK);
}
}

View File

@ -0,0 +1,26 @@
package com.baeldung.exclude_urls_filter.filter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class FilterRegistrationConfig {
@Bean
public FilterRegistrationBean<LogFilter> logFilter() {
FilterRegistrationBean<LogFilter> registrationBean = new FilterRegistrationBean<>();
registrationBean.setFilter(new LogFilter());
registrationBean.addUrlPatterns("/health", "/faq/*");
return registrationBean;
}
@Bean
public FilterRegistrationBean<HeaderValidatorFilter> headerValidatorFilter() {
FilterRegistrationBean<HeaderValidatorFilter> registrationBean = new FilterRegistrationBean<>();
registrationBean.setFilter(new HeaderValidatorFilter());
registrationBean.addUrlPatterns("*");
return registrationBean;
}
}

View File

@ -0,0 +1,30 @@
package com.baeldung.exclude_urls_filter.filter;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpStatus;
import org.springframework.web.filter.OncePerRequestFilter;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@Order(1)
public class HeaderValidatorFilter extends OncePerRequestFilter {
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException {
String path = request.getRequestURI();
if ("/health".equals(path)) {
filterChain.doFilter(request, response);
return;
}
String countryCode = request.getHeader("X-Country-Code");
if (!"US".equals(countryCode)) {
response.sendError(HttpStatus.BAD_REQUEST.value(), "Invalid Locale");
return;
}
filterChain.doFilter(request, response);
}
}

View File

@ -0,0 +1,25 @@
package com.baeldung.exclude_urls_filter.filter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;
import org.springframework.web.filter.OncePerRequestFilter;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@Order(1)
public class LogFilter extends OncePerRequestFilter {
private final Logger logger = LoggerFactory.getLogger(LogFilter.class);
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
FilterChain filterChain) throws ServletException, IOException {
String path = request.getRequestURI();
String contentType = request.getContentType();
logger.info("Request URL path : {}, Request content type: {}", path, contentType);
filterChain.doFilter(request, response);
}
}

View File

@ -0,0 +1,5 @@
package com.baeldung.exclude_urls_filter.service;
public interface FAQService {
String getHelpLineNumber();
}

View File

@ -0,0 +1,15 @@
package com.baeldung.exclude_urls_filter.service;
import org.springframework.stereotype.Service;
@Service
public class FAQServiceImpl implements FAQService {
private static final String HELPLINE_NUMBER = "+1 888-777-66";
@Override
public String getHelpLineNumber() {
return HELPLINE_NUMBER;
}
}