Skip to content

Instantly share code, notes, and snippets.

@LuisSala
Created April 13, 2012 23:23
Show Gist options
  • Save LuisSala/2380847 to your computer and use it in GitHub Desktop.
Save LuisSala/2380847 to your computer and use it in GitHub Desktop.
Alfresco Login Ticket Generator Example in Java
package org.alfresco.sample;
import org.springframework.context.ApplicationContext;
import org.alfresco.util.ApplicationContextHelper;
import org.alfresco.service.ServiceRegistry;
import org.alfresco.service.cmr.security.AuthenticationService;
import org.alfresco.repo.security.authentication.AuthenticationException;
// ==========================================================================
// TicketGenerator Java Class
// Generates or returns the Alfresco login ticket for a user or guest
// session.
// ==========================================================================
public class TicketGenerator
{
//-------------------------------------------------------------------------
// Method generateGuestTicket
// Generates an Alfresco Authentication Ticket based on the guest login.
//-------------------------------------------------------------------------
public String generateGuestTicket()
{
String generatedTicketId = "";
try {
//-------------------------------------------------------------------
// Get a reference to the Alfresco Application Context and use it to
// get a handle to the Alfresco Service Registry so that we can look
// up the Alfresco Authentication Service:
ApplicationContext appContext = ApplicationContextHelper.getApplicationContext();
ServiceRegistry svcRegistry = (ServiceRegistry) appContext.getBean( ServiceRegistry.SERVICE_REGISTRY );
//-------------------------------------------------------------------
// Now, get a handle to the Alfresco Authentication Service, and ask
// it to authenticate us as a guest. Finally, ask it to generate an
// authentication ticket that we can use to get gues readable content
// contained in the repository on a future call:
AuthenticationService authenticationService = svcRegistry.getAuthenticationService();
authenticationService.authenticateAsGuest();
generatedTicketId = authenticationService.getNewTicket();
} catch ( NoSuchBeanDefinitionException noBeanDef ) {
// thrown if there is no bean definition with the specified name
} catch ( BeansException beanEx ) {
// thrown if bean could not be obtained
} catch ( AuthenticationException authEx ) {
// log/handle error here
} catch ( Exception ex ) {
// handle generic exception / null pointer
}
return( generatedTicketId );
} // generateGuestTicket
//-------------------------------------------------------------------------
// Method generateLoginTicket
// Generates an Alfresco Authentication Ticket based on the user name and
// password passed in.
//-------------------------------------------------------------------------
public String generateLoginTicket( String userName, String userPw )
{
String generatedTicketId = "";
try {
//-------------------------------------------------------------------
// Get a reference to the Alfresco Application Context and use it to
// get a handle to the Alfresco Service Registry so that we can look
// up the Alfresco Authentication Service:
ApplicationContext appContext = ApplicationContextHelper.getApplicationContext();
ServiceRegistry svcRegistry = (ServiceRegistry) appContext.getBean( ServiceRegistry.SERVICE_REGISTRY );
//-------------------------------------------------------------------
// Now, get a handle to the Alfresco Authentication Service, and ask
// it to authenticate us as a guest. Finally, ask it to generate an
// authentication ticket that we can use to get gues readable content
// contained in the repository on a future call:
AuthenticationService authenticationService = svcRegistry.getAuthenticationService();
authenticationService.authenticate( userName, userPw.toCharArray() );
generatedTicketId = authenticationService.getNewTicket();
} catch ( NoSuchBeanDefinitionException noBeanDef ) {
// thrown if there is no bean definition with the specified name
} catch ( BeansException beanEx ) {
// thrown if bean could not be obtained
} catch ( AuthenticationException authEx ) {
// log/handle error here
} catch ( Exception ex ) {
// handle generic exception / null pointer
}
return( generatedTicketId );
}
//-------------------------------------------------------------------------
// Method getCurrentLoginTicket
// Returns the current login ticket for the logged in user session.
//-------------------------------------------------------------------------
public String getCurrentLoginTicket()
{
String generatedTicketId = "";
try {
//-------------------------------------------------------------------
// Get a reference to the Alfresco Application Context and use it to
// get a handle to the Alfresco Service Registry so that we can look
// up the Alfresco Authentication Service:
ApplicationContext appContext = ApplicationContextHelper.getApplicationContext();
ServiceRegistry svcRegistry = (ServiceRegistry) appContext.getBean( ServiceRegistry.SERVICE_REGISTRY );
//-------------------------------------------------------------------
// Now, get a handle to the Alfresco Authentication Service, and ask
// it to authenticate us as a guest. Finally, ask it to generate an
// authentication ticket that we can use to get gues readable content
// contained in the repository on a future call:
AuthenticationService authenticationService = svcRegistry.getAuthenticationService();
generatedTicketId = authenticationService.getCurrentTicket();
} catch ( NoSuchBeanDefinitionException noBeanDef ) {
// thrown if there is no bean definition with the specified name
} catch ( BeansException beanEx ) {
// thrown if bean could not be obtained
} catch ( Exception ex ) {
// handle generic exception / null pointer
}
return( generatedTicketId );
}
} // TicketGenerator
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment