@EnableGlobalMethodSecurity vs @EnableWebSecurity

时间:2015-04-18 18:20:36

标签: spring rest spring-security spring-boot

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

1 个答案:

答案 0 :(得分:35)

EnableWebSecurity将通过HttpSecurity提供配置,提供您在xml配置中使用<http></http>标记找到的配置,它允许您根据网址模式,身份验证端点配置访问权限,处理程序等...

EnableGlobalMethodSecurity为方法提供AOP安全性,它将启用的一些注释PreAuthorize PostAuthorize也支持JSR-250There is also more parameters in configuration for you

根据您的需要,最好将两者混合使用。使用REST,您只需@EnableWebSecurity即可实现所需,因为HttpSecurity#antMatchers(HttpMethod,String...)接受对Http方法的控制