【问题标题】:How to dynamically configure Httpsecurity in the Resource server using Spring security OAuth2 in Java?java - 如何使用Java中的Spring security OAuth2在资源服务器中动态配置Httpsecurity?
【发布时间】:2016-04-27 05:09:34
【问题描述】:

我是 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 启动时,它自己想要在资源服务器中动态地从数据库中配置 antMatchers 和相应的 scope

我怎样才能做到这一点?

【问题讨论】:

    标签: java spring spring-security oauth-2.0


    【解决方案1】:

    根据您的数据库访问机制,只需自动装配您的 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()+"')");
        }
    }
    

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

    【讨论】:

    • Matcher 是您数据库中的实体吗?
    猜你喜欢
    • 2017-09-13
    • 2017-03-29
    • 2017-03-25
    • 2016-03-14
    • 2013-12-21
    • 2017-02-13
    • 2019-11-27
    • 2016-01-18
    • 1970-01-01
    相关资源
    最近更新 更多