Java @EnableWebSecurity 和 @EnableWebMvcSecurity 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27906161/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the difference between @EnableWebSecurity and @EnableWebMvcSecurity?
提问by Mike R
@EnableWebSecurity
@启用网络安全
The JavaDoc documentaion:
JavaDoc 文档:
Add this annotation to an
@Configuration
class to have theSpring Security
configuration defined in anyWebSecurityConfigurer
or more likely by extending theWebSecurityConfigurerAdapter
base class and overriding individual methods.
通过扩展基类和覆盖单个方法,将此注释添加到
@Configuration
类中以Spring Security
在任何WebSecurityConfigurer
或更可能的情况下定义配置WebSecurityConfigurerAdapter
。
@EnableWebMvcSecurity
@EnableWebMvcSecurity
The JavaDoc documentaion:
JavaDoc 文档:
Add this annotation to an
@Configuration
class to have theSpring Security
configuration integrate withSpring MVC
.
将此批注添加到
@Configuration
类中以将Spring Security
配置与Spring MVC
.
采纳答案by Steve
If you take a look at those classes, @EnableWebMvcSecurity
actually adds the @EnableWebSecurity
annotation in WebMvcSecurityConfiguration
. Therefore, @EnableWebMvcSecurity
does everything that @EnableWebSecurity
does, and a bit more.
如果你看看那些类,@EnableWebMvcSecurity
实际上@EnableWebSecurity
在WebMvcSecurityConfiguration
. 因此,@EnableWebMvcSecurity
做所有@EnableWebSecurity
能做的事,甚至更多。
What more you ask?
你还问什么?
If you look at WebMvcSecurityConfiguration
, you will see that it adds an AuthenticationPrincipalArgumentResolver
so that you can access the authentication principal by adding an annotation to a controller method argument. i.e.:
如果您查看WebMvcSecurityConfiguration
,您将看到它添加了 ,AuthenticationPrincipalArgumentResolver
以便您可以通过向控制器方法参数添加注释来访问身份验证主体。IE:
public String show(@AuthenticationPrincipal CustomUser customUser) {
// do something with CustomUser
return "view";
}
It also integrates with Spring Web MVC to add a CSRF token to forms.
它还与 Spring Web MVC 集成以向表单添加 CSRF 令牌。
回答by Cassian
As of Spring Security 4.0,
@EnableWebMvcSecurity
is deprecated. The replacement is@EnableWebSecurity
which will determine adding the Spring MVC features based upon the classpath.To enable Spring Security integration with Spring MVC add the
@EnableWebSecurity
annotation to your configuration.
从 Spring Security 4.0 开始,
@EnableWebMvcSecurity
已弃用。替换@EnableWebSecurity
将根据类路径确定添加 Spring MVC 功能。要启用 Spring Security 与 Spring MVC 的集成,请将注释添加
@EnableWebSecurity
到您的配置中。