【问题标题】:Access OAuth2 Protected api from the Java Batch Application从 Java 批处理应用程序访问受 OAuth2 保护的 api
【发布时间】:2020-04-25 22:30:57
【问题描述】:

我有 Springboot 微服务应用程序包括(发现、Eureka 客户端、Zulu 代理、网关),它配置了 OAUTH2,工作正常。 OAUTH2 配置为内存令牌存储。我已经暴露了休息端点网关

例如:

localhost:8080/hello/gateway

现在我有了 java batch,它将调用微服务应用网关示例(上面的 api)来获得所需的响应。因为它受 OAUTH2 保护,所以我无法直接访问 api。

  • 有没有办法在没有令牌的情况下访问 api,或者我们可以通过从批处理中传递硬编码令牌并在网关中验证来绕过授权逻辑
  • 试图创建一个不会过期的令牌,但是由于它的内存令牌,它在 api 重启后将无法工作
  • 厌倦了创建自定义过滤器,但它没有按预期工作。下面是我的资源服务器代码。
@EnableResourceServer
public class ResourceServerConfig extends ResourceServerConfigurerAdapter {

private static final String RESOURCE_ID = "resource_id";

@Autowired
private AppProperties appProperties;

@Override
public void configure(ResourceServerSecurityConfigurer resources) {
    resources.resourceId(RESOURCE_ID).stateless(false);
}


@Override
public void configure(HttpSecurity http) throws Exception {

        http.
                anonymous().disable()
                .authorizeRequests()
                .antMatchers("/testService/**").authenticated()
                .and()
                //.addFilterBefore(new BatchCustomFilter(), BasicAuthenticationFilter.class)
                .exceptionHandling().accessDeniedHandler(new OAuth2AccessDeniedHandler());


  }

}

如果知道有什么好的方法,请告诉我,非常感谢您的建议。

【问题讨论】:

    标签: java spring-boot spring-security oauth-2.0 microservices


    【解决方案1】:

    是的,已覆盖 WebSecurityConfigurerAdapter 配置方法以忽略特定端点。

    @Override
    public void configure(final WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/testService/testApi/**");
    }
    

    现在我可以在从网络安全中忽略端点后批量访问 Api。

    【讨论】:

      猜你喜欢
      • 2020-04-03
      • 1970-01-01
      • 1970-01-01
      • 2021-11-09
      • 1970-01-01
      • 2012-08-16
      • 2016-12-23
      • 1970-01-01
      • 2020-05-22
      相关资源
      最近更新 更多