簡體   English   中英

Spring Boot 2 OAuth2不提供靜態內容

[英]Spring Boot 2 OAuth2 not serving static content

我創建了兩個應用程序,即App-Server和App-Client。

App Server是Spring Boot 2和Spring Security OAuth2。

應用客戶端是Angular JS 6。

當我分別運行兩個應用程序時,它們運行良好,但是當我嘗試使用App-Server提供靜態內容(應用程序客戶端)時,則無法正常工作。

我正在使用以下代碼將靜態文件添加到App-Server。

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
        <plugin>
            <artifactId>maven-resources-plugin</artifactId>
            <executions>
                  <execution>
                      <id>copy-resources</id>
                      <phase>validate</phase>
                      <goals><goal>copy-resources</goal></goals>
                      <configuration>
                          <outputDirectory>${basedir}/target/classes/static/</outputDirectory>
                          <resources>
                              <resource>
                                  <directory>${basedir}/../app-client/dist/app-client</directory >
                              </resource>
                          </resources>
                      </configuration>
                  </execution>
            </executions>
        </plugin>
    </plugins>
</build>

我的網絡安全看起來像

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
    @Order(Ordered.HIGHEST_PRECEDENCE)
    protected void configure(HttpSecurity http) throws Exception {
        http
        .sessionManagement()
        .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
        .and()
        .csrf().disable()
        .authorizeRequests()
        .antMatchers("/about","/signup","/oauth/token","/users/otp").permitAll()
        .anyRequest().authenticated()
        .and().logout().logoutSuccessUrl("/").permitAll()
        .and()
        .httpBasic()
            .realmName(REALM);
    }
//Some other code .....
}

我的任何ResourceServerConfig如下所示

    @Configuration
    @EnableResourceServer
    public class ResourceServerConfig extends ResourceServerConfigurerAdapter

 {

    @Autowired
    private TokenStore tokenStore;

    @Override
    public void configure(ResourceServerSecurityConfigurer resources)throws Exception{
        resources.resourceId("app-server-api").tokenStore(tokenStore);
    }

    @Override
    public void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
        .antMatchers("/","/users/otp", "/index.html","/resources/**","/images/**")
        .permitAll()
        .antMatchers("/v1/**")
        .authenticated();
    }

}

每當我嘗試訪問任何靜態文件時,我都沒有任何回復。

但是在當地,我可以看到

在名稱為'dispatcherServlet'的DispatcherServlet中找不到帶有URI [/api/index.html]的HTTP請求的映射

...

我所做的更改很少,首先,我(通過解壓縮)檢出了jar,發現沒有可用於Static Content的文件。

經過幾次試用和運行,我發現在pom.xml中,我需要正確地映射來自app-client的ui內容。 我做了以下。

<plugin>
            <artifactId>maven-resources-plugin</artifactId>
            <executions>
                  <execution>
                      <id>copy-resources</id>
                      <phase>validate</phase>
                      <goals><goal>copy-resources</goal></goals>
                      <configuration>
                          <outputDirectory>${basedir}/target/classes/static/</outputDirectory>
                          <resources>
                              <resource>
                                  <directory>${basedir}/../app-client/dist</directory >
                              </resource>
                          </resources>
                      </configuration>
                  </execution>
            </executions>
        </plugin>

請注意,我們需要在<directory>標記下使用..使其與目錄結構匹配。

希望這對某人有所幫助。

暫無
暫無

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

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