spring-boot - 文件中的 Spring Boot http 安全 jwt key

标签 spring-boot spring-security spring-cloud spring-security-oauth2

目前,我正在使用以下配置来验证添加 jwt 的 key ,该 key 在每个请求上解密授权 token :

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 获取这个键值,而是在服务启动期间以某种方式添加这个键值,从文件系统中获取它并让资源服务器配置处理其余的事情为我。无休止的谷歌搜索让我一无所获,尽管我最好的猜测是 Autowiring 身份验证管理器并覆盖该方法,但我认为这不适用于 Spring 的 http 安全性。

任何帮助都会很棒,谢谢。

最佳答案

在我看来,您想要执行 Externalized Configuration 中详细说明的操作.

例如,有一个类似 yourfile.properties 的文件,其中包含

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

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

关于spring-boot - 文件中的 Spring Boot http 安全 jwt key ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48407532/

相关文章:

java - 如何更改 Spring Cloud Stream 配置中所有 RabbitMQ 消费者的默认恢复间隔?

spring-boot - 带有@ConditionalOnProperty 的@RefreshScope 不起作用

java - Spring Cloud Dataflow应用程序http返回401未经授权

spring - Spring Data Elasticsearch 2.2.3.RELEASE如何配置Rest高级客户端的socket超时

java - HTTP 状态 405 - 请求方法 'POST' 不支持 Spring Security Java Config

java - 由 : java.net.URISyntaxException 引起:索引 111 处的查询中存在非法字符:由于 URI 中的 {} 导致错误

Spring Social 记住我登录

java - 在 Spring Security 中添加组

java.sql.时间戳值; Spring-boot项目中的嵌套异常是java.sql.SQLDataException

java - Spring Batch WriterNotOpenException