Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.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 @EnableGlobalMethodSecurity vs@EnableWebSecurity_Spring_Rest_Spring Security_Spring Boot - Fatal编程技术网

Spring @EnableGlobalMethodSecurity vs@EnableWebSecurity

Spring @EnableGlobalMethodSecurity vs@EnableWebSecurity,spring,rest,spring-security,spring-boot,Spring,Rest,Spring Security,Spring Boot,我正在使用Spring4开发一个RESTAPI。我想使用Spring安全性保护一些端点,但根据我所读的内容,这可以通过@EnableGlobalMethodSecurity或@EnableWebSecurity实现。不幸的是,我找到的这些文档并没有清楚地解释它们的作用(或者它们是如何比较的)。如果我想使用基于标准关系数据库中声明的数据和关系的身份验证和授权来保护Spring REST API,那么在Spring 4中实现这一点的推荐方法是什么?EnableWebSecurity将通过提供配置。这

我正在使用Spring4开发一个RESTAPI。我想使用Spring安全性保护一些端点,但根据我所读的内容,这可以通过
@EnableGlobalMethodSecurity
@EnableWebSecurity
实现。不幸的是,我找到的这些文档并没有清楚地解释它们的作用(或者它们是如何比较的)。如果我想使用基于标准关系数据库中声明的数据和关系的身份验证和授权来保护Spring REST API,那么在Spring 4中实现这一点的推荐方法是什么?

EnableWebSecurity将通过提供配置。这是您可以在xml配置中使用
标记找到的配置,它允许您根据URL模式、身份验证端点、处理程序等配置访问

EnableGlobalMethodSecurity
为方法提供AOP安全性。它提供的一些注释是
预授权
后授权
。它还支持


为了满足您的需要,最好将两者混合使用。使用REST,您只需使用
@EnableWebSecurity
即可实现所需的所有功能,因为
HttpSecurity#antMatchers(HttpMethod,String…
接受对Http方法的控制

最后一段可以稍微展开吗?我的需要如何使两者更好地结合?您可以使用URL模式来决定资源是否需要授权,
GlobalMethodSecurity
将提供特定用户可以在方法中执行的操作,例如,授权用户名John访问
/foo/maria
,但是,
@PreAuthorize
会截取它,因为它有一个SpEL,告诉用户的名字必须等于path变量的名字。如果您只需要授权或不授权某个URL
EnableWebSecurity
将提供您所需的一切