-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c97cae2
commit 337c06e
Showing
6 changed files
with
230 additions
and
22 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
57 changes: 57 additions & 0 deletions
57
src/main/java/com/programming/userService/config/JwtRequestFilter.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,57 @@ | ||
package com.programming.userService.config; | ||
|
||
import com.programming.userService.util.JwtUtil; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.security.core.userdetails.UserDetails; | ||
import org.springframework.security.core.userdetails.UserDetailsService; | ||
import org.springframework.security.web.authentication.WebAuthenticationDetailsSource; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.filter.OncePerRequestFilter; | ||
import jakarta.servlet.FilterChain; | ||
import jakarta.servlet.ServletException; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
|
||
import java.io.IOException; | ||
|
||
@Component | ||
public class JwtRequestFilter extends OncePerRequestFilter { | ||
|
||
@Autowired | ||
private UserDetailsService userDetailsService; | ||
|
||
@Autowired | ||
private JwtUtil jwtUtil; | ||
|
||
@Override | ||
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) | ||
throws ServletException, IOException { | ||
|
||
final String authorizationHeader = request.getHeader("Authorization"); | ||
|
||
String username = null; | ||
String jwt = null; | ||
|
||
if (authorizationHeader != null && authorizationHeader.startsWith("Bearer ")) { | ||
jwt = authorizationHeader.substring(7); | ||
username = jwtUtil.extractUsername(jwt); | ||
} | ||
|
||
if (username != null && SecurityContextHolder.getContext().getAuthentication() == null) { | ||
|
||
UserDetails userDetails = this.userDetailsService.loadUserByUsername(username); | ||
|
||
if (jwtUtil.validateToken(jwt, userDetails)) { | ||
|
||
UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken = new UsernamePasswordAuthenticationToken( | ||
userDetails, null, userDetails.getAuthorities()); | ||
usernamePasswordAuthenticationToken | ||
.setDetails(new WebAuthenticationDetailsSource().buildDetails(request)); | ||
SecurityContextHolder.getContext().setAuthentication(usernamePasswordAuthenticationToken); | ||
} | ||
} | ||
chain.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
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
50 changes: 50 additions & 0 deletions
50
src/main/java/com/programming/userService/entity/CustomUserDetails.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,50 @@ | ||
package com.programming.userService.entity; | ||
|
||
import org.springframework.security.core.GrantedAuthority; | ||
import org.springframework.security.core.userdetails.UserDetails; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
|
||
public class CustomUserDetails implements UserDetails { | ||
|
||
private final AuthUser authUser; | ||
|
||
public CustomUserDetails(AuthUser authUser) { | ||
this.authUser = authUser; | ||
} | ||
|
||
@Override | ||
public Collection<? extends GrantedAuthority> getAuthorities() { | ||
return Collections.emptyList(); // Thêm danh sách quyền nếu cần thiết | ||
} | ||
|
||
@Override | ||
public String getPassword() { | ||
return authUser.getPassword(); | ||
} | ||
|
||
@Override | ||
public String getUsername() { | ||
return authUser.getUsername(); | ||
} | ||
|
||
@Override | ||
public boolean isAccountNonExpired() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public boolean isAccountNonLocked() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public boolean isCredentialsNonExpired() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public boolean isEnabled() { | ||
return true; | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
src/main/java/com/programming/userService/util/JwtUtil.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,56 @@ | ||
package com.programming.userService.util; | ||
|
||
import io.jsonwebtoken.Claims; | ||
import io.jsonwebtoken.Jwts; | ||
import io.jsonwebtoken.SignatureAlgorithm; | ||
import org.springframework.security.core.userdetails.UserDetails; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.Date; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.function.Function; | ||
|
||
|
||
@Service | ||
public class JwtUtil { | ||
private String SECRET_KEY = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"; // Thay đổi SECRET_KEY với giá trị bảo mật của bạn | ||
|
||
public String extractUsername(String token) { | ||
return extractClaim(token, Claims::getSubject); | ||
} | ||
|
||
public Date extractExpiration(String token) { | ||
return extractClaim(token, Claims::getExpiration); | ||
} | ||
|
||
public <T> T extractClaim(String token, Function<Claims, T> claimsResolver) { | ||
final Claims claims = extractAllClaims(token); | ||
return claimsResolver.apply(claims); | ||
} | ||
|
||
private Claims extractAllClaims(String token) { | ||
return Jwts.parser().setSigningKey(SECRET_KEY).parseClaimsJws(token).getBody(); | ||
} | ||
|
||
private Boolean isTokenExpired(String token) { | ||
return extractExpiration(token).before(new Date()); | ||
} | ||
|
||
public String generateToken(UserDetails userDetails) { | ||
Map<String, Object> claims = new HashMap<>(); | ||
return createToken(claims, userDetails.getUsername()); | ||
} | ||
|
||
private String createToken(Map<String, Object> claims, String subject) { | ||
return Jwts.builder().setClaims(claims).setSubject(subject).setIssuedAt(new Date(System.currentTimeMillis())) | ||
.setExpiration(new Date(System.currentTimeMillis() + 1000 * 60 * 60 * 10)) // Token có hiệu lực trong 10 | ||
// giờ | ||
.signWith(SignatureAlgorithm.HS256, SECRET_KEY).compact(); | ||
} | ||
|
||
public Boolean validateToken(String token, UserDetails userDetails) { | ||
final String username = extractUsername(token); | ||
return (username.equals(userDetails.getUsername()) && !isTokenExpired(token)); | ||
} | ||
} |