Configure Spring Boot for SPA frontend

2019-01-15 16:37发布

I have application where whole frontend part is laying in resource. I would like to separate things apart. And have separate server for UI, provided by gulp, for example.

So that I assume that my server should return index.html for all requests that are rendered by client side.

Eg: I have 'user/:id' rout that is managing by angular routing and doesn't need server for anything. How can I configure so that server will not reload or redirect me to anywhere?

My security config is following(don't know if it 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);
    } 

2条回答
Juvenile、少年°
2楼-- · 2019-01-15 16:41

For routing, according to this guide at Using "Natural" Routes (specifically here), you have to add a controller that does the following:

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

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

查看更多
贼婆χ
3楼-- · 2019-01-15 17:04

If you are using Angular with Spring Data Rest, I think that the most straightforward way to do it is using angular hash location strategy.

Just putting this in the providers array in your app module:

{ provide: LocationStrategy, useClass: HashLocationStrategy }

and, obviously, import it.

查看更多
登录 后发表回答