Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace Global Exception Handler for Whitelabel reroute #41

Open
PaulKreft opened this issue Feb 15, 2024 · 0 comments
Open

Replace Global Exception Handler for Whitelabel reroute #41

PaulKreft opened this issue Feb 15, 2024 · 0 comments

Comments

@PaulKreft
Copy link
Owner

package de.neuefische.backend.security;

import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.resource.PathResourceResolver;

import java.io.IOException;

@Configuration
public class ReactRoutingForwarding implements WebMvcConfigurer {
    public static final String DEFAULT_STARTING_PAGE = "static/index.html";

    static class ReactRoutingPathResourceResolver extends PathResourceResolver {
        @Override
        protected Resource getResource(String resourcePath, Resource location) throws IOException {
            var requestedResource = location.createRelative(resourcePath);

            // Is this a request to a real file?
            if (requestedResource.exists() && requestedResource.isReadable()) {
                return requestedResource;
            }

            // It seems to be only a frontend-routing request (Single-Page-Application).
            return new ClassPathResource(DEFAULT_STARTING_PAGE);
        }
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**")
                .addResourceLocations("classpath:/static/")
                .resourceChain(true)
                .addResolver(new ReactRoutingPathResourceResolver());
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant