我是 Spring OAuth 2 的新手。我想知道如何使用 Spring OAuth 2.0 在资源服务器中动态配置 Httpsecurity
。因为我有存储在数据库中的 antMatchers
和 scopes
列表(将来可能是 antMatchers
和 scopes
可以添加)。
对于单个 antMatchers
和 scope
我已经尝试过如下所示并且它按预期工作:
@Override
public void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/adminservice/")
.access("#oauth2.hasScope('Products')");
}
如果数据库中存储了 antMatchers
和 scopes
的列表,我应该如何配置它,例如:
**antMatchers** **scopes**
/adminservice/ products
/xxxx/ yyyy
/yyyy/ xxxx
/jkjlk/ uyuuy
/klklk/ hjkskjk
在这里,当 spring 启动时,它自己想要在资源服务器中动态地从数据库配置具有相应 scope
的 antMatchers
。
我怎样才能做到这一点?
最佳答案
根据您的数据库访问机制,只需 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/