java
@Slf4j
public class X509CertificateFilter extends OncePerRequestFilter {
private final X509Certificate apiCert;
public X509CertificateFilter(X509Certificate apiCert) {
this.apiCert = apiCert;
}
@Override
protected void doFilterInternal(@NonNull HttpServletRequest request, @NonNull HttpServletResponse response, @NonNull FilterChain filterChain) throws ServletException, IOException {
final X509Certificate[] certs = (X509Certificate[]) request.getAttribute("javax.servlet.request.X509Certificate");
if (certs != null) {
log.info("Found {} certificates", certs.length);
for (X509Certificate cert : certs) {
if (cert.equals(apiCert)) {
filterChain.doFilter(request, response);
}
}
}
log.error("Forbidden at {} access from: {}", request.getRequestURI(), request.getRemoteAddr());
response.setStatus(HttpServletResponse.SC_FORBIDDEN);
}
}