首页 > 解决方案 > 由后端提供的带有角度的 Spring 安全配置

问题描述

我正在构建一个 webapp,前端为 angular,后端为 java(堆栈 spring-boot、spring security、jwt、...) 我对如何协同工作感到有点困惑。

在开发模式下,它工作正常:

我已经用下面的东西代理了我的后端 api 调用

proxy.conf.js

const PROXY_CONFIG = [{
    context: [
        "/api"
    ],
    target: "http://localhost:8080",
    secure: false
}]

module.exports = PROXY_CONFIG;

并像这样设置弹簧安全配置

    ...

    private static final String[] AUTH_WHITELIST = {
        "/signup",
        "/h2-console/*",
        "/login",
        "/api/public/**",
        "/error",};

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.cors().and().csrf().disable()
                .headers().frameOptions().disable()
                .and()
                .exceptionHandling()
                .authenticationEntryPoint(unauthorizedHandler)
                .and()
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                .authorizeRequests()
                .antMatchers(AUTH_WHITELIST)
                .permitAll()
                .anyRequest().authenticated();
        http.addFilterBefore(jwtAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class);
    }

对于生产模式,我希望后端在一个 jar 中提供由 angular 生成的 api 和前端,并且能够像这样启动我的 webapp:

java -jar myWebapp.jar

让我给你看我的 pom 文件

前端模块 pom.xml :

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    
...
    
    <!-- Build -->
    <build>
        
        <!-- Resources -->
        <resources>
            <resource>
                <directory>./dist</directory>
                <targetPath>static</targetPath>
            </resource>
        </resources>
        
        <!-- Plugin --> 
        <plugins>
            
            <!-- Maven frontend plugin -->
            <plugin>
                <groupId>com.github.eirslett</groupId>
                <artifactId>frontend-maven-plugin</artifactId>
                <version>1.10.0</version>
                <configuration>
                    <workingDirectory>./</workingDirectory>
                    <nodeVersion>v12.16.1</nodeVersion>
                    <npmVersion>6.14.5</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>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

后端模块 pom.xml :

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    ...    

    <!-- Build -->
    <build>        
        <plugins>
            <plugin>
                <artifactId>maven-resources-plugin</artifactId>
                <version>3.1.0</version>
                <executions>
                    <execution>
                        <id>copy-resources-frontend</id>
                        <phase>validate</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${basedir}/src/main/resources/static/</outputDirectory>
                            <resources>
                                <resource>
                                    <directory>${basedir}/../frontend/dist/</directory>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
    
</project>

问题: 现在,我的后端提供 api 和 angular 生成的静态内容。

我有 4 种类型的查询:

我应该如何更新我的 spring 安全配置以不阻止静态内容?除了 /api/private/** (应该经过身份验证)之外,使用 permitAll on /** 设置 spring 安全性是否安全,因此所有静态内容都可用?

标签: angularspring-bootspring-security

解决方案


您可以添加多个 antmachers

.antMatchers("/").permitAll()  //root level
.antMatchers("/api/public/**,/index.html**").permitAll()

在 antMatchers 中添加/ ** 将允许用户访问所有内容。其他不匹配的 url 将由 spring security 自动处理进行身份验证


推荐阅读