Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the use of @EnableWebSecurity in Spring?

As per the Spring documantation:

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:

Or As this @EnableWebSecurity depicts, is used to enable SpringSecurity in our project.

But my question is that even if I don't annotate any of my class with @EnableWebSecurity still the application prompting for username and password.(default behaviour)

So I am receiving the same behaviour with @EnableWebSecurity and without @EnableWebSecurity.

Can someone please explain what exactly is this annotation for?

like image 507
Mehraj Malik Avatar asked Jun 21 '17 09:06

Mehraj Malik


People also ask

What is the use of @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 the use of AbstractSecurityWebApplicationInitializer?

AbstractSecurityWebApplicationInitializer. Creates a new instance that assumes the Spring Security configuration is loaded by some other means than this class. For example, a user might create a ContextLoaderListener using a subclass of AbstractContextLoaderInitializer .

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.


1 Answers

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.

Yes, it is the default behavior. If you looked at your classpath, you could find other classes marked with that annotation (depends on your dependencies):

  • SpringBootWebSecurityConfiguration;
  • FallbackWebSecurityAutoConfiguration;
  • WebMvcSecurityConfiguration.

Consider them carefully, turn the needed configuration off, or override its behavior.

like image 55
Andrew Tobilko Avatar answered Sep 28 '22 06:09

Andrew Tobilko