为SPA前端配置Spring Boot [英] Configure Spring Boot for SPA frontend

查看:511
本文介绍了为SPA前端配置Spring Boot的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有整个前端部分正在铺设资源的应用程序。我想将事情分开。例如,为用户界面提供单独的服务器。

所以我假设我的服务器应该为客户端呈现的所有请求返回 index.html

例如:我有'user /:id'路由,它是通过角路由管理的,并且不需要任何服务器。我如何配置,以便服务器不会重新加载或重定向到任何地方?



我的安全配置如下(不知道它是否对此负责):

  public class Application extends WebSecurityConfigurerAdapter {
$ b @Override
protected void configure(HttpSecurity http)throws Exception {
http.antMatcher(/ ** ).authorizeRequests()。antMatchers(/,/ login **,/ webjars / **,/ app / **,/app.js)
.permitAll() .anyRequest()。authenticated()。和()。exceptionHandling()
.authenticationEntryPoint(new LoginUrlAuthenticationEntryPoint(/))。和().logout()
.logoutSuccessUrl(/) .permitAll()。和()。csrf()
.csrfTokenRepository(csrfTokenRepository())。和()
.addFilterAfter(csrfHeaderFilter(),CsrfFilter.class)
.addFilterBefore(ssoFilter (),BasicAuthenticationFilter.class);


$解析方案

本指南<使用自然路线 (特别是

  @控制器
公共类RouteController {
@RequestMapping(value =/{{{{:\\\\.]*})
public String redirect(){
return 前锋:/;


然后使用Spring Boot, index.html / 处加载,并且可以加载资源;路线由Angular处理。


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);
    } 

解决方案

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.

这篇关于为SPA前端配置Spring Boot的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆