为 SPA 前端配置 Spring Boot [英] Configure Spring Boot for SPA frontend
问题描述
我有整个前端部分都放在资源中的应用程序.我想把事情分开.并且有单独的 UI 服务器,例如由 gulp 提供.
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.
所以我假设我的服务器应该为客户端呈现的所有请求返回 index.html
.
So that I assume that my server should return index.html
for all requests that are rendered by client side.
例如:我有 'user/:id' 路由,它由角度路由管理,不需要任何服务器.如何配置以便服务器不会重新加载或将我重定向到任何地方?
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);
}
推荐答案
对于路由,根据 本指南位于 Using "Natural" Routes
(特别是 这里),你必须添加一个控制器来执行以下操作:
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:/";
}
}
然后使用Spring Boot,在/
加载index.html
,就可以加载资源了;路由由 Angular 处理.
Then using Spring Boot, the index.html
loads at /
, and resources can be loaded; routes are handled by Angular.
这篇关于为 SPA 前端配置 Spring Boot的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!