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

标签 java spring spring-security oauth-2.0

我是 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/

相关文章:

java - Spring ExceptionHandler 日志记录不起作用

java - 忘记密码功能 Spring(密码找回)

java - 只有一个用户可以访问 spring security

Spring REST @RequestMapping 采用多种媒体类型

Spring-Security OAuth2 设置 - 无法找到 oauth2 命名空间处理程序

java.lang.NoClassDefFoundError : ProceedingJoinPoint 错误

java - 如何判断矩形是否是黄金矩形?

java - 如何在 javafx 中显示图像的特定部分

java - 如何在 Windows 下以低优先级从 Eclipse 启动?

java - JPA LazyInitializationException - 无法初始化代理