spring-boot - Spring Boot 安全性允许来自给定 IP 地址的请求
问题描述
我们有以下安全配置代码,
@Override
protected void configure(HttpSecurity httpSecurity) throws Exception {
httpSecurity.cors().and().csrf().disable().authorizeRequests().antMatchers("/api/**").anyRequest()
.authenticated().and().exceptionHandling().accessDeniedPage("/").and()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
}
GET /api/users
POST /api/users
GET /api/users/{userId}
我们需要在 Spring Boot 应用程序中限制以下请求(不是所有请求),并且只允许这些请求ipaddress (multiple ipaddress)
在属性中给定。
解决方案
尝试以下配置:
@Override
protected void configure(HttpSecurity httpSecurity) throws Exception {
httpSecurity.cors().and().csrf().disable().authorizeRequests()
.antMatchers("/api/users/**").hasIpAddress("127.0.0.1")
.and()
.authorizeRequests()
.anyRequest()
.authenticated().and().exceptionHandling().accessDeniedPage("/").and()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
}
推荐阅读
- r - 基于R中的日期和小时以15分钟的间隔聚合数据
- c - C中OO继承的别名问题
- c++ - 使用未初始化的结构变量作为参数调用函数不会产生警告
- node.js - 如何在不硬编码查找索引的情况下使用符号获取节点超时的内部 id?
- vue.js - 如何在 main.js 中导入 javascript 库以在整个 vue 应用程序中全局可用?
- mysql - GROUP_CONCAT(DISTINCT xxx) 反转连接值的顺序
- java - 在 Eclipse 中为 JavaFx 打开设计选项卡时出错
- python - 如何使用 Pandas 数据框将 R 代码语法转换为 Python 语法?
- javascript - 将 props 从子组件传递给父组件
- list - 如何将列表中的每个数字与另一个列表中的每个数字相乘