簡體   English   中英

通過Spring Boot + Spring Security服務靜態資源

[英]Serving static Resource with spring boot + spring security

我在angularjs應用程序上工作,我嘗試通過Spring Boot服務靜態資源(前端)。

我使用gulp構建項目,並得到以下分發結構: 在此處輸入圖片說明

  • 這是哈希文件夾的內容

在此處輸入圖片說明

我的春季安全配置如下所示:

@Override
protected void configure(HttpSecurity http) throws Exception {
    http
            .exceptionHandling()
            .authenticationEntryPoint(myEntryPoint());
    http
            .authorizeRequests()
            .antMatchers("/__hash__/**").permitAll() 
            .antMatchers("/views/**").permitAll()
            .antMatchers("/index.html").permitAll()
            .antMatchers("/api/**").authenticated()
            .antMatchers("/**").permitAll(); 

    http    // login configuration
            .addFilterAfter(springSecurityFilter(), BasicAuthenticationFilter.class);

    /*http
            .authorizeRequests()
            .anyRequest().authenticated();*/

    http    //logout configuration
            .logout()
            .logoutSuccessHandler(logoutHandler());

    http.csrf().disable();

}

用於提供靜態內容的配置彈簧:

@Configuration
public class MyContentConfig extends WebMvcConfigurerAdapter {

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry
            .addResourceHandler("/**")
            .addResourceLocations("file:///" + "c:/front/") ;
}

@Override
public void addViewControllers(ViewControllerRegistry registry) {
    registry.addViewController("/").setViewName("forward:/index.html");
}

}

經過身份驗證后,我得到了index.html頁面,但是沒有CSS樣式和js。

在此處輸入圖片說明

當我嘗試使用此URL“ https:// localhost:9999 / hash /styles.min.css ”訪問CSS時,出現此錯誤:

 There was an unexpected error (type=Not Acceptable, status=406).
 Could not find acceptable representation

您正在通過http加載livereload.js 如果使用https,則必須通過https加載所有資源。 因此, 通過https加載livereload.js

我解決了這個問題。 它與“ spring-cloud-config-server”有關。 我只是刪除此配置:org.springframework.cloud spring-cloud-config-server

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM