Spring 从文件启动 http 安全 jwt 密钥

Spring boot http security jwt key from file

目前我正在使用以下配置进行身份验证,为每个请求解密授权令牌的 jwt 添加密钥:

security:
   oauth2:
      resource:
         jwt:
            keyValue: test

下面是使用它来只允许特定端点的 http 安全配置

@Configuration
@EnableResourceServer
@ConditionalOnProperty(name = "securityEnabled")
public class ResourceServiceConfiguration extends ResourceServerConfigurerAdapter {

@Override
public void configure(HttpSecurity httpSecurity) throws Exception {     
    httpSecurity
        // we don't need CSRF because our token is invulnerable
        .csrf().disable()

        // don't create session
        .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()

        .authorizeRequests()

        // allow anonymous requests to root
        .antMatchers( HttpMethod.GET, "/" ).permitAll()
        .anyRequest().hasAuthority("admin");
}
}

我想做的是,不是从 application.yml 中获取这个键值,而是在服务启动期间以某种方式添加这个键值,从文件系统中获取它并让资源服务器配置剩下的交给我。无休止的谷歌搜索让我无处可去,尽管我最好的猜测是自动装配身份验证管理器并覆盖该方法,但我认为这不适用于 spring.

中的 http 安全性

如有帮助将非常感谢。

在我看来,您想执行 Externalized Configuration 中详述的操作。

例如有一个像 yourfile.properties 这样的文件,其中包含

security.oauth2.resource.jwt.key-value=yourkey

然后使用 --spring.config.location=yourfile.properties 参数启动您的应用程序。