Hi Tijs, I looked at BasicAuthenticationProvider and added the identityService. Anyway, here are the changes I made.
In SecurityConfiguration, I added the below line just before the <code>.httpBasic()</code>
<code>
.addFilterBefore(new CustomHeaderAuthenticationFilter(), BasicAuthenticationFilter.class)
</code>
Here is the CustomHeaderAuthenticationFilter.
<code>
public class CustomHeaderAuthenticationFilter extends
RequestHeaderAuthenticationFilter {
public CustomHeaderAuthenticationFilter() {
super();
setPrincipalRequestHeader("Custom_User");
setExceptionIfHeaderMissing(false);
PreAuthenticatedAuthenticationProvider preAuth = new PreAuthenticatedAuthenticationProvider();
UserDetailsByNameServiceWrapper userDetails = new UserDetailsByNameServiceWrapper();
userDetails.setUserDetailsService(new CustomUserDetailsService());
preAuth.setPreAuthenticatedUserDetailsService(userDetails);
List<AuthenticationProvider> providers = new ArrayList<AuthenticationProvider>(1);
providers.add(preAuth);
setAuthenticationManager(new ProviderManager(providers));
setContinueFilterChainOnUnsuccessfulAuthentication(true);
}
}
</code>
And here is CustomUserDetailsService.
<code>
public class CustomUserDetailsService implements UserDetailsService {
@Autowired
private IdentityService identityService;
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>(1);
authorities.add(new GrantedAuthorityImpl("ROLE_USER"));
UserDetails user = new User(username, "", true, true, true, true, authorities);
identityService.setAuthenticatedUserId(username);
return user;
}
}
</code>
After successful authentication in our application, we set the "Custom_User" header to the user id and then make a call to Activiti REST. I am able to successfully make this call. But as mentioned in my original post, "startUserId" is not set. So I tried to autowire IdentifyService. But I am getting NullPointerException at <code>
identityService.setAuthenticatedUserId(username);</code>
Here is the stacktrace:
<code>
SEVERE: Servlet.service() for servlet [dispatcher] in context with path [/activiti-rest] threw exception
java.lang.NullPointerException
at my.custom.package.CustomUserDetailsService.loadUserByUsername(CustomUserDetailsService.java:25)
at org.springframework.security.core.userdetails.UserDetailsByNameServiceWrapper.loadUserDetails(UserDetailsByNameServiceWrapper.java:51)
at org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationProvider.authenticate(PreAuthenticatedAuthenticationProvider.java:80)
at org.springframework.security.authentication.ProviderManager.authenticate(ProviderManager.java:156)
at org.springframework.security.web.authentication.preauth.AbstractPreAuthenticatedProcessingFilter.doAuthenticate(AbstractPreAuthenticatedProcessingFilter.java:121)
at org.springframework.security.web.authentication.preauth.AbstractPreAuthenticatedProcessingFilter.doFilter(AbstractPreAuthenticatedProcessingFilter.java:91)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:110)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:57)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:50)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192)
at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160)
at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:344)
at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:261)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:314)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
</code>