首页 文章

如何在spring security中使用IP白名单/ oauth / check_token endpoints

提问于
浏览
0

我有两个应用程序(战争),一个充当 Resource Server ,另一个是我的 Auth Server 在两个不同的服务器上运行 . 我正在使用 client_credentials grant_type . 我需要 white list 我的资源服务器的IP,以便其他人不能直接使用post-man或浏览器或任何其他用户代理访问 "/oauth/check_token" endpoints . 以下是auth服务器的代码片段:

@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());
 }
}

我在 Spring 天找到了将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" 之前,所有资源服务器都应该经过身份验证和授权

Edited

我提到了类似的问题:How to find users' IPs in Spring Security?但它没有帮助我,因为在那个问题中他们试图授权资源服务器Api,但在我的情况下我想授权spring-security默认 endpoints ,即; /oauth/check_token

在进一步的调试,我发现如果我删除 .antMatchers(HttpMethod.GET,"/oauth/check_token").access("hasIpAddress('0.0.0.0/0')").accessDecisionManager(accessDecisionManager) 我仍然能够使用 /oauth/check_token endpoints .

然后我尝试在AuthorizationServerSecurityConfigurer.checkTokenAccess("permittAll()")中将fullyAuthenticated更改为permitAll();它开始允许每个人访问check_token endpoints . 这意味着它甚至没有从 .antMatchers(HttpMethod.GET,"/oauth/check_token") 读取配置

现在我很困惑如何配置hasIpAddress()并且我们需要明确提到antMatcher(“/ oauth / check_token”),或者它是默认提供的 .

1 回答

  • 0

    您可以像这样对其进行建模,从而保持这两个要求 - IP白名单和所有请求都经过身份验证 . 如果不需要,您可以删除localhost访问规则:

    http
          .authorizeRequests()
          .antMatchers(GET, "/oauth/check_token")
            .access("isFullyAuthenticated() and
                   (hasIpAddress('IP_OR_NETWORK_OF_RESOURCE_SERVER') 
                    or hasIpAddress('127.0.0.1/32'))")
          .antMatchers("/**").fullyAuthenticated();
    

相关问题