fix WebConfig for angular routing

This commit is contained in:
Patrick Haßel 2021-10-28 17:01:27 +02:00
parent bb7eb594a0
commit ab39f5a010

View File

@ -3,13 +3,19 @@ package de.ph87.homeautomation.web;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.servlet.config.annotation.CorsRegistry; import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.resource.PathResourceResolver;
import java.io.IOException;
@Configuration @Configuration
@EnableWebSecurity @EnableWebSecurity
@ -33,17 +39,18 @@ public class WebConfig extends WebSecurityConfigurerAdapter implements WebMvcCon
registry.addMapping("/**").allowCredentials(true).allowedOrigins("http://localhost:4200").allowedMethods("*"); registry.addMapping("/**").allowCredentials(true).allowedOrigins("http://localhost:4200").allowedMethods("*");
} }
// @Override @Override
// public void addResourceHandlers(final ResourceHandlerRegistry registry) { public void addResourceHandlers(final ResourceHandlerRegistry registry) {
// registry registry
// .addResourceHandler("/**") .addResourceHandler("/**")
// .addResourceLocations("classpath:/resources/") .addResourceLocations("classpath:/resources/")
// .resourceChain(true) .resourceChain(true)
// .addResolver(new PathResourceResolver() { .addResolver(new PathResourceResolver() {
// protected Resource getResource(String resourcePath, Resource location) throws IOException { protected Resource getResource(String resourcePath, Resource roomLocation) throws IOException {
// Resource requestedResource = location.createRelative(resourcePath); Resource requestedResource = roomLocation.createRelative(resourcePath);
// return requestedResource.exists() && requestedResource.isReadable() ? requestedResource : new ClassPathResource("/resources/index.html"); return requestedResource.exists() && requestedResource.isReadable() ? requestedResource : new ClassPathResource("/resources/index.html");
// } }
// }); });
// } }
} }