gpt4 book ai didi

java - 如何从资源服务器中的 Spring Security OAuth2 Boot 中提取声明?

转载 作者:太空宇宙 更新时间:2023-11-04 09:33:23 24 4
gpt4 key购买 nike

我有一个在 .Net Core 中内置的授权服务器,使用 Identity Server 4 !它正在按预期工作,从 Node Js 和 .Net 授权客户端和资源。现在我尝试添加 Java spring Boot 2 API (jdk 1.8) 作为 protected 资源。我通过使用 OAuth2 Boot Documentation 实现了这个目标!到目前为止一切正常。现在,我需要从授权服务器生成的访问 token 中提取声明。这是 JWT 类型的不记名 token 。我对此的实现如下:

@Configuration
@EnableResourceServer
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfiguration extends ResourceServerConfigurerAdapter {
public String resourceId;

@Autowired
public SecurityConfiguration(@Value("${security.oauth2.resource.id}") String resourceId) {
this.resourceId = resourceId;
}

@Override
public void configure(ResourceServerSecurityConfigurer resources) {
resources.resourceId(this.resourceId);
}

@Override
public void configure(HttpSecurity httpSecurity) throws Exception {
httpSecurity
.csrf()
.disable()
.authorizeRequests()
.antMatchers("/swagger-ui.html", "/webjars/**", "/swagger-resources/**", "/**/api-docs/**", "/actuator/**")
.permitAll()
.and()
.authorizeRequests().anyRequest().fullyAuthenticated();
}

问题是,当我尝试访问 Controller 内的声明时,它们不可用。我已经检查了 Spring Security 内部 DefaultAccessTokenConverter 的默认 extractAuthentication 方法,实际上它忽略了所有非默认声明。我想到的是创建一个扩展 DefaultAccessToken Converter 的新转换器,如下所示:

@Component
public class CustomAccessTokenConverter extends DefaultAccessTokenConverter {

@Override
public OAuth2Authentication extractAuthentication(Map<String, ?> claims) {
OAuth2Authentication authentication = super.extractAuthentication(claims);
authentication.setDetails(claims);
return authentication;
}
}

但我还没有弄清楚在哪里注入(inject)或引用这个新转换器。

最佳答案

不幸的是,Spring Boot auto-configuration似乎没有提供替换 DefaultAccessTokenConverter 的方法,它是 RemoteTokenServices 中的默认 token 转换器。要替换转换器,您必须替换默认创建的 RemoteTokenServices

如果您的转换器是一个 bean,您可以将其设置在您自己的 RemoteTokenServices 对象上,然后您可以在 ResourceServerSecurityConfigurer 上设置该对象(以便它可以在后台应用于 OAuth2AuthenticationManager):

@Configuration
@EnableResourceServer
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfiguration extends ResourceServerConfigurerAdapter {
// ...

@Autowired
private ResourceServerProperties resource;

@Autowired
private CustomAccessTokenConverter customConverter;

@Override
public void configure(ResourceServerSecurityConfigurer resources) {
resources.tokenServices(customTokenServices());
// ..
}

private RemoteTokenServices customTokenServices() {
RemoteTokenServices services = new RemoteTokenServices();
services.setAccessTokenConverter(this.customConverter);

// configure based on .properties file
services.setCheckTokenEndpointUrl(this.resource.getTokenInfoUri());
services.setClientId(this.resource.getClientId());
services.setClientSecret(this.resource.getClientSecret());

return services;
}

// ..


关于java - 如何从资源服务器中的 Spring Security OAuth2 Boot 中提取声明?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56811656/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com