All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.activiti.rest.security.BasicAuthenticationProvider Maven / Gradle / Ivy

There is a newer version: 6.0.0
Show newest version
package org.activiti.rest.security;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.activiti.engine.IdentityService;
import org.activiti.engine.identity.Group;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;

public class BasicAuthenticationProvider implements AuthenticationProvider {
  
  @Autowired
  private IdentityService identityService;
  
  @Override
  public Authentication authenticate(Authentication authentication) 
    throws AuthenticationException {
      String name = authentication.getName();
      String password = authentication.getCredentials().toString();

      boolean authenticated = identityService.checkPassword(name, password);
      if (authenticated) {
        List groups = identityService.createGroupQuery().groupMember(name).list();
        Collection grantedAuthorities = new ArrayList();
        for (Group group : groups) {
            grantedAuthorities.add(new SimpleGrantedAuthority(group.getId()));
        }
        identityService.setAuthenticatedUserId(name);
        return new UsernamePasswordAuthenticationToken(name, password, grantedAuthorities);
      } else {
        throw new BadCredentialsException("Authentication failed for this username and password");
      }
  }

  @Override
  public boolean supports(Class authentication) {
      return authentication.equals(UsernamePasswordAuthenticationToken.class);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy