As per the Spring documantation:
Add this annotation to an
@Configuration
class to have the Spring Security configuration defined in anyWebSecurityConfigurer
or more likely by extending theWebSecurityConfigurerAdapter
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?
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.
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.
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 .
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With