-
Notifications
You must be signed in to change notification settings - Fork 16
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
3855241
commit 7896e6b
Showing
7 changed files
with
121 additions
and
1 deletion.
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
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
Empty file.
49 changes: 49 additions & 0 deletions
49
kbazaar/src/main/java/com/kampus/kbazaar/security/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,49 @@ | ||
package com.kampus.kbazaar.security; | ||
|
||
import jakarta.servlet.FilterChain; | ||
import jakarta.servlet.ServletException; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; | ||
import org.springframework.security.core.GrantedAuthority; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.filter.OncePerRequestFilter; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import static org.springframework.http.HttpHeaders.AUTHORIZATION; | ||
|
||
@Component | ||
public class JwtAuthFilter extends OncePerRequestFilter { | ||
|
||
private final JwtService jwtService; | ||
|
||
public JwtAuthFilter(JwtService jwtService) { | ||
this.jwtService = jwtService; | ||
} | ||
|
||
@Override | ||
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { | ||
final String authHeader = request.getHeader(AUTHORIZATION); | ||
final String jwtToken; | ||
|
||
if (authHeader == null || !authHeader.startsWith("Bearer")) { | ||
filterChain.doFilter(request,response); | ||
return; | ||
} | ||
jwtToken = authHeader.substring(7); | ||
|
||
if (SecurityContextHolder.getContext().getAuthentication() == null) { | ||
if (!jwtService.isTokenExpired(jwtToken)) { | ||
UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken("mockUser", null, new ArrayList<>()); | ||
SecurityContextHolder.getContext().setAuthentication(authenticationToken); | ||
} | ||
} | ||
|
||
filterChain.doFilter(request,response); | ||
} | ||
|
||
} |
32 changes: 32 additions & 0 deletions
32
kbazaar/src/main/java/com/kampus/kbazaar/security/JwtService.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,32 @@ | ||
package com.kampus.kbazaar.security; | ||
|
||
import io.jsonwebtoken.Claims; | ||
import io.jsonwebtoken.Jwts; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.Date; | ||
import java.util.function.Function; | ||
|
||
@Component | ||
public class JwtService { | ||
|
||
@Value("${security.jwt.secret}") | ||
private String SECRET_KEY = ""; | ||
|
||
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 (Claims) Jwts.parserBuilder().setSigningKey(SECRET_KEY.getBytes()).build().parse(token).getBody(); | ||
} | ||
|
||
public Boolean isTokenExpired(String token) { | ||
return extractExpiration(token).before(new Date()); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
kbazaar/src/main/java/com/kampus/kbazaar/security/SecurityConfig.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,33 @@ | ||
package com.kampus.kbazaar.security; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.security.config.annotation.web.builders.HttpSecurity; | ||
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; | ||
import org.springframework.security.config.http.SessionCreationPolicy; | ||
import org.springframework.security.web.SecurityFilterChain; | ||
import org.springframework.security.web.authentication.www.BasicAuthenticationFilter; | ||
|
||
@EnableWebSecurity | ||
@Configuration | ||
public class SecurityConfig { | ||
|
||
private final JwtAuthFilter jwtAuthFilter; | ||
public SecurityConfig(JwtAuthFilter jwtAuthFilter) { | ||
this.jwtAuthFilter = jwtAuthFilter; | ||
} | ||
|
||
@Bean | ||
SecurityFilterChain defaultSecurityFilterChain(HttpSecurity http) throws Exception { | ||
return http | ||
.csrf(httpSecurityCsrfConfigurer -> httpSecurityCsrfConfigurer.disable()) | ||
.authorizeHttpRequests((requests) -> | ||
requests | ||
.requestMatchers("/swagger-ui/**","/v3/api-docs/**").permitAll() | ||
.anyRequest() | ||
.authenticated()) | ||
.sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS)) | ||
.addFilterBefore(jwtAuthFilter, BasicAuthenticationFilter.class) | ||
.build(); | ||
} | ||
} |
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