【发布时间】:2018-09-07 05:56:43
【问题描述】:
我有两个应用程序(战争),一个充当Resource Server,另一个是我的Auth Server,在两台不同的服务器上运行。我正在使用client_credentialsgrant_type。我需要白名单我的资源服务器的 IP,这样其他人就不能直接使用 post-man 或浏览器或任何其他方式访问 "/oauth/check_token" 端点用户代理。这是来自身份验证服务器的代码 sn-p:
@EnableAuthorizationServer
@Configuration
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {
@Override
public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
security.checkTokenAccess("isFullyAuthenticated()")
.allowFormAuthenticationForClients().realm(REALM + "/client");
}
...
some other configuration code related to ClientDetails and tokenStore.
...
}
@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
public void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
.anonymous().disable()
.authorizeRequests()
.antMatchers(HttpMethod.GET,"/oauth/check_token").access("isFullyAuthenticated() and hasIpAddress('0.0.0.0/0')").accessDecisionManager(accessDecisionManager)
.and()
.httpBasic()
.authenticationEntryPoint(authenticationEntryPoint)
.and().exceptionHandling().accessDeniedHandler(new OAuth2AccessDeniedHandler());
}
}
我在春季找到了一种将 IP 列入白名单的方法:
@Override
public void configure(HttpSecurity http) throws Exception {
http.csrf().disable();
http
.authorizeRequests()
.anyRequest().access("hasIpAddress('0.0.0.0/0')");
}
但这对我的情况没有帮助,因为它只检查fullyAuthenticated()。我希望所有资源服务器在访问 "/oauth/check_token"
已编辑
我提到过类似的问题:How to find users' IPs in Spring Security?,但这对我没有帮助,因为在那个问题中他们试图授权资源服务器 Api,但在我的情况下,我想授权 spring-security 默认端点IE; /oauth/check_token
在进一步调试时,我发现如果我删除 .antMatchers(HttpMethod.GET,"/oauth/check_token").access("hasIpAddress('0.0.0.0/0')").accessDecisionManager(accessDecisionManager)
我仍然可以使用/oauth/check_token 端点。
然后我尝试在 AuthorizationServerSecurityConfigurer.checkTokenAccess("permittAll()"); 中将 fullyAuthenticated 更改为 permitAll();它开始允许每个人访问 check_token 端点。这意味着它甚至没有从.antMatchers(HttpMethod.GET,"/oauth/check_token")读取配置
现在我很困惑如何配置 hasIpAddress() 以及我们是否需要明确提及 antMatcher("/oauth/check_token") 或者它是默认提供的。
【问题讨论】:
标签: spring-boot spring-security spring-security-oauth2