Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.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
Java SpringMVC,Restful,API,如何匹配url以控制权限_Java_Api_Rest_Spring Mvc_Privilege - Fatal编程技术网

Java SpringMVC,Restful,API,如何匹配url以控制权限

Java SpringMVC,Restful,API,如何匹配url以控制权限,java,api,rest,spring-mvc,privilege,Java,Api,Rest,Spring Mvc,Privilege,SpringMVC,restfulapi GET/order/{orderId} POST/order/{orderId}/abc/{abcId}-{bcdId} POST/order/{orderId}/myresource/{subResources:[a-zA-Z0-9_/]+} 角色1可以调用api1 角色2可以调用api1、api2和api3 如何匹配API路径的url 对不起,我的英语很差。如果您使用的是基于Java的配置,则可以执行以下操作: @Configuration publ

SpringMVC,restfulapi

GET/order/{orderId}


POST/order/{orderId}/abc/{abcId}-{bcdId}

POST/order/{orderId}/myresource/{subResources:[a-zA-Z0-9_/]+}

角色1可以调用api1 角色2可以调用api1、api2和api3

如何匹配API路径的url


对不起,我的英语很差。

如果您使用的是基于Java的配置,则可以执行以下操作:

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
            .requestMatchers(new AntPathRequestMatcher("/order/*", HttpMethod.GET.name())).hasAnyRole("ROLE1", "ROLE2")
            .requestMatchers(new AntPathRequestMatcher("/order/*/abc/*", HttpMethod.POST.name())).hasRole("ROLE2")
            .requestMatchers(new AntPathRequestMatcher("/order/*/myresource/**", HttpMethod.POST.name())).hasRole("ROLE2");
    }
}
这只是显示了可以应用于URL的基于角色的授权配置,而不是完整的Spring安全配置。关于url匹配角色授权的问题

您还可以使用许多其他RequestMatcher实现。如果ant路径匹配对您来说还不够,那么您也可以实现自己的

使用相同结果执行此操作的另一种完全不同的方法是在配置文件中使用注释@EnableGlobalMethodSecurity启用全局方法安全性。然后在服务/端点中使用@Secured、@PreAuthorize或@PostAuthorize注释之一。例如:

@RequestMapping(value="/order/{orderId}", method=RequestMethod.GET)
@Secured(value = {"ROLE1", "ROLE2"})
public @ResponseBody Order getOrder(@PathVariable("orderId") String orderId) {
    ...
}

同样,这只是展示了如何将角色授权应用于端点,而不是Spring安全性所需的所有配置。

角色数据是从数据库中选择的,因此@Secured不是fix/order/{orderId}/abc/{abcId}-{bcdId}/order/{orderId}/abc/{abcId}如何匹配您应该使用@PreAuthorize然后与支持的表达式之一匹配,或者您可以实现自己的PermissionEvaluator并将其作为Bean公开。然后在@PreAuthorize中,您将使用hasPermissionuser、orderId