在springboot java项目中捆绑angular ui项目并访问其UI

时间:2019-03-06 06:39:36

标签: angular spring-boot spring-security

我的项目结构如下:

my-app/pom.xml
my-app/my-app-service/pom.xml
my-app/my-app-ui/pom.xml

在my-app / pom.xml内部

...
<modules>
    <module>my-app-ui</module>
    <module>my-app-service</module>
</modules>
...

在my-app / my-app-service / pom.xml内部

...
<packaging>jar</packaging>
...
<plugin>
    <artifactId>maven-resources-plugin</artifactId>
    <executions>
        <execution>
            <id>copy-resources</id>
            <phase>validate</phase>
            <goals>
                <goal>copy-resources</goal>
            </goals>
            <configuration>
                <outputDirectory>${project.build.directory}/classes/resources/public/</outputDirectory>
                <resources>
                    <resource>
                        <directory>${project.parent.basedir}/my-app-ui/dist/my-app-ui/</directory>
                    </resource>
                </resources>
            </configuration>
        </execution>
    </executions>
</plugin>
...

在my-app / my-app-ui / pom.xml内部

...
<build>
    <plugins>
        <plugin>
            <groupId>com.github.eirslett</groupId>
            <artifactId>frontend-maven-plugin</artifactId>
            <version>1.7.5</version>
            <configuration>
                <nodeVersion>v10.14.1</nodeVersion>
                <npmVersion>6.4.1</npmVersion>
            </configuration>
            <executions>
                <execution>
                    <id>install node and npm</id>
                    <goals>
                        <goal>install-node-and-npm</goal>
                    </goals>
                </execution>
                <execution>
                    <id>npm install</id>
                    <goals>
                        <goal>npm</goal>
                    </goals>
                </execution>
                <execution>
                    <id>npm run build</id>
                    <goals>
                        <goal>npm</goal>
                    </goals>
                    <configuration>
                        <arguments>run build</arguments>
                    </configuration>
                </execution>
                <execution>
                    <id>prod</id>
                    <goals>
                        <goal>npm</goal>
                    </goals>
                    <configuration>
                        <arguments>run-script build</arguments>
                    </configuration>
                    <phase>generate-resources</phase>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
...

在my-app / my-app-ui / dist / my-app-ui内部:index.htmlfavicon.icoall jsall .map,包括{{1} }文件夹

内部SecurityConfig.java:

assets

MyAppConfigSupport.java内部

@EnableWebSecurity
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
...
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.httpBasic().and().authorizeRequests()
            .antMatchers("/", "/**").permitAll()
            .antMatchers("/rest/api/**").hasAnyAuthority(adminRead, adminReadWrite)
            .and().formLogin().loginPage("/index.html").permitAll()
            .and().logout().deleteCookies("JSESSIONID");
        http.authorizeRequests().anyRequest().authenticated();
        http.csrf().disable();
    }
...
}

swagger.properties内部

@Configuration
@PropertySource("classpath:swagger.properties")
@EnableSwagger2
public class MyAppConfigSupport extends WebMvcConfigurationSupport {
...
    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
        registry.addResourceHandler("/**").addResourceLocations("classpath:/public/");
        registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }
...
}

内部application.properties

springfox.documentation.swagger.v2.path=/swagger
  1. 我正在将jar作为java -jar my-app-service.jar运行在目标文件夹中。
  2. 我能够访问... server.servlet.context-path=/my-app-services ... 并进行所有REST调用。
  3. 我可以看到https://localhost:8080/my-app-services/swagger-ui.html文件夹中的所有ui文件。

问题:我无法访问my-app-service/BOOT-INF/classes/resources/public/https://localhost:8080/my-app-services/的错误是https://localhost:8080/my-app-services/index.html

不知道问题出在哪里。任何帮助深表感谢!预先感谢。

0 个答案:

没有答案