This works great for Spring Websocket.
But when i try this for my Servlet it redirects me to login…
package de.bytestore.hostinger.api;
import com.google.gson.Gson;
import de.bytestore.hostinger.gson.templates.JSONStatus;
import de.bytestore.hostinger.gson.templates.JSONStatusMessage;
import de.bytestore.hostinger.handler.PaymentHandler;
import io.jmix.core.JmixSecurityFilterChainOrder;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.web.util.UriTemplate;
import java.io.IOException;
/**
* Configuration class for the PaymentAPIRoute servlet.
*/
@Configuration
@WebServlet(value = "/api/payment/{provider}", loadOnStartup = 1)
public class PaymentAPIRoute extends HttpServlet {
protected static final Logger log = org.slf4j.LoggerFactory.getLogger(PaymentAPIRoute.class);
@Bean(name = "paymentSecurityFilterChain")
@Order(JmixSecurityFilterChainOrder.FLOWUI - 10)
// https://forum.jmix.io/t/static-resources-problem/2351/10
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http.securityMatcher("/api/payment/**")
.authorizeHttpRequests((authorize) -> authorize.requestMatchers("/api/payment/**").permitAll());
return http.build();
}
@Bean
public ServletRegistrationBean exampleServletBean() {
ServletRegistrationBean bean = new ServletRegistrationBean(
this, "/api/payment/*");
bean.setLoadOnStartup(1);
return bean;
}
/**
* Handles a POST request to authorize a payment with a specified provider.
*
* @param requestIO the HTTP servlet request object
* @param responseIO the HTTP servlet response object
* @throws ServletException if a servlet-specific error occurs during this request
* @throws IOException if an I/O error occurs during this request
*/
@Override
protected void doPost(HttpServletRequest requestIO, HttpServletResponse responseIO) throws ServletException, IOException {
this.handleRequest(requestIO, responseIO);
}
/**
* Handles a request for payment authorization with a specified provider.
*
* @param requestIO the HTTP servlet request object
* @param responseIO the HTTP servlet response object
*/
private void handleRequest(HttpServletRequest requestIO, HttpServletResponse responseIO) {
String providerIO = this.extractProviderFromUri(requestIO.getRequestURI());
try {
if(providerIO != null && !providerIO.isEmpty()) {
boolean stateIO = PaymentHandler.authorize(requestIO, responseIO, providerIO);
if(!stateIO) {
this.notFound(responseIO, providerIO);
} else {
log.info("Handled Payment Webhook for Provider '" + providerIO + "'.");
}
} else {
this.notFound(responseIO, providerIO);
}
} catch (Exception exceptionIO) {
log.error("Unable to execute Payment Provider IPN Function for '" + providerIO + "'.", exceptionIO);
}
}
/**
* Sets the HTTP response status as internal server error and writes a JSON status message indicating that the authorize provider for the given provider name cannot be found.
*
* @param responseIO the HTTP servlet response object
* @param providerIO the name of the authorize provider
* @throws IOException if an I/O error occurs during this request
*/
private void notFound(HttpServletResponse responseIO, String providerIO) throws IOException {
responseIO.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
responseIO.getWriter().write(new Gson().toJson(new JSONStatusMessage(JSONStatus.ERROR, "Unable to find authorize provider for '" + providerIO + "'.")));
}
/**
* Handles a GET request to authorize a payment with a specified provider.
*
* @param requestIO the HTTP servlet request object
* @param responseIO the HTTP servlet response object
* @throws ServletException if a servlet-specific error occurs during this request
*/
@Override
protected void doGet(HttpServletRequest requestIO, HttpServletResponse responseIO) throws ServletException, IOException {
this.handleRequest(requestIO, responseIO);
// responseIO.setStatus(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
// responseIO.getWriter().write(new Gson().toJson(new JSONStatusMessage(JSONStatus.ERROR, "Please use POST Method.")));
}
/**
* Extracts the provider name from the given URI.
*
* @param uri The URI string from which to get the provider name
* @return The provider name extracted from the URI
*/
private String extractProviderFromUri(String uri) {
UriTemplate template = new UriTemplate("/api/payment/{provider}");
return template.match(uri).get("provider");
}
}
18:48:11.445 [http-nio-80-exec-3] INFO c.v.flow.spring.security.RequestUtil - Navigation Access Control is disabled. Cannot determine if api/payment/paypal refers to a public view, thus access is denied. Please add an explicit request matcher rule for this URL.