Removed obsolete files. Refactored certain files to use newer ones.
This commit is contained in:
@@ -1,105 +0,0 @@
|
||||
package com.skycrate.backend.skycrateBackend.config;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.security.authentication.AuthenticationProvider;
|
||||
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.UsernamePasswordAuthenticationFilter;
|
||||
import org.springframework.web.cors.CorsConfiguration;
|
||||
import org.springframework.web.cors.CorsConfigurationSource;
|
||||
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Configuration
|
||||
@EnableWebSecurity
|
||||
public class SecurityConfiguration {
|
||||
private final AuthenticationProvider authenticationProvider;
|
||||
private final JwtAuthenticationFilter jwtAuthenticationFilter;
|
||||
|
||||
public SecurityConfiguration(
|
||||
JwtAuthenticationFilter jwtAuthenticationFilter,
|
||||
AuthenticationProvider authenticationProvider
|
||||
) {
|
||||
this.authenticationProvider = authenticationProvider;
|
||||
this.jwtAuthenticationFilter = jwtAuthenticationFilter;
|
||||
}
|
||||
|
||||
// @Bean
|
||||
// public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
|
||||
// http.csrf()
|
||||
// .disable()
|
||||
// .authorizeHttpRequests()
|
||||
// .requestMatchers("/api/hdfs/**") // Specific API endpoints that don't require authentication
|
||||
// .permitAll()
|
||||
// .requestMatchers("/api/**") // Other endpoints that should be open
|
||||
// .permitAll()
|
||||
// .anyRequest()
|
||||
// .authenticated() // All other requests require authentication
|
||||
// .and()
|
||||
// .sessionManagement()
|
||||
// .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
|
||||
// .and()
|
||||
// .authenticationProvider(authenticationProvider)
|
||||
// .addFilterBefore(jwtAuthenticationFilter, UsernamePasswordAuthenticationFilter.class);
|
||||
//
|
||||
// return http.build();
|
||||
// }
|
||||
|
||||
@Bean
|
||||
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
|
||||
return http
|
||||
.securityMatcher("/**")
|
||||
.authorizeHttpRequests(auth -> auth
|
||||
.requestMatchers("/api/hdfs/**", "/api/**").permitAll()
|
||||
.anyRequest().authenticated()
|
||||
)
|
||||
.sessionManagement(session -> session
|
||||
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
|
||||
)
|
||||
.authenticationProvider(authenticationProvider)
|
||||
.addFilterBefore(jwtAuthenticationFilter, UsernamePasswordAuthenticationFilter.class)
|
||||
.csrf(csrf -> csrf.disable())
|
||||
.cors(cors -> {}) // 🔥 This line enables CORS and connects to your CorsConfigurationSource bean
|
||||
.build();
|
||||
}
|
||||
|
||||
|
||||
|
||||
// @Bean
|
||||
// CorsConfigurationSource corsConfigurationSource() {
|
||||
// CorsConfiguration configuration = new CorsConfiguration();
|
||||
//
|
||||
// configuration.setAllowedOrigins(List.of("*"));
|
||||
// configuration.setAllowedMethods(List.of("GET", "PUT", "DELETE", "POST"));
|
||||
// configuration.setAllowedHeaders(List.of("Authorization", "Content-Type"));
|
||||
//
|
||||
// UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
|
||||
//
|
||||
// source.registerCorsConfiguration("/**", configuration);
|
||||
//
|
||||
// return source;
|
||||
// }
|
||||
|
||||
@Bean
|
||||
CorsConfigurationSource corsConfigurationSource() {
|
||||
CorsConfiguration configuration = new CorsConfiguration();
|
||||
|
||||
// 🔥 Allow all origins (wildcard) safely with credentials
|
||||
configuration.setAllowedOriginPatterns(List.of("*"));
|
||||
|
||||
configuration.setAllowedMethods(List.of("GET", "POST", "PUT", "DELETE", "OPTIONS"));
|
||||
configuration.setAllowedHeaders(List.of("*"));
|
||||
configuration.setExposedHeaders(List.of("Authorization"));
|
||||
configuration.setAllowCredentials(true); // Needed for cookies / Authorization headers
|
||||
|
||||
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
|
||||
source.registerCorsConfiguration("/**", configuration);
|
||||
return source;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
package com.skycrate.backend.skycrateBackend.controller;
|
||||
|
||||
import com.skycrate.backend.skycrateBackend.dto.LoginRequest;
|
||||
import com.skycrate.backend.skycrateBackend.security.JwtService;
|
||||
import com.skycrate.backend.skycrateBackend.services.JwtService;
|
||||
import com.skycrate.backend.skycrateBackend.entity.User;
|
||||
import com.skycrate.backend.skycrateBackend.repository.UserRepository;
|
||||
import com.skycrate.backend.skycrateBackend.security.TokenBlacklistService;
|
||||
|
||||
@@ -1,24 +0,0 @@
|
||||
package com.skycrate.backend.skycrateBackend.controller;
|
||||
|
||||
import com.skycrate.backend.skycrateBackend.dto.SignupRequest;
|
||||
import com.skycrate.backend.skycrateBackend.services.UserService;
|
||||
import jakarta.validation.Valid;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/auth")
|
||||
public class SignupController {
|
||||
|
||||
private final UserService userService;
|
||||
|
||||
public SignupController(UserService userService) {
|
||||
this.userService = userService;
|
||||
}
|
||||
|
||||
@PostMapping("/signup")
|
||||
public ResponseEntity<?> signup(@Valid @RequestBody SignupRequest request) {
|
||||
userService.registerUser(request);
|
||||
return ResponseEntity.ok("User registered successfully");
|
||||
}
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
package com.skycrate.backend.skycrateBackend.dto;
|
||||
|
||||
import com.skycrate.backend.skycrateBackend.services.EncryptionUtil;
|
||||
|
||||
import java.security.KeyPair;
|
||||
|
||||
public class User {
|
||||
private String username;
|
||||
private KeyPair keyPair;
|
||||
|
||||
public User(String username) throws Exception {
|
||||
this.username = username;
|
||||
this.keyPair = EncryptionUtil.generateKeyPair();
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
public KeyPair getKeyPair() {
|
||||
return keyPair;
|
||||
}
|
||||
}
|
||||
@@ -1,25 +0,0 @@
|
||||
package com.skycrate.backend.skycrateBackend.repository;
|
||||
|
||||
import com.skycrate.backend.skycrateBackend.dto.User;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@Service
|
||||
public class UserManager {
|
||||
private Map<String, User> users = new HashMap<>();
|
||||
|
||||
public User getUser(String username) throws Exception {
|
||||
if (!users.containsKey(username)) {
|
||||
users.put(username, new User(username));
|
||||
}
|
||||
return users.get(username);
|
||||
}
|
||||
|
||||
public boolean authenticate(String username, String password) {
|
||||
// Implement your authentication logic here
|
||||
return "admin".equals(username) && "password123".equals(password);
|
||||
}
|
||||
}
|
||||
|
||||
+1
@@ -2,6 +2,7 @@ package com.skycrate.backend.skycrateBackend.security;
|
||||
|
||||
import com.skycrate.backend.skycrateBackend.repository.UserRepository;
|
||||
import com.skycrate.backend.skycrateBackend.entity.User;
|
||||
import com.skycrate.backend.skycrateBackend.services.JwtService;
|
||||
import jakarta.servlet.FilterChain;
|
||||
import jakarta.servlet.ServletException;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
|
||||
@@ -1,43 +0,0 @@
|
||||
package com.skycrate.backend.skycrateBackend.security;
|
||||
|
||||
import com.skycrate.backend.skycrateBackend.entity.User;
|
||||
import io.jsonwebtoken.*;
|
||||
import io.jsonwebtoken.security.Keys;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.security.Key;
|
||||
import java.util.Date;
|
||||
|
||||
@Service
|
||||
public class JwtService {
|
||||
|
||||
private static final String SECRET = "super-secret-256-bit-key-which-you-should-keep-safe!";
|
||||
private static final long EXPIRATION_MS = 1000 * 60 * 60; // 1 hour
|
||||
|
||||
private final Key key = Keys.hmacShaKeyFor(SECRET.getBytes());
|
||||
|
||||
public String generateToken(User user) {
|
||||
return Jwts.builder()
|
||||
.setSubject(user.getEmail())
|
||||
.setIssuedAt(new Date())
|
||||
.setExpiration(new Date(System.currentTimeMillis() + EXPIRATION_MS))
|
||||
.signWith(key)
|
||||
.compact();
|
||||
}
|
||||
|
||||
public String extractUsername(String token) {
|
||||
return Jwts.parserBuilder().setSigningKey(key).build()
|
||||
.parseClaimsJws(token)
|
||||
.getBody().getSubject();
|
||||
}
|
||||
|
||||
public boolean isTokenValid(String token, User user) {
|
||||
return extractUsername(token).equals(user.getEmail()) && !isTokenExpired(token);
|
||||
}
|
||||
|
||||
public boolean isTokenExpired(String token) {
|
||||
return Jwts.parserBuilder().setSigningKey(key).build()
|
||||
.parseClaimsJws(token)
|
||||
.getBody().getExpiration().before(new Date());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user