2016-11-23 50 views
2

我有整個前端部件鋪設在資源中的應用程序。我想將事情分開。例如,爲用戶界面提供單獨的服務器。爲SPA前端配置Spring Boot

因此,我假設我的服務器應該返回index.html所有客戶端呈現的請求。例如:我有'用戶/:id'路由管理角路由,不需要服務器的任何東西。我如何配置,以便服務器不會重新加載或重定向到任何地方?

我的安全配置,那麼以下(如果它負責這樣的事情就不知道了):

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

回答

7

路由,根據this guide at Using "Natural" Routes(特別here),你必須添加一個控制器,執行以下操作:

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

然後使用Spring開機後,加載index.html/和資源可以加載;路線由Angular處理。

+0

雖然這並不包含帶有另一個正斜槓的URL。例如。 localhost:8080/im-covered和localhost:8080/im/not/covered – james

+1

@EpicPandaForce {{[path:[^ \\。] *}是什麼意思?它從何而來?我理解正則表達式,我不明白Spring Boot如何處理「路徑」部分。 –

+0

@BobbyBrown不幸的是,我所知道的是,我遵循[本指南](https://spring.io/blog/2015/05/13/modularizing-the-client-angular-js-and-spring-security-part-七),它的工作。 – EpicPandaForce

0

如果你在Spring Data Rest中使用Angular,我認爲最直接的方法是使用角度散列位置策略。

只是把這個提供一個存儲在您的應用程序模塊:

{ provide: LocationStrategy, useClass: HashLocationStrategy }

,很明顯,將其導入。