JWT + Spring Security [Spring Boot 3]
Автор: LaMalditaProgramadora
Загружено: 2023-07-28
Просмотров: 2023
Link al repositorio: https://github.com/LaMalditaProgramad...
JSON Web Token (JWT) + Spring Security [Spring Boot 3]
--------------------------------------------------------------------------------------------
0. Agregamos las dependencias
jjwt-api: https://mvnrepository.com/artifact/io...
jjwt-impl: https://mvnrepository.com/artifact/io...
jjwt-jackson: https://mvnrepository.com/artifact/io...
spring-boot-starter-security (no es necesario poner la versión, se toma del spring-boot-starter-parent): https://mvnrepository.com/artifact/or...
1. En nuestro package model, creamos:
Users.java: https://github.com/LaMalditaProgramad...
Role.java: https://github.com/LaMalditaProgramad...
2. En nuestro package repository, creamos:
IUserRepository.java: https://github.com/LaMalditaProgramad...
3. En nuestro package service, creamos:
IUserService.java: https://github.com/LaMalditaProgramad...
4. En nuestro package service.impl, creamos:
UserServiceImpl.java: https://github.com/LaMalditaProgramad...
JwtUserDetailsService.java: https://github.com/LaMalditaProgramad...
5. En nuestro package util, creamos:
CORS.java: https://github.com/LaMalditaProgramad...
6. En nuestro package security, creamos:
https://github.com/LaMalditaProgramad...
JwtAuthenticationEntryPoint.java
JwtRequest.java
JwtRequestFilter.java
JwtResponse.java
JwtTokenUtil.java
WebSecurityConfig.java
7. En application.properties, agregar:
spring.mvc.pathmatch.matching-strategy=ant-path-matcher
jwt.secret=LaMalditaProgramadoraLaMalditaProgramadoraLaMalditaProgramadora
Nota: jwt.secret puede variar
8. En nuestro package controller, creamos:
UserController.java: https://github.com/LaMalditaProgramad...
JwtAuthenticationController.java: https://github.com/LaMalditaProgramad...
9. En HeroController.java, definir los permisos de los métodos con:
@PreAuthorize("hasAuthority('ROLE_USER') or hasAuthority('ROLE_ADMIN')")
@PreAuthorize("hasAuthority('ROLE_ADMIN')")
@PreAuthorize("hasAuthority('ROLE_USER')")
10. Importamos hero-api.postman_collection.json en Postman y probamos.
https://github.com/LaMalditaProgramad...
11. Suscríbete :D
#springboot #jwt #springsecurity #java
Доступные форматы для скачивания:
Скачать видео mp4
-
Информация по загрузке: