keets2012 / Auth-service

spring security + oauth2.0 + jwt
MIT License
659 stars 267 forks source link

博主,请教一下传参的问题。 #7

Open summer1969 opened 6 years ago

summer1969 commented 6 years ago

首先非常感谢博主的几篇文章。看完那几篇文章后,我在使用博主demo的过程中遇到两个问题想请教下: 1 例如我想根据用户id查询用户的所有Permission。我调用的是UserRoleResource类的@Path("/userPermissions") 方法。postman的get请求: localhost:10101/authserver/userPermissions?access_token="太长,略去"&userId=af6ef7a9-413c-4727-9ca9-0ea00aed99bc&client=frontend 响应结果: { "timestamp": 1524385914909, "status": 404, "error": "Not Found", "message": "No message available", "path": "/authserver/userPermissions" } 是参数哪里有问题吗? 2 使用资源的数据库中定义,要打开代码中的哪些注释? 我尝试打开类ResourceServerConfig的注释,发现不起作用,打开哪些注释权限的校验会进入到SecurityAccessDecisionManager里的decide方法去判断用户是否有权限访问资源。

CANGWU commented 6 years ago

第一个问题中,请求参数的问题可以参考backend-server中的定义的FeignAuthClient类,上述访问中失败的原因是路径错误了,通过网关直接访问路径应该http://localhost:10101/auth/api/userPermissions,直接访问的地址为http://localhost:9091/api/userPermissions

第二问题中,我们的后端服务不是资源服务器,如果想在后端服务中使用权限控制的行为,可以尝试使用项目中定义的@PreAuth注解开启鉴权切面。如果想使用资源服务器的中的鉴权功能,需要配置资源服务器(使用@EnableResourceServer和ResourceServerConfigurerAdapter配置资源服务器 )的HttpSecurity,比如下面的例子,将会配置对应的FilterSecurityInterceptor进行权限拦截(其内使用AccessDecisionManager进行判定)。

    @Override
    public void configure(HttpSecurity http) throws Exception {
        http.csrf().disable()
                .requestMatchers().antMatchers("/**")
                .and()
                .authorizeRequests()
                .anyRequest().authenticated()
                .antMatchers("/user/**").hasRole("user")
                .antMatchers("/admin/**").hasAuthority("ROLE_ADMIN")
                .antMatchers("/resource/**").access("hasRole('ADMIN') and hasAuthority('ROLE_ADMIN')") ;
    }