Example usage for org.springframework.security.core.context SecurityContext getAuthentication

List of usage examples for org.springframework.security.core.context SecurityContext getAuthentication

Introduction

In this page you can find the example usage for org.springframework.security.core.context SecurityContext getAuthentication.

Prototype

Authentication getAuthentication();

Source Link

Document

Obtains the currently authenticated principal, or an authentication request token.

Usage

From source file:org.openinfinity.core.aspect.MultiTenantAspect.java

/**
 * Injection of tenant id will be done based on the <code>org.openinfinity.core.annotation.MultiTenant</code> annotation on method level. After injection of the tenant id 
 * <code>org.openinfinity.core.domain.entity.MultiTenantBaseEntity</code> can be used to retrieve the actual tenant id. <code>org.openinfinity.core.domain.entity.MultiTenantBaseEntity</code> 
 * can be extended by <code>org.openinfinity.core.domain.entity.MultiTenantBaseEntity</code>. 
 * //  ww  w.  j av  a  2  s.  c  o m
 * @param method Represents the method to be executed when exposing <code>org.openinfinity.core.annotation.MultiTenant</code> metadata to it.
 * @param multiTenant Represents the annotation which executed by the aspect.
 * @return Object Represents the original arguments for the method with injected tenant id.
 * @throws Throwable Represents the occurred exception during the execution of the aspect.
 */
@Around("multiTenantMethod() && @annotation(multiTenant)")
public Object populateTenantIdToMultiTenantEntity(ProceedingJoinPoint method, MultiTenant multiTenant)
        throws Throwable {
    if (LOGGER.isDebugEnabled())
        LOGGER.debug("MultiTenantAspect.populateTenantIdToMultiTenantEntity initialized.");
    SecurityContext securityContext = SecurityContextHolder.getContext();
    Authentication authentication = securityContext.getAuthentication();
    if (authentication instanceof Identity) {
        Identity identity = (Identity) authentication;
        Object[] arguments = method.getArgs();
        for (Object object : arguments) {
            if (object instanceof MultiTenantBaseEntity) {
                if (LOGGER.isDebugEnabled())
                    LOGGER.debug(
                            "MultiTenantAspect.populateTenantIdToMultiTenantEntity arguments is istance of MultiTenantBaseEntity.");
                MultiTenantBaseEntity<?, ?, ?> multiTenantBaseEntity = (MultiTenantBaseEntity<?, ?, ?>) object;
                Object tenantId = identity.getTenantPrincipal().getId();
                Field tenantIdField = multiTenantBaseEntity.getClass().getField(TENANT_ID_FIELD);
                if (!tenantIdField.isAccessible()) {
                    tenantIdField.setAccessible(true);
                }
                Object convertedTenantId = typeConverter.convert(tenantId);
                if (tenantIdField.getType().isAssignableFrom(convertedTenantId.getClass())) {
                    if (LOGGER.isDebugEnabled())
                        LOGGER.debug(
                                "MultiTenantAspect.populateTenantIdToMultiTenantEntity tenant id is assignable from ["
                                        + convertedTenantId.getClass().getName() + ".");
                    ReflectionUtils.setField(tenantIdField, multiTenantBaseEntity, convertedTenantId);
                    if (LOGGER.isInfoEnabled())
                        LOGGER.info("MultiTenantAspect.populateTenantIdToMultiTenantEntity injected tenant id ["
                                + convertedTenantId.toString() + "] to the entity.");
                } else {
                    ExceptionUtil.throwSystemException("Field [" + tenantIdField.getType().getName()
                            + "] is not assignable from [" + convertedTenantId.getClass().getName());
                }
            }
        }
    }
    return method.proceed();
}

From source file:org.openinfinity.sso.identityprovisioning.bpmn.ProcessEngineBridgeImpl.java

@Log(level = LogLevel.INFO)
@AuditTrail//from w w w . j av a  2 s  .  c o m
public void claimTask(String taskId) {
    SecurityContext securityContext = SecurityContextHolder.getContext();
    Authentication authentication = securityContext.getAuthentication();
    IdentityService identityService = processEngine.getIdentityService();
    identityService.setAuthenticatedUserId(authentication.getName());
    TaskService taskService = processEngine.getTaskService();
    taskService.claim(taskId, authentication.getName());
}

From source file:org.openinfinity.sso.identityprovisioning.bpmn.ProcessEngineBridgeImpl.java

@Log(level = LogLevel.INFO)
@AuditTrail/* ww  w  .  j  a v  a  2 s .c  o m*/
public void completeTask(String taskId, Map<String, Object> variables) {
    SecurityContext securityContext = SecurityContextHolder.getContext();
    Authentication authentication = securityContext.getAuthentication();
    IdentityService identityService = processEngine.getIdentityService();
    identityService.setAuthenticatedUserId(authentication.getName());
    TaskService taskService = processEngine.getTaskService();
    taskService.complete(taskId, variables);
}

From source file:org.openinfinity.sso.identityprovisioning.bpmn.ProcessEngineBridgeImpl.java

public Collection<org.openinfinity.sso.identityprovisioning.bpmn.Task> queryForTasksByRole(String role) {
    SecurityContext securityContext = SecurityContextHolder.getContext();
    Authentication authentication = securityContext.getAuthentication();
    IdentityService identityService = processEngine.getIdentityService();
    identityService.setAuthenticatedUserId(authentication.getName());
    TaskService taskService = processEngine.getTaskService();
    List<Task> tasks = taskService.createTaskQuery().taskCandidateGroup(role).list();
    Collection<org.openinfinity.sso.identityprovisioning.bpmn.Task> simpleTasks = mapTasks(tasks);
    return Collections.unmodifiableCollection(simpleTasks);
}

From source file:org.openinfinity.sso.identityprovisioning.bpmn.ProcessEngineBridgeImpl.java

@Log(level = LogLevel.INFO)
@AuditTrail/*from  w  ww.j  a  v a2s.c o  m*/
public String startProcess(String processDefinitionKey, Map<String, Object> variables) {
    SecurityContext securityContext = SecurityContextHolder.getContext();
    Authentication authentication = securityContext.getAuthentication();
    IdentityService identityService = processEngine.getIdentityService();
    identityService.setAuthenticatedUserId(authentication.getName());
    RuntimeService runtimeService = processEngine.getRuntimeService();
    ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processDefinitionKey, variables);
    return processInstance.getId();
}

From source file:org.openinfinity.sso.identityprovisioning.bpmn.ProcessEngineBridgeImpl.java

@Log(level = LogLevel.INFO)
@AuditTrail/*from  www  .java2 s .  co m*/
public Collection<org.openinfinity.sso.identityprovisioning.bpmn.Task> queryForAllUserTasks() {
    SecurityContext securityContext = SecurityContextHolder.getContext();
    Authentication authentication = securityContext.getAuthentication();
    IdentityService identityService = processEngine.getIdentityService();
    identityService.setAuthenticatedUserId(authentication.getName());
    TaskService taskService = processEngine.getTaskService();
    List<Task> tasks = taskService.createTaskQuery().list();
    Collection<org.openinfinity.sso.identityprovisioning.bpmn.Task> simpleTasks = mapTasks(tasks);
    return Collections.unmodifiableCollection(simpleTasks);
}

From source file:org.openinfinity.sso.identityprovisioning.bpmn.ProcessEngineBridgeImpl.java

@Log(level = LogLevel.INFO)
@AuditTrail//  w w w. j av  a  2  s. com
public Collection<org.openinfinity.sso.identityprovisioning.bpmn.Task> queryForUserTasks() {
    SecurityContext securityContext = SecurityContextHolder.getContext();
    Authentication authentication = securityContext.getAuthentication();
    IdentityService identityService = processEngine.getIdentityService();
    identityService.setAuthenticatedUserId(authentication.getName());
    TaskService taskService = processEngine.getTaskService();
    List<Task> tasks = taskService.createTaskQuery().taskCandidateUser(authentication.getName()).list();
    Collection<org.openinfinity.sso.identityprovisioning.bpmn.Task> simpleTasks = mapTasks(tasks);
    return Collections.unmodifiableCollection(simpleTasks);
}

From source file:org.openinfinity.sso.identityprovisioning.bpmn.ProcessEngineBridgeImpl.java

public Collection<org.openinfinity.sso.identityprovisioning.bpmn.Task> queryForTasksByUserRoles() {
    SecurityContext securityContext = SecurityContextHolder.getContext();
    Authentication authentication = securityContext.getAuthentication();
    IdentityService identityService = processEngine.getIdentityService();
    identityService.setAuthenticatedUserId(authentication.getName());
    TaskService taskService = processEngine.getTaskService();
    Collection<? extends GrantedAuthority> grantedAuthorities = authentication.getAuthorities();
    List<String> candidateGroups = new ArrayList<String>();
    for (GrantedAuthority grantedAuthority : grantedAuthorities) {
        if (grantedAuthority.getAuthority() != null)
            candidateGroups.add(grantedAuthority.getAuthority());
    }/*from  w w  w.j  a v a2 s  .  co  m*/
    List<Task> tasks = taskService.createTaskQuery().taskCandidateGroupIn(candidateGroups).list();
    Collection<org.openinfinity.sso.identityprovisioning.bpmn.Task> simpleTasks = mapTasks(tasks);
    return Collections.unmodifiableCollection(simpleTasks);
}

From source file:org.musicrecital.webapp.listener.UserCounterListener.java

/**
 * When user's logout, remove their name from the hashMap
 *
 * @param event the session binding event
 * @see javax.servlet.http.HttpSessionAttributeListener#attributeRemoved(javax.servlet.http.HttpSessionBindingEvent)
 *///from  ww  w .j a v a  2s. co m
public void attributeRemoved(HttpSessionBindingEvent event) {
    if (event.getName().equals(EVENT_KEY) && !isAnonymous()) {
        SecurityContext securityContext = (SecurityContext) event.getValue();
        Authentication auth = securityContext.getAuthentication();
        if (auth != null && (auth.getPrincipal() instanceof User)) {
            User user = (User) auth.getPrincipal();
            removeUsername(user);
        }
    }
}

From source file:org.trustedanalytics.user.manageusers.UsersConfig.java

private OAuth2Authentication getAuthentication() {
    SecurityContext context = SecurityContextHolder.getContext();
    if (context == null) {
        return null;
    }/* ww  w.  j a  v  a2 s.  co m*/

    return (OAuth2Authentication) context.getAuthentication();
}