我在zuul网关后面有一组服务,其中一个是auth服务器。我将其配置为使用从/.well-known/jwks.json
的身份验证服务器设置的jwk为每个服务上的用户解析jwt并使用密码授予访问简单端点的权限,但是我想知道是否可以通过以下方式确定情况在这些服务和端点必须调用其他服务的情况下,哪个控制器和端点正在使用用户的访问令牌,而不是在使用服务的客户端凭据的情况下。例如:
我有一个管理客户的联系服务,以及另一个管理库存的服务。
当用户想要查看哪些客户正在与哪个库存进行交互时,我可以使用OAuth2RestTemplate
来调用其他服务,
@RequestMapping("/sales/{id}")
public Map<Object, Customer> getSales(@PathVariable Long customerId) {
Object inventory = restTemplate.getForObject("http://inventory-service/inventory", Object.class);
Customer customer = repository.findById(customerId);
Map<Object, Customer> sales = new HashMap;
sales.put(customer, inventory);
return sales;
}
即使我尝试将客户服务配置为使用客户端凭据流而不是授权代码流,我仍得到500的响应A redirect is required to get the users approval
。
客户服务的安全设置:
security:
oauth2:
resource:
jwk:
key-set-uri: ${GATEWAY:http://localhost:8762}/.well-known/jwks.json
client:
client-id: first-client
client-secret: noonewilleverguess
access-token-uri: ${GATEWAY:http://localhost:8762}/oauth/token
,其主类带有@SpringBootApplication
,@EnableGlobalMethodSecurity(prePostEnabled = true)
和@EnableResourceServer
注释。
这是上下文的更多配置
@EnableWebSecurity
public class SecurityConfig extends ResourceServerConfigurerAdapter {
@Override
public void configure(HttpSecurity http) throws Exception {
http.csrf().disable().authorizeRequests().anyRequest().permitAll();
}
@LoadBalanced
@Bean
public OAuth2RestTemplate restTemplate(OAuth2ClientContext clientContext,
OAuth2ProtectedResourceDetails resourceDetails) {
return new OAuth2RestTemplate(resourceDetails, clientContext);
}
文档显示,在公开@EnableOAuth2Client
时不必使用OAuth2RestTemplate
,因此我省略了该注释。
理想情况下,我希望能够选择哪些请求使用用户的访问令牌,哪些请求使用服务的客户端凭据,但是我没有找到任何这样做的资源。甚至有可能吗?