How to enable the use-expressions
option of the Spring Security configuration in a code based configuration? This option is needed to enable Expression-Based Access Control usage for Thymeleaf.
In the XML configuration it looks like this:
...
<http auto-config="true" use-expressions="true" >
...
</http>
...
Method-level security is implemented by placing the @PreAuthorize annotation on controller methods (actually one of a set of annotations available, but the most commonly used). This annotation contains a Spring Expression Language (SpEL) snippet that is assessed to determine if the request should be authenticated.
hasRole, hasAnyRole. These expressions are responsible for defining the access control or authorization to specific URLs and methods in our application: @Override protected void configure(final HttpSecurity http) throws Exception { ... . antMatchers("/auth/admin/*").
@PostAuthorize: The @PostAuthorize as name suggest checks for authorization after method execution. The @PostAuthorize authorizes on the basis of logged in roles, return object by method and passed argument to the method. For the returned object spring security provides built-in keyword i.e. returnObject.
In Spring Boot 2, if we want our own security configuration, we can simply add a custom WebSecurityConfigurerAdapter. This will disable the default auto-configuration and enable our custom security configuration. Spring Boot 2 also uses most of Spring Security's defaults.
According to this blog post, use-expressions
is set to true when using the Java Configuration.
Comparison to XML Namespace
...
One difference is that Java Configuration uses authorizeUrls
to specify use-expressions="true"
Hope this helps.
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