I am using Spring Security in my application and I have a REST API exposed. The API authenticates the user who is trying to access the API and also I am using Spring's ACL implementation(placed as annotations PreAuthorize, PostAuthorize etc).
The problem is that there are some resources that can be accessed by non-logged in users, and some of that resource's operations can be used by logged in users only. To provide access to non-logged-in users, I have added those urls to the SecurityConfiguration class:
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
@ImportResource("classpath:acl-config.xml")
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring()
.antMatchers("/api/v1/resource1")
.antMatchers("/api/v1/resource2")
....
Now the problem is that whenever anyone accesses resource1 or resource2, the SecurityContextHolder.getContext().getAuthentication() returns null. This is causing problems as I am using Spring's ACL implementation and the implementation's source files use the SecurityContextHolder.getContext().getAuthentication() method and gets null in return. So ACL has stopped working. In my own code, I can get the authentication instance as mentioned here but not in ACL's source files.
I would like to do something in the configuration as a one time thing, so that even if I provide urls in SecurityConfiguration, I can still get SecurityContextHolder.getContext().getAuthentication() instance.