【发布时间】:2017-11-02 07:45:34
【问题描述】:
在我的 Spring Boot 项目中,我试图向具有特定 IP 地址的多个管理员用户授予访问权限。
是否可以将单个角色映射到多个 IP 地址?
这是我的安全配置中的代码,它不起作用。 (为简单起见,我给出了硬编码的角色名称和 IP 地址)
@SuppressWarnings("ALL")
@Configuration
@EnableWebSecurity
public class MyWebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
List<String> ipAddresses = new ArrayList<>();
ipAddresses.add("127.0.0.1");
ipAddresses.add("192.168.1.0/24");
ipAddresses.add("0:0:0:0:0:0:0:1");
for (String ip : ipAddresses) {
http.authorizeRequests().
antMatchers("/admin" + "/**")
.access("hasRole('admin') and hasIpAddress('" + ip + "')");
}
}
//some other configurations
}
【问题讨论】:
-
我收到以下错误:HTTP Status 403 -message- description 对指定资源的访问已被禁止。
标签: java security spring-boot spring-security ip-address