angular - 由后端提供的带有角度的 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 种类型的查询:
- /api/private/** => 只有经过身份验证
- /api/public/** => 所有人
- 任何静态资源,例如 index.html 或 logo.png => 每个人
- /dashboard 或 /profile => route angular 应该由 spring security 允许并且将由 angular router 处理
我应该如何更新我的 spring 安全配置以不阻止静态内容?除了 /api/private/** (应该经过身份验证)之外,使用 permitAll on /** 设置 spring 安全性是否安全,因此所有静态内容都可用?
解决方案
您可以添加多个 antmachers
.antMatchers("/").permitAll() //root level
.antMatchers("/api/public/**,/index.html**").permitAll()
在 antMatchers 中添加/ ** 将允许用户访问所有内容。其他不匹配的 url 将由 spring security 自动处理进行身份验证
推荐阅读
- php - 如何使整个确认按钮成为 laravel 控制器中 sweetalert2 中的链接
- c# - 使用 uwp 应用程序在两个 Windows 设备之间通过 wifi 共享文件
- jquery - 在最后一张幻灯片之后单击屏幕右侧的第 1 个/在第 2 个幻灯片之后的第 1 个中单击屏幕左侧
- python - 将奇怪的 json 文件读入 pandas
- vim - vim的CA的奇怪行为?
- azure - 具有共享 azure arm 模板的多个项目
- c# - 检查指定选中列表框中的所有项目c#
- android - 如何解析这个树结构的 json 响应以在 android 中形成回收器树视图?
- powershell - 使用变量名调用 .cmd 文件
- android - 已填充后将项目添加到微调器