您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

Spring Security配置中的单角色多个IP地址

Spring Security配置中的单角色多个IP地址

您的for循环导致以下配置:

@SuppressWarnings("ALL")
@Configuration
@EnableWebSecurity
public class MyWebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {

        http
            .authorizeRequests()
                .antMatchers("/admin/**").access("hasRole('admin') and hasIpAddress('127.0.0.1')")
                .antMatchers("/admin/**").access("hasRole('admin') and hasIpAddress('192.168.1.0/24')")
                .antMatchers("/admin/**").access("hasRole('admin') and hasIpAddress('0:0:0:0:0:0:0:1')");
    }

    //some other configurations
}

所以对于URL:

http://localhost:9595/admin/checkappeals/211

仅考虑第一个匹配器,请参见HttpSecurity#authorizeRequests

注意匹配器是按顺序考虑的。因此,以下内容无效,因为第一个匹配器匹配每个请求,并且永远不会到达第二个映射:

http.authorizeRequests().antMatchers(“/”) .hasRole(“ADMIN”)

您必须构建类似:

http
    .authorizeRequests()
        .antMatchers("/admin/**").acces("hasRole('admin') and (hasIpAddress('127.0.0.1') or hasIpAddress('192.168.1.0/24') or hasIpAddress('0:0:0:0:0:0:0:1'))";
Java 2022/1/1 18:20:40 有337人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶