【问题标题】:Bundling angular ui project in springboot java project and accessing its UI在springboot java项目中捆绑angular ui项目并访问其UI
【发布时间】:2019-07-27 18:15:48
【问题描述】:

我的项目结构如下:

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,包括 assets 文件夹

SecurityConfig.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();
    }
...
}

在 MyAppConfigSupport.java 中

@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/");
    }
...
}

在 swagger.properties 中

springfox.documentation.swagger.v2.path=/swagger

在 application.properties 中

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

问题:我无法访问https://localhost:8080/my-app-services/https://localhost:8080/my-app-services/index.html 错误是Whitelabel Error Page 404 not found

不确定问题出在哪里。非常感谢任何帮助!提前致谢。

【问题讨论】:

  • 我遇到了类似的问题,当我将 dist 放入 WEB-INF 时,我无法访问 index.html。我将 dist 移到 WEB-INF 之外,并使用 base-href 属性集进行了角度构建。然后能够得到index.html。没有使用过弹簧靴。所以不能准确给出意见。

标签: angular spring-boot spring-security


【解决方案1】:

最后我能够通过以下更改解决我的问题:

在 SecurityConfig.java 中:

...
.antMatchers("/", "/*.html", "/*.js", "/*.map", "/assets/**", "/*.ico").permitAll()
...

在 my-app/my-app-service/pom.xml 中

...
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
...
<plugin>
                <artifactId>maven-resources-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-ui</id>
                        <phase>validate</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/classes/static/</outputDirectory>
                            <resources>
                                <resource>
                                    <directory>${project.parent.basedir}/my-app-ui/dist/my-app-ui/</directory>
                                    <excludes>
                                        <exclude>index.html</exclude>
                                    </excludes>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                    <execution>
                        <id>copy-index-page</id>
                        <phase>validate</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/classes/templates/</outputDirectory>
                            <resources>
                                <resource>
                                    <directory>${project.parent.basedir}/my-app-ui/dist/my-app-ui/</directory>
                                    <includes>
                                        <include>index.html</include>
                                    </includes>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
...

MyAppConfig.java 内部

@Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
        registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
    }

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("index");
        registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
    }

在 my-app-ui/app/index.html 中

&lt;base href="/"&gt;&lt;base href="./"&gt;

【讨论】:

    猜你喜欢
    • 2021-10-08
    • 2014-09-12
    • 2019-01-10
    • 2018-08-08
    • 2015-03-09
    • 1970-01-01
    • 1970-01-01
    • 2017-07-01
    • 1970-01-01
    相关资源
    最近更新 更多