问题
I have a Spring Boot
application based on this repository.
The problem is I want to serve a UI5 app at the end.
I need to make a src\main\webapp
folder in my server app to push my static files in.
For making this working I did these steps:
- In my
pom.xml
I have:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
<executions>
<execution>
<id>copy-resources</id>
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target/classes/static</outputDirectory>
<resources>
<resource>
<directory>src/main/webapp</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
- In my
application.properties
I have:
spring.resources.static-locations=classpath:src/main/webapp
- In my
webapp/WEB-INF/web.xml
I have:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
- And finally in my web config file I have:
@Profile("!dev")
@Configuration
@EnableWebSecurity(debug = true)
@EnableJpaRepositories(basePackages = "me.cimply.ask.odata.repository")
@EnableGlobalMethodSecurity(
securedEnabled = true,
jsr250Enabled = true,
prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
private final CustomUserDetailsService userDetailsService;
private final JwtAuthenticationEntryPoint jwtEntryPoint;
@Autowired
public WebSecurityConfig(CustomUserDetailsService userDetailsService, JwtAuthenticationEntryPoint jwtEntryPoint) {
this.userDetailsService = userDetailsService;
this.jwtEntryPoint = jwtEntryPoint;
}
@Override
@Bean
public AuthenticationManager authenticationManager() throws Exception {
return super.authenticationManager();
}
@Bean
public JwtAuthenticationFilter jwtAuthenticationFilter() {
return new JwtAuthenticationFilter();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
}
@Override
public void configure(WebSecurity web) {
web.ignoring().antMatchers("/v2/api-docs", "/configuration/ui", "/swagger-resources/**", "/configuration/**",
"/swagger-ui.html", "/webjars/**", "/index.html*", "/odata.svc/$metadata*");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.cors()
.and()
.csrf().disable()
.exceptionHandling().authenticationEntryPoint(jwtEntryPoint)
.and()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.authorizeRequests()
.antMatchers("/",
"/favicon.ico",
"/**/*.json",
"/**/*.xml",
"/**/*.properties",
"/**/*.woff2",
"/**/*.woff",
"/**/*.ttf",
"/**/*.ttc",
"/**/*.ico",
"/**/*.bmp",
"/**/*.png",
"/**/*.gif",
"/**/*.svg",
"/**/*.jpg",
"/**/*.jpeg",
"/**/*.html",
"/**/*.css",
"/**/*.js").permitAll()
.antMatchers("/**/api/auth/**").permitAll()
.anyRequest().authenticated();
http.addFilterBefore(jwtAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class);
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@Bean
public CorsFilter corsFilter() {
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
CorsConfiguration config = new CorsConfiguration();
config.setAllowCredentials(true);
config.addAllowedOrigin("*");
config.addAllowedHeader("*");
config.addAllowedMethod("*");
source.registerCorsConfiguration("/**", config);
return new CorsFilter(source);
}
}
The problem is when I run this app with mvn spring-boot:run
everything is fine and I can visit http://localhost:9090/
and it serves the index.html
without mentioning the index.html
in the url!.
However when I deploy it in Tomcat or serve it with internal Tomcat server inside the Eclipse, when I visit http://localhost:8080/my-app/
it does not serve the index.html
and shows so called Whitelabel Error Page (404)
. But if I visit http://localhost:8080/my-app/index.html
then it serves the index.html
file perfectly. It seems there is something wrong in my WebSecurityConfig
class that when I visit /
it will send the error page.
The other interesting point is, if I remove this line from application.properties
:
spring.resources.static-locations=classpath:src/main/webapp
and change the name of the webapp
folder to static
then it serves the index.html
when I visit the /
. However, then my UI5 app does not work, and I need to keep the name of this folder as it is defined in the UI5 apps (i.e. webapp
)!
I think I have to somehow tell WebSecurityConfigurerAdapter
that this webapp
folder is static and do not run security check for that!
Please notice that I don't have any MVC in this app, I searched a lot and most of the solutions was referring to MVC configuration, however I don't use MVC in this app.
来源:https://stackoverflow.com/questions/63175074/websecurityconfigureradapter-does-not-serve-index-html-when-its-name-does-not-m