diff --git a/pom.xml b/pom.xml index 428b562..39cc9bb 100644 --- a/pom.xml +++ b/pom.xml @@ -50,10 +50,25 @@ org.springframework.boot spring-boot-starter-data-jpa - - - - + + org.springframework.boot + spring-boot-starter-security + + + io.jsonwebtoken + jjwt-api + 0.11.5 + + + io.jsonwebtoken + jjwt-impl + 0.11.5 + + + io.jsonwebtoken + jjwt-jackson + 0.11.5 + org.springframework.boot spring-boot-starter-web diff --git a/src/main/java/org/cmh/backend/Config/CorsConfig.java b/src/main/java/org/cmh/backend/Config/CorsConfig.java deleted file mode 100644 index 7852636..0000000 --- a/src/main/java/org/cmh/backend/Config/CorsConfig.java +++ /dev/null @@ -1,26 +0,0 @@ -package org.cmh.backend.Config; -// CorsConfig.java - -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.web.servlet.config.annotation.CorsRegistry; -import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; - -@Configuration -public class CorsConfig { - - @Bean - public WebMvcConfigurer corsConfigurer() { - return new WebMvcConfigurer() { - @Override - public void addCorsMappings(CorsRegistry registry) { - registry.addMapping("/**") - .allowedOrigins("http://localhost:8080") - .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS") - .allowedHeaders("*") - .allowCredentials(true); - } - }; - } -} - diff --git a/src/main/java/org/cmh/backend/Config/SecurityConfig.java b/src/main/java/org/cmh/backend/Config/SecurityConfig.java new file mode 100644 index 0000000..c917d0a --- /dev/null +++ b/src/main/java/org/cmh/backend/Config/SecurityConfig.java @@ -0,0 +1,23 @@ +package org.cmh.backend.Config; + +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.configurers.AbstractHttpConfigurer; +import org.springframework.security.web.SecurityFilterChain; + +@Configuration +public class SecurityConfig { + + @Bean + public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { + // Use the new API to disable CSRF + http.csrf(AbstractHttpConfigurer::disable) + // Permit all requests + .authorizeHttpRequests(authorize -> authorize + .anyRequest().permitAll() + ); + + return http.build(); + } +} diff --git a/src/main/java/org/cmh/backend/Utils/JwtUtil.java b/src/main/java/org/cmh/backend/Utils/JwtUtil.java new file mode 100644 index 0000000..97b1c69 --- /dev/null +++ b/src/main/java/org/cmh/backend/Utils/JwtUtil.java @@ -0,0 +1,39 @@ +package org.cmh.backend.Utils; + +import io.jsonwebtoken.Claims; +import io.jsonwebtoken.Jwts; +import io.jsonwebtoken.SignatureAlgorithm; +import io.jsonwebtoken.security.Keys; + +import javax.crypto.SecretKey; +import java.util.Date; + +public class JwtUtil { + + private static final SecretKey SECRET_KEY = Keys.hmacShaKeyFor("9cbf491e853995ab73a2a3dcd7206549".getBytes()); + + public static String generateToken(String username) { + return Jwts.builder() + .setSubject(username) + .setIssuedAt(new Date()) + .setExpiration(new Date(System.currentTimeMillis() + 1000 * 60 * 60 * 10)) // 10 hours + .signWith(SECRET_KEY, SignatureAlgorithm.HS256) + .compact(); + } + + public static Claims extractClaims(String token) { + return Jwts.parserBuilder() + .setSigningKey(SECRET_KEY) + .build() + .parseClaimsJws(token) + .getBody(); + } + + public static boolean isTokenValid(String token, String username) { + return username.equals(extractClaims(token).getSubject()) && !isTokenExpired(token); + } + + private static boolean isTokenExpired(String token) { + return extractClaims(token).getExpiration().before(new Date()); + } +}