> For the complete documentation index, see [llms.txt](https://olavo-moreira.gitbook.io/studies/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://olavo-moreira.gitbook.io/studies/cobertura-de-testes-com-jacoco/fixando-tudo.-test-coverage-+-testes-unitarios-na-camada-service-dscommerce/userservice/melhorias-metodo-autenticated.md).

# Melhorias método autenticated

```java
protected User authenticated() {
	try {
		Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
		Jwt jwtPrincipal = (Jwt) authentication.getPrincipal();
		String username = jwtPrincipal.getClaim("username");
		return repository.findByEmail(username).get();
	}
	catch (Exception e) {
		throw new UsernameNotFoundException("Invalid user");
	}
}
```

Faremos o seguinte: o primeiro bloco do Try, tudo que está dentro dele, iremos alocar para uma nova classe chamada "CustomUserUtil" no pacote Util (essa classe será um @Component).

<figure><img src="https://2863635367-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fu8GTnJcMhmxNO7IoAuV1%2Fuploads%2Fh6t8SxjTyOASPh0j64KD%2Fimage.png?alt=media&amp;token=72ff4e29-e228-4298-9d6a-f266df941398" alt=""><figcaption></figcaption></figure>

### Incluindo no UserService

Importa o component criado

<figure><img src="https://2863635367-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fu8GTnJcMhmxNO7IoAuV1%2Fuploads%2Fz0IQcQW2uFj11qwYHvAb%2Fimage.png?alt=media&amp;token=d520641a-1820-4741-8ca7-1130525cd1d8" alt=""><figcaption></figcaption></figure>

E coloca no bloco do Try

<figure><img src="https://2863635367-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fu8GTnJcMhmxNO7IoAuV1%2Fuploads%2FQ6XirCEqN158KlfyNQJk%2Fimage.png?alt=media&amp;token=84b84aee-1341-4030-a797-54a8f2d49205" alt=""><figcaption></figcaption></figure>
