Configure Spring Download for SPA interface

I have an application in which the entire part of the interface is in a resource. I would like to share things. And to have a separate server for the user interface provided by gulp, for example.

So, I believe that my server should return index.html for all requests that are displayed by the client.

For example: I have a route "user /: id" that is controlled by angular routing and does not need any server. How to configure so that the server does not restart or redirect me anywhere?

Below is my security configuration (I don’t know if it is responsible for such things):

 public class Application extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.antMatcher("/**").authorizeRequests().antMatchers("/", "/login**", "/webjars/**", "/app/**", "/app.js") .permitAll().anyRequest().authenticated().and().exceptionHandling() .authenticationEntryPoint(new LoginUrlAuthenticationEntryPoint("/")).and().logout() .logoutSuccessUrl("/").permitAll().and().csrf() .csrfTokenRepository(csrfTokenRepository()).and() .addFilterAfter(csrfHeaderFilter(), CsrfFilter.class) .addFilterBefore(ssoFilter(), BasicAuthenticationFilter.class); } 
+3
source share
3 answers

To route according to this guide in Using "Natural" Routes (specifically here ), you must add a controller that performs the following actions:

 @Controller public class RouteController { @RequestMapping(value = "/{path:[^\\.]*}") public String redirect() { return "forward:/"; } } 

Then, using Spring Boot, index.html loaded in / and resources can be loaded; routes are handled by Angular.

+13
source

If you are using Angular with Spring Data Rest, I think the easiest way to do this is to use Angular's hash placement strategy.

Just put this in an array of providers in your application module:

{ provide: LocationStrategy, useClass: HashLocationStrategy }

and obviously import it.

+2
source

EpicPandaForce has an excellent answer, and I would like to expand it. The next endpoint will also allow mapping on nested routes. If you want to have an admin section, you can configure it to return another index.html.

 @Controller class PageController { @GetMapping("/**/{path:[^\\.]*}") fun forward(request: HttpServletRequest): String? { if(request.requestURI.startsWith("/admin")) { return "forward:/admin/index.html" } return "forward:/index.html" } } 

This RequestMapping (or @GetMapping) works by excluding any request that contains a point (for example, "index.html").

0
source

Source: https://habr.com/ru/post/1268333/


All Articles