Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Spring引导上使用OAuth2保护特定资源id_Spring_Spring Boot_Oauth - Fatal编程技术网

在Spring引导上使用OAuth2保护特定资源id

在Spring引导上使用OAuth2保护特定资源id,spring,spring-boot,oauth,Spring,Spring Boot,Oauth,我在Spring Boot上有一个工作的OAUTH2实现,AuthorizationServer和ResourceServer在同一个实现上,使用密码授权。 关于服务器: TokenStore是自定义的,并使用Web服务远程存储令牌 我有一个自定义的AuthenticationProvider 这用于根据给定的权限控制对资源的访问,例如,在ResourceServer配置中: public void configure(HttpSecurity http) throws Except

我在Spring Boot上有一个工作的OAUTH2实现,AuthorizationServer和ResourceServer在同一个实现上,使用密码授权。 关于服务器:

  • TokenStore是自定义的,并使用Web服务远程存储令牌
  • 我有一个自定义的AuthenticationProvider
这用于根据给定的权限控制对资源的访问,例如,在ResourceServer配置中:

    public void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
            .antMatchers("/api/resource/**")
                .hasAnyAuthority("USER", "ADMIN")
            .antMatchers("/api/admin/**")
                .hasAnyAuthority("ADMIN");
    }
现在,我需要控制用户可以访问“/api/resource/1”,但不能访问“/api/resource/2”,此ID可以更改,我可以在身份验证期间获取列表

我尝试将ID列表添加到OAuth2AccessToken附加信息中,并在ResourceServer配置中添加自定义筛选器,但它总是为空


那么,在不使用JWT的情况下如何正确地实现这一点呢?

经过一些思考和研究,如果有人试图实现类似的目标,我将这样做:

  • 将允许的ID映射到权限,例如ID_201和作为角色的模块,因此我将有一个角色_ADMIN
  • 可以在Web安全表达式中引用路径变量,如中所述。因此,我们的想法是传递变量(资源id)并检查它是否被允许

    public class WebSecurity {
       public boolean checkResourceId(Authentication authentication, int id) {
            //check if the list of authorities contains ID_{id}.
       }
    }
    
    在Web安全配置中:

    http
    .authorizeRequests()
            .antMatchers("/resource/{resourceId}/**").access("@webSecurity.checkResourceId(authentication,#resourceId)")
            ...
    
如果您正在使用SpringSecurity4.0.4进行SpringBoot的工作,请确保按照本文中的报告升级到4.1.1