java - 我可以在资源服务器启动时禁用颁发者验证吗?
问题描述
我正在尝试使用 Spring Security 设置资源服务器,现在我想在我的机器上运行它,它必须通过 SSH 隧道才能到达令牌颁发者,所以我的应用程序最终调用的 URI 类似于http://localhost:1234/.well-known/openid-configuration
. 然而,这个端点会返回类似https://my-auth-server.my-domain.com
for 的issuer
内容,当框架在启动期间尝试检查两个 URI 是否相等时会产生问题。
我已经能够将其追踪JwtDecoderProviderConfigurationUtils
到此检查发生的位置,但我找不到任何挂钩来操纵它:
JwtDecoders
不公开任何指示它不验证issuer
启动时的属性(源文件)。JwtDecoderProviderConfigurationUtil
使用它自己的私有RestTemplate
,所以我不能向它添加任何拦截器。JwtDecoderProviderConfigurationUtil
是包私有的,所以我无法访问它的任何方法来编写我自己的JwtDecoder
.
我很高兴收到有关如何解决此问题的任何指示!我宁愿不必为了让它工作而复制一大堆代码。
解决方案
您可以创建一个不包含以下内容的自定义 JwtDecoder:JwtIssuerValidator
OAuth2TokenValidator<Jwt> defaultValidators = JwtValidators.createDefault()
下面是一个例子,如果你想用你自己的发行者 URL 创建它,你想验证,即使用
OAuth2TokenValidator<Jwt> defaultValidators = JwtValidators
.createDefaultWithIssuer("http://localhost:8081/auth/realms/CryptoInc");
IE
@Override
protected void configure(HttpSecurity http) throws Exception {
http.cors().and()
.authorizeRequests()
.anyRequest().authenticated()
.and().oauth2ResourceServer()
.jwt()
.decoder(jwtTokenDecoder());
}
private JwtDecoder jwtTokenDecoder() {
NimbusJwtDecoderJwkSupport decoder = (NimbusJwtDecoderJwkSupport)
JwtDecoders.fromOidcIssuerLocation("http://localhost:8081/auth/realms/CryptoInc");
OAuth2TokenValidator<Jwt> defaultValidators = JwtValidators
.createDefault();
OAuth2TokenValidator<Jwt> delegatingValidator =
new DelegatingOAuth2TokenValidator<>(defaultValidators, new CryptoJwtTokenValidator());
decoder.setJwtValidator(delegatingValidator);
return decoder;
}
推荐阅读
- unit-testing - How could I mock the constructor of a node module with jest
- python - 通过已分组数据框的另一列使用 pyspark countDistinct
- python - ImportError: cannot import name 'CUDF_INSTALLED' from 'xgboost.compat'
- azure-ad-b2c - Limit user access in Azure AD B2C
- sql - 用于查找记录的 Oracle 查询
- arrays - 检查字符串仅包含唯一的数组元素,重复字母
- css - 如何在 div 中而不是在窗口中打开 element-ui 抽屉
- datetime - DocusignApi Envelope.completedDateTime 使用什么偏移量
- python - 如何实现乘法表的输入?
- powershell - PowerShell:如何确定 Invoke-WebRequest 是否已超时?