package io.cfp.domain.common;

import io.cfp.entity.Role;
import io.cfp.entity.User;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;

import java.util.Collection;
import java.util.stream.Collectors;

public class UserAuthentication implements Authentication {

    private final User user;
    private final Collection<Role> roles;
    private boolean authenticated = true;

    public UserAuthentication(User user, Collection<Role> roles) {
        this.user = user;
        this.roles = roles;
    }

    @Override
    public String getName() {
        return user.getEmail();
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return roles.stream()
            .map(role -> new SimpleGrantedAuthority(role.getName()))
            .collect(Collectors.toList());
    }

    @Override
    public Object getCredentials() {
        return "****";
    }

    @Override
    public User getDetails() {
        return user;
    }

    @Override
    public Object getPrincipal() {
        return user;
    }

    @Override
    public boolean isAuthenticated() {
        return authenticated;
    }

    @Override
    public void setAuthenticated(boolean authenticated) {
        this.authenticated = authenticated;
    }
}

@@ -295,24 +295,47 @@
 Collection;%0A
+import java.util.List;%0A
 import java.
@@ -358,16 +358,16 @@
 ectors;%0A
-
 %0Apublic 
@@ -467,36 +467,54 @@
 e final 
-Collection%3CRole%3E rol
+List%3CSimpleGrantedAuthority%3E authoriti
 es;%0A    
@@ -657,26 +657,221 @@
     
-this.roles = roles
+authorities = roles.stream()%0A            .map(role -%3E new SimpleGrantedAuthority(role.getName()))%0A            .collect(Collectors.toList());%0A%0A        authorities.add(new SimpleGrantedAuthority(Role.AUTHENTICATED))
 ;%0A  
@@ -1060,133 +1060,19 @@
 urn 
-roles.stream()%0A            .map(role -%3E new SimpleGrantedAuthority(role.getName()))%0A            .collect(Collectors.toList())
+authorities
 ;%0A  

Convert roles to GrantedAuthorities in constructor to ease up things