We are using Spring Security version 4. By default the anonymous user has the ROLE_ANONYMOUS assigned.
We want to add more roles for the anonymous user.
I tried to extend the AnonymousAuthenticationFilter and add it to Spring Security context as below:
<http entry-point-ref="authenticationEntryPoint">
<custom-filter ref="sabaAnonymousAuthenticationFilter" position="ANONYMOUS_FILTER"/>
<anonymous enabled="false"/>
.....
<beans:bean id="sabaAnonymousAuthenticationFilter"
class="foo.bar.CustomAnonymousAuthenticationFilter">
<beans:constructor-arg index="0" value="SomeUniqueKeyForThisApplication"/>
</beans:bean>
And the class:
public class CustomAnonymousAuthenticationFilter extends AnonymousAuthenticationFilter {
@Inject
HelperClass aHelperClass;
public CustomAnonymousAuthenticationFilter(String key) {
super(key);
getAuthorities().add(new SimpleGrantedAuthority("ROLE_FOO_BAR"));
......
}
}
The above code change anonymous roles and add ROLE_FOO_BAR, but I can not @Inject or @Autowire other Spring beans in this filter.
Please let me know:
- Is this a correct way of defining a custom anonymous filter?
- How can I
authowireother beans here?
I used same approach for defining a custom UserDetailsService and the autowire works there.