代码之家  ›  专栏  ›  技术社区  ›  fermoga

通过Zuul网关访问路由时,不考虑用户的权限/角色

  •  0
  • fermoga  · 技术社区  · 5 年前

    我在Zuul Gateway微服务中设置了对URL的权限,因为我通过网关访问端点,所以我认为应该在这里设置规则。

    客户端、令牌、角色和权限都存储在数据库中。

    roles

    enter image description here

    authorities

    enter image description here

    ADMIN 能读写,同时 USER 我只能看书。我在以下位置生成令牌用户我的管理员凭据: oauth/token ,然后在 oauth/check_token ,详情如下:

    GET http://localhost:8765/authorization-service/oauth/check_token?token=f23e9fcc-1a85-43c4-8ef2-4efd00a7ccf5
    
    {
        "active": true,
        "exp": 1604879905,
        "user_name": "gamofe",
        "authorities": [
            "ROLE_ADMIN",
            "CAN_MANAGE",
            "CAN_READ"
        ],
        "client_id": "client",
        "scope": [
            "READ",
            "WRITE"
        ]
    }
    

    在Zuul gateway微服务中,我创建了一个 @Configuration 类扩展 WebSecurityConfigurerAdapter 将规则应用于路线。在这里,我设置了允许所有人进入 authorization-service/** /user-service/api/v1/users/** 扮演什么角色 管理 使用者

    @Configuration
    @EnableWebSecurity
    public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter {
        public static final String USERS_URI = "/user-service/api/v1/users/**";
        
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http
                .cors().and()
                .csrf().disable()
                .formLogin().disable()
                .sessionManagement()
                    .sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
                .authorizeRequests()
                    .antMatchers("/authorization-service/**").permitAll()
                    .antMatchers(USERS_URI).hasAnyRole("ADMIN", "USER")
                    .anyRequest().authenticated().and()
                .oauth2ResourceServer().opaqueToken();
        }
    }
    

    我还在应用程序中设置instrospection uri。yml文件:

    spring:
      security:
        oauth2:
          resourceserver:
            opaquetoken:
              introspection-uri: 'http://localhost:8765/authorization-service/oauth/check_token'
              client-id: 'client'
              client-secret: 'pass'
    

    .anyRequest().authenticated().and() 有效地检查用户是否确实经过身份验证,其权限和角色被忽略,始终禁止访问受保护的路由。

    $ curl --location --request GET 'http://localhost:8765/user-service/api/v1/users' --header 'Authorization: Bearer f23e9fcc-1a85-43c4-8ef2-4efd00a7ccf5'
    {"timestamp":"2020-11-08T23:26:21.012+00:00","status":403,"error":"Forbidden","message":"Access Denied","path":"/user-service/api/v1/users"}%  
    

    我试过了 hasAuthority ,我试着用很多方法修改端点,我不知道缺少了什么。如果我移除 hasAnyRole 这将再次允许我,它似乎无法看到用户拥有的权限。

    我不知道还有什么要补充的,所以请问我。或者如果你感觉很好,你可以 check my repository.

    0 回复  |  直到 5 年前