我想允许对URL的匿名访问,仅限于特定的IP地址子网。
网址是:
http://10.102.34.98:880/auth/tokens/revoke/blabla 其中 auth 是 Web 应用的上下文根。
访问IP地址10.102.34.98访问IP地址的子网掩码255.255.255.0trusted.client.subnet属性设置为10.102.34.0/24
匿名访问工作正常:
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
.requestMatchers()
.antMatchers("/login", "/oauth/authorize","/tokens/revoke/**")
.and()
.authorizeRequests()
.antMatchers("/tokens/revoke/**").permitAll()
.and()
.authorizeRequests()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.permitAll();
}
但是,一旦我将permitAll()
替换为hasIpAddress()
,我就会被重定向到我的登录页面。
如何允许受IP地址子网限制的匿名访问?
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
.requestMatchers()
.antMatchers("/login", "/oauth/authorize","/tokens/revoke/**")
.and()
.authorizeRequests()
.antMatchers("/tokens/revoke/**").hasIpAddress(environment.getProperty("trusted.client.subnet"))
.and()
.authorizeRequests()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.permitAll();
}
更新1:
是最后的这段代码强制显示登录表单。我希望它不会将其带入accont,因为它已经通过了IP地址白名单。
.and()
.authorizeRequests()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.permitAll();
您应该通过启用Spring Security日志获得更多信息:
-Dlogging.level.org.springframework.security=TRACE
最有可能的是,正在发生的事情是,一个 hasIpAddress
匹配,ROLE_ANONYMOUS
角色被赋予发出请求的用户。除非在安全配置中启用了 anonymous(),
否则不会启用该角色。
请参阅以下其他问题: