Reputation: 9427
I am trying to set up a simple spring mvc / spring security webapp, but I can't seem to find the way to accomplish this:
How do I go about implementing the last stage?
Upvotes: 0
Views: 936
Reputation: 9427
OK the answer is basically:
SecurityContextHolder.getContext().setAuthentication(...)
However to be able to use it in the scenario I described above where the Spring MVC controller controls the authentication process, a few other things need to be done:
Spring security won't start up without an authentication manager which isn't used in this scenario, so I created a null authentication manager:
@Service("nullAuthenticationProvider")
public class NullAuthenticationProvider implements AuthenticationProvider
{
@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException
{
return authentication;
}
@Override
public boolean supports(Class<?> authentication)
{
return true;
}
}
And finally the spring context.xml:
<security:global-method-security secured-annotations="enabled" />
<security:http disable-url-rewriting="true">
<security:access-denied-handler error-page="/login" />
<security:form-login login-page="/login" />
</security:http>
<security:authentication-manager>
<security:authentication-provider ref='nullAuthenticationProvider'/>
</security:authentication-manager>
Upvotes: 0
Reputation: 3534
I am not sure if I understand your question fully, but if I understand it correctly, you can perhaps extend AbstractPreAuthenticatedProcessingFilter and override getPreAuthenticatedPrincipal and getPreAuthenticatedCredentials with calls to your restful service/controller etc. The override AuthenticationUserDetailsService and probide a simple service, and add that your security context, like this:
<beans:bean id="preauthAuthProvider"
class="org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationProvider">
<beans:property name="preAuthenticatedUserDetailsService">
<beans:bean class="com.YourCompany.YourPreAuthenticatedGrantedAuthoritiesUserDetailsService"></beans:bean>
</beans:property>
<beans:property name="order" value="1"/>
</beans:bean>
<authentication-manager alias="authenticationManager" >
<authentication-provider ref="preauthAuthProvider" ></authentication-provider>
</authentication-manager>
Upvotes: 1