Spring安全多个hasIPAddress antMatchers

smdnsysy  于 11个月前  发布在  Spring
关注(0)|答案(3)|浏览(211)

我有以下spring安全配置片段:

http
   .authorizeRequests()
   .antMatchers("/tokens").hasIpAddress("10.0.0.0/16")
   ....

字符串
这可以工作,但我也想从127.0.0.1授予对"/tokens"的访问权限。我希望下面的代码沿着工作,但它不工作:

http
   .authorizeRequests()
   .antMatchers("/tokens").hasIpAddress("10.0.0.0/16").hasIpAddress("127.0.0.1/32")
   ....

olmpazwi

olmpazwi1#

http
    .authorizeRequests()
    .antMatchers("/tokens").access(
            "hasIpAddress('10.0.0.0/16') or hasIpAddress('127.0.0.1/32')")
....

字符串

mklgxw1f

mklgxw1f2#

尝试在Spring安全配置文件中设置此配置,如下所示

<http auto-config="true" use-expressions="true">
<intercept-url pattern="/tokens**" access="hasIpAddress('10.0.0.0/16') or hasIpAddress('127.0.0.1/32')" />
</http>

字符串

cbjzeqam

cbjzeqam3#

对于Spring Security 6,我认为应该这样做:

http
.authorizeHttpRequests()
.requestMatchers("/token/**")
.access((authentication, context) -> 
new AuthorizationDecision(new IpAddressMatcher("10.0.0.0/16").matches(context.getRequest())));

return http.build();

字符串

相关问题