我想允许匿名访问URL,仅限于特定的IP地址子网。
URL为:
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();
}
但一旦我用hasIpAddress()
替换permitAll()
,我就会被重定向到我的登录页面。
如何允许受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:
最后的这段代码强制显示登录表单。我希望它不会像通过了IP地址白名单一样考虑到这一点。
.and()
.authorizeRequests()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.permitAll();
您应该通过启用Spring Security日志来获得更多信息:
-Dlogging.level.org.springframework.security=TRACE
最有可能的情况是,一个hasIpAddress
匹配,角色ROLE_ANONYMOUS
被赋予发出请求的用户。除非在安全配置中启用了anonymous()
,否则不会启用该角色。
请参阅以下其他问题:
- Spring Security在rest服务中获取用户信息,用于已验证和未验证的用户
- Spring Security permitAll((不允许匿名访问