Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between @EnableWebSecurity and @EnableWebMvcSecurity?

@EnableWebSecurity

The JavaDoc documentaion:

Add this annotation to an @Configuration class to have the Spring Security configuration defined in any WebSecurityConfigurer or more likely by extending the WebSecurityConfigurerAdapter base class and overriding individual methods.

@EnableWebMvcSecurity

The JavaDoc documentaion:

Add this annotation to an @Configuration class to have the Spring Security configuration integrate with Spring MVC.

  • What exactly does it mean to 'integrate with Spring MVC' ? What extra behaviors do I get?
  • I found guides & answers, which suggest that this annotation adds CSRF Tokens to Spring MVC Forms, is this the only thing it adds?
like image 254
Mike R Avatar asked Jan 12 '15 16:01

Mike R


People also ask

What is @EnableWebSecurity in Spring boot?

The @EnableWebSecurity is a marker annotation. It allows Spring to find (it's a @Configuration and, therefore, @Component ) and automatically apply the class to the global WebSecurity . If I don't annotate any of my class with @EnableWebSecurity still the application prompting for username and password.

What is the use of WebSecurityConfigurerAdapter?

WebSecurityConfigurerAdapter is a convenience class that allows customization to both WebSecurity and HttpSecurity. We can extend WebSecurityConfigurerAdapter multiple times (in distinct objects) to replicate the behavior of having multiple http elements.

What is Spring AuthenticationEntryPoint?

AuthenticationEntryPoint is used in Spring Web Security to configure an application to perform certain actions whenever an unauthenticated client tries to access private resources.

What is @EnableGlobalMethodSecurity in Spring boot?

EnableGlobalMethodSecurity provides AOP security on methods. Some of the annotations that it provides are PreAuthorize , PostAuthorize . It also has support for JSR-250. There are more parameters in the configuration for you.


2 Answers

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.

source

like image 145
Cassian Avatar answered Sep 23 '22 00:09

Cassian


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.

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.:

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.

like image 39
Steve Avatar answered Sep 22 '22 00:09

Steve