8
I am with the following web application created initially for and web using Spring Boot, Spring Security and Thymeleaf (authentication and authorization).
I did a REST job and separated the front-end in another application that consumes services through RestTemplate
. Every application is using JWT and Spring Security for authentication and authorization. I did several tests with Postman and everything is ok.
The problem is I can’t ride the login in the client application for not knowing how to create Main, because it is in the same application and not in a remote server.
How to create my controller for login specifically?
Before I could use the Userdetail and the Userdetailservice and it was very simple
But now I’m gonna need credentials on all the requisitions
How can I capture the logged-in user in the client application to handle their permissions to access the pages and their contents that are already configured with Thymeleleaf and Spring Security?
Where to find an example of a Spring Boot application consuming a REST API by logging in to access other features?
In the client project I am doing so to get the token:
@SpringBootApplication
public class DemoApplication implements CommandLineRunner {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
String token = fazerLogin();
RestTemplate restTemplate = new RestTemplate();
String url = "http://localhost:8080/usuario/perfil/155";
Map<String, String> param2 = new HashMap();
param2.put("Authorization", "Bearer " + token);
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
headers.set("Authorization", "Bearer " + token);
HttpEntity<String> entity = new HttpEntity<>(null, headers);
ResponseEntity<String> usuarioLogado;
usuarioLogado = restTemplate.exchange(url, HttpMethod.GET, entity, String.class);
System.out.println("Usuario Logado =>" + usuarioLogado);
}
private String fazerLogin() throws RestClientException {
Map<String, String> param = new HashMap();
param.put("email", "[email protected]");
param.put("senha", "123");
RestTemplate restTemplate = new RestTemplate();
String url = "http://localhost:8080/login";
ResponseEntity<String> response;
response = restTemplate.postForEntity(url, param, String.class);
String leitura = response.toString().substring(21, 195);
System.out.println("Leitura =>" + leitura);
String token = leitura.substring(7, 174);
System.out.println("Token =>" + token);
return token;
}
}
I read that one article interesting, but does not have the whole application, only small snippets.