gpt4 book ai didi

java - 如何在 Java 中使用 Spring security OAuth2 在资源服务器中动态配置 Httpsecurity?

转载 作者:行者123 更新时间:2023-11-30 06:55:49 25 4
gpt4 key购买 nike

我是 Spring OAuth 2 的新手。我想知道如何使用 Spring OAuth 2.0 在资源服务器中动态配置 Httpsecurity。因为我有存储在数据库中的 antMatchersscopes 列表(将来可能是 antMatchersscopes可以添加)。

对于单个 antMatchersscope 我已经尝试过如下所示并且它按预期工作:

@Override
public void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/adminservice/")
.access("#oauth2.hasScope('Products')");
}

如果数据库中存储了 antMatchersscopes 的列表,我应该如何配置它,例如:

**antMatchers**     **scopes**
/adminservice/ products
/xxxx/ yyyy
/yyyy/ xxxx
/jkjlk/ uyuuy
/klklk/ hjkskjk

在这里,当 spring 启动时,它自己想要在资源服务器中动态地从数据库配置具有相应 scopeantMatchers

我怎样才能做到这一点?

最佳答案

根据您的数据库访问机制,只需 Autowiring 您的 jdbcTemplate、entityService 并执行以下操作:

@Override
public void configure(HttpSecurity http) throws Exception {
List<Matcher> matchers=matchersService.getAll();
for(Matcher m : matchers) {
http.authorizeRequests()
.antMatchers(m.getMapping())
.access("#oauth2.hasScope('"+m.getScope()+"')");
}
}

现在将解决启动期间的配置问题。如果您希望能够在运行时动态更改它,我会简单地建议重新启动服务器(如果可以的话)。

关于java - 如何在 Java 中使用 Spring security OAuth2 在资源服务器中动态配置 Httpsecurity?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34915562/

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