【发布时间】:2019-11-10 16:37:33
【问题描述】:
我有一个使用Identity Server 4 内置在.Net Core 中的授权服务器!它按预期工作,从 Node Js 和 .Net 授权客户端和资源。现在我正在尝试添加一个 Java spring Boot 2 API (jdk 1.8) 作为受保护的资源。我通过使用OAuth2 Boot Documentation 实现了这个目标!到目前为止一切正常。现在,我需要从授权服务器生成的访问令牌中提取声明。这是 JWT 类型的不记名令牌。我对此的实现如下:
@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();
}
问题是当我尝试访问控制器内的声明时,它们不可用。我已经在 Spring Security 内部检查了 DefaultAccessTokenConverter 中的默认 extractAuthentication 方法,实际上它忽略了所有非默认声明。我想到的是创建一个扩展 DefaultAccessToken 转换器的新转换器,如下所示:
@Component
public class CustomAccessTokenConverter extends DefaultAccessTokenConverter {
@Override
public OAuth2Authentication extractAuthentication(Map<String, ?> claims) {
OAuth2Authentication authentication = super.extractAuthentication(claims);
authentication.setDetails(claims);
return authentication;
}
}
但我还没有弄清楚在哪里注入或引用这个新转换器。
【问题讨论】:
-
当前实现适用于 jwt 或 opaque Tokens。我想保留此功能
-
你可以在这里阅读关于 spring security jwts docs.spring.io/spring-security/site/docs/5.2.0.M3/reference/…
标签: java spring spring-boot spring-security spring-security-oauth2