first week launch
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
package com.walkguide.config;
|
||||
|
||||
import org.springframework.boot.CommandLineRunner;
|
||||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import com.walkguide.entity.User;
|
||||
import com.walkguide.repository.UserRepository;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class DataSeeder implements CommandLineRunner {
|
||||
|
||||
private final UserRepository userRepository;
|
||||
|
||||
@Override
|
||||
public void run(String... args) throws Exception {
|
||||
// Mesin enkripsi password
|
||||
PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
|
||||
|
||||
// Ngecek kalau tabel users kosong, baru diisi
|
||||
if (userRepository.count() == 0) {
|
||||
|
||||
// 1. Bikin akun Guardian (Pendamping)
|
||||
User guardian = User.builder()
|
||||
.email("guardian@walkguide.com")
|
||||
.password(passwordEncoder.encode("guardian123")) // Password barunya
|
||||
.role("ROLE_GUARDIAN") // Role udah diganti
|
||||
.build();
|
||||
|
||||
// 2. Bikin akun User (Tunanetra)
|
||||
User user = User.builder()
|
||||
.email("user@walkguide.com")
|
||||
.password(passwordEncoder.encode("user123"))
|
||||
.role("ROLE_USER")
|
||||
.build();
|
||||
|
||||
// Simpan ke database
|
||||
userRepository.save(guardian);
|
||||
userRepository.save(user);
|
||||
|
||||
System.out.println("✅ Data Seeder berhasil: Akun Guardian dan User telah masuk ke Database!");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.walkguide.config;
|
||||
|
||||
import io.swagger.v3.oas.models.OpenAPI;
|
||||
import io.swagger.v3.oas.models.info.Info;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
public class OpenApiConfig {
|
||||
|
||||
@Bean
|
||||
public OpenAPI customOpenAPI() {
|
||||
return new OpenAPI()
|
||||
.info(new Info()
|
||||
.title("Walk Guide API")
|
||||
.version("1.0")
|
||||
.description("API Documentation for Walk Guide Application (Final Exam)"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package com.walkguide.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.configuration.EnableWebSecurity;
|
||||
import org.springframework.security.config.http.SessionCreationPolicy;
|
||||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
import org.springframework.security.web.SecurityFilterChain;
|
||||
|
||||
@Configuration
|
||||
@EnableWebSecurity
|
||||
public class SecurityConfig {
|
||||
|
||||
// MESIN HASHING PASSWORD: Biar password yang disimpan di database gak bisa dibaca langsung, kita enkripsi dulu pake BCrypt
|
||||
@Bean
|
||||
public PasswordEncoder passwordEncoder() {
|
||||
return new BCryptPasswordEncoder();
|
||||
}
|
||||
|
||||
// Aturan jalan masuk ke API kita:
|
||||
@Bean
|
||||
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
|
||||
http
|
||||
.cors(cors -> cors.configurationSource(request -> {
|
||||
var corsConfiguration = new org.springframework.web.cors.CorsConfiguration();
|
||||
// Izinkan semua origin localhost dan semua port (biar Chrome gak ngeblok)
|
||||
corsConfiguration.setAllowedOriginPatterns(java.util.List.of(
|
||||
"http://localhost:*",
|
||||
"http://127.0.0.1:*"
|
||||
));
|
||||
corsConfiguration.setAllowedMethods(java.util.List.of("GET", "POST", "PUT", "DELETE", "OPTIONS"));
|
||||
corsConfiguration.setAllowedHeaders(java.util.List.of("*"));
|
||||
corsConfiguration.setAllowCredentials(true);
|
||||
return corsConfiguration;
|
||||
}))
|
||||
.csrf(csrf -> csrf.disable())
|
||||
.sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
|
||||
.authorizeHttpRequests(auth -> auth
|
||||
.requestMatchers("/api/auth/**").permitAll()
|
||||
.anyRequest().authenticated()
|
||||
);
|
||||
|
||||
return http.build();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user