为SPA前端配置Spring Boot [英] Configure Spring Boot for SPA frontend
问题描述
我有整个前端部分正在铺设资源的应用程序。我想将事情分开。例如,为用户界面提供单独的服务器。
所以我假设我的服务器应该为客户端呈现的所有请求返回 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);
$解析方案
本指南< 然后使用Spring Boot, 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 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):
For routing, according to this guide at Then using Spring Boot, the 这篇关于为SPA前端配置Spring Boot的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
@控制器
公共类RouteController {
@RequestMapping(value =/{{{{:\\\\.]*})
public String redirect(){
return 前锋:/;
index.html
在 /
处加载,并且可以加载资源;路线由Angular处理。index.html
for all requests that are rendered by client side. 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
(specifically here), you have to add a controller that does the following:@Controller
public class RouteController {
@RequestMapping(value = "/{path:[^\\.]*}")
public String redirect() {
return "forward:/";
}
}
index.html
loads at /
, and resources can be loaded; routes are handled by Angular.