-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Problem in maven
- Loading branch information
Showing
9 changed files
with
191 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
Back/Spring/backend/src/main/java/pl/zeto/backend/VMC/config/JwtAuthFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package pl.zeto.backend.VMC.config; | ||
|
||
import jakarta.servlet.FilterChain; | ||
import jakarta.servlet.ServletException; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.web.filter.OncePerRequestFilter; | ||
|
||
import java.io.IOException; | ||
|
||
@RequiredArgsConstructor | ||
public class JwtAuthFilter extends OncePerRequestFilter { | ||
|
||
private final UserAuthProvider userAuthProvider; | ||
|
||
|
||
@Override | ||
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { | ||
String header = request.getHeader(HttpHeaders.AUTHORIZATION); | ||
if (header != null) { | ||
String[] authElements = header.split(" "); | ||
if (authElements.length == 2 && "Bearer".equals(authElements[0])) { | ||
try{ | ||
SecurityContextHolder.getContext().setAuthentication(userAuthProvider.validateToken(authElements[1])); | ||
} catch (RuntimeException e) { | ||
SecurityContextHolder.clearContext(); | ||
throw e; | ||
} | ||
} | ||
} | ||
filterChain.doFilter(request, response); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
Back/Spring/backend/src/main/java/pl/zeto/backend/VMC/config/UserAuthProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package pl.zeto.backend.VMC.config; | ||
|
||
import com.auth0.jwt.JWT; | ||
import com.auth0.jwt.JWTVerifier; | ||
import com.auth0.jwt.algorithms.Algorithm; | ||
import com.auth0.jwt.interfaces.DecodedJWT; | ||
import jakarta.annotation.PostConstruct; | ||
import lombok.Data; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.stereotype.Component; | ||
import pl.zeto.backend.VMC.dto.UserDto; | ||
|
||
import java.util.Base64; | ||
import java.util.Collections; | ||
import java.util.Date; | ||
|
||
@RequiredArgsConstructor | ||
@Component | ||
public class UserAuthProvider { | ||
@Value("${security.jwt.token.secret-key:secret-key}") | ||
private String secretKey; | ||
|
||
@PostConstruct | ||
public void init() { | ||
secretKey = Base64.getEncoder().encodeToString(secretKey.getBytes()); | ||
|
||
} | ||
|
||
public String createToken(UserDto dto) { | ||
Date now = new Date(); | ||
Date validity = new Date(now.getTime() + 3600000); | ||
return JWT.create() | ||
.withIssuer(dto.getUsername()) | ||
.withIssuedAt(now) | ||
.withExpiresAt(validity) | ||
.withClaim("firstName", dto.getFirstName()) | ||
.withClaim("lastName", dto.getLastName()) | ||
.sign(Algorithm.HMAC256(secretKey)); | ||
|
||
} | ||
|
||
public Authentication validateToken(String token) { | ||
Algorithm algorithm = Algorithm.HMAC256(secretKey); | ||
|
||
JWTVerifier verifier = JWT.require(algorithm).build(); | ||
|
||
DecodedJWT decodedJWT = verifier.verify(token); | ||
|
||
UserDto user = UserDto.builder() | ||
.username(decodedJWT.getIssuer()) | ||
.firstName(decodedJWT.getClaim("firstName").asString()) | ||
.lastName(decodedJWT.getClaim("lastName").asString()) | ||
.build(); | ||
return new UsernamePasswordAuthenticationToken(user, null, Collections.emptyList()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
Back/Spring/backend/src/main/java/pl/zeto/backend/VMC/dto/SignUpDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
package pl.zeto.backend.VMC.dto; | ||
|
||
public record SignUpDto (String firstName, String lastName,String email,String username, char[] password){ // Niemutowalna klasa przechowująca dane rejestracji | ||
public record SignUpDto (String firstName, String lastName,String email,String username, char[] password){// Niemutowalna klasa przechowująca dane rejestracji | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters