@Override
 @Transactional
 public void remove(AuthenticationHolderEntity a) {
   AuthenticationHolderEntity found = getById(a.getId());
   if (found != null) {
     manager.remove(found);
   } else {
     throw new IllegalArgumentException("AuthenticationHolderEntity not found: " + a);
   }
 }
 @Override
 @Transactional
 public AuthenticationHolderEntity save(AuthenticationHolderEntity a) {
   return JpaUtil.saveOrUpdate(a.getId(), manager, a);
 }
  private OAuth2AccessTokenEntity createAssociatedToken(
      ClientDetailsEntity client, Set<String> scope) {

    // revoke any previous tokens that might exist, just to be sure
    OAuth2AccessTokenEntity oldToken = tokenService.getRegistrationAccessTokenForClient(client);
    if (oldToken != null) {
      tokenService.revokeAccessToken(oldToken);
    }

    // create a new token

    Map<String, String> authorizationParameters = Maps.newHashMap();
    OAuth2Request clientAuth =
        new OAuth2Request(
            authorizationParameters,
            client.getClientId(),
            Sets.newHashSet(new SimpleGrantedAuthority("ROLE_CLIENT")),
            true,
            scope,
            null,
            null,
            null,
            null);
    OAuth2Authentication authentication = new OAuth2Authentication(clientAuth, null);

    OAuth2AccessTokenEntity token = new OAuth2AccessTokenEntity();
    token.setClient(client);
    token.setScope(scope);

    AuthenticationHolderEntity authHolder = new AuthenticationHolderEntity();
    authHolder.setAuthentication(authentication);
    authHolder = authenticationHolderRepository.save(authHolder);
    token.setAuthenticationHolder(authHolder);

    JWTClaimsSet claims =
        new JWTClaimsSet.Builder()
            .audience(Lists.newArrayList(client.getClientId()))
            .issuer(configBean.getIssuer())
            .issueTime(new Date())
            .expirationTime(token.getExpiration())
            .jwtID(UUID.randomUUID().toString()) // set a random NONCE in the middle of it
            .build();

    JWSAlgorithm signingAlg = jwtService.getDefaultSigningAlgorithm();
    JWSHeader header =
        new JWSHeader(
            signingAlg,
            null,
            null,
            null,
            null,
            null,
            null,
            null,
            null,
            null,
            jwtService.getDefaultSignerKeyId(),
            null,
            null);
    SignedJWT signed = new SignedJWT(header, claims);

    jwtService.signJwt(signed);

    token.setJwt(signed);

    return token;
  }