Já resolvi.
Criei uma classe implementando AuthenticationProvider, efetuando dentro do method authenticate a autenticação na apiServer.
E também criei outra Class implementando Principal para guardar o token do usuario.
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
@Component
public class AuthenticationProviderService implements AuthenticationProvider {
@Value("${climatempo.url.LoginAuth}") // buscando url de auteticação na apiServer salva no application.properties
private String urlLogin;
private UserInfo login;
@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
String name = authentication.getName();
String password = authentication.getCredentials().toString();
this.login = new UserInfo(name, password);
System.out.println(this.login.toString());
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<UserInfo> response = restTemplate.postForEntity(urlLogin, this.login, UserInfo.class);
if (response.getStatusCode().equals(HttpStatus.OK)) {
this.login.setToken(response.getBody().getToken());
this.login.setTipo(response.getBody().getTipo());
System.out.println(this.login.toString());
// use the credentials
// and authenticate against the third-party system
return new UsernamePasswordAuthenticationToken(this.login.getName(), authentication.getCredentials(), getAuthority());
} else {
System.err.println("Login e/ou Senha inválidos.");
throw new UsernameNotFoundException("Login e/ou Senha inválidos.");
}
}
@Override
public boolean supports(Class<?> authentication) {
return authentication.equals(UsernamePasswordAuthenticationToken.class);
}
private List<SimpleGrantedAuthority> getAuthority() {
SimpleGrantedAuthority authority = new SimpleGrantedAuthority("ROLE_USER");
List<SimpleGrantedAuthority> authorities = new ArrayList<SimpleGrantedAuthority>();
authorities.add(authority);
return authorities;
}
}