Added server-side CORS support for API endpoints

This commit is contained in:
Zhongheng Liu 2024-01-19 21:02:17 +02:00
commit 7992705e9c
No known key found for this signature in database
4 changed files with 23 additions and 11 deletions

View file

@ -4,6 +4,9 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@SpringBootApplication @SpringBootApplication
public class EpqapiApplication extends Thread { public class EpqapiApplication extends Thread {
@ -14,4 +17,13 @@ public class EpqapiApplication extends Thread {
SpringApplication.run(EpqapiApplication.class, args); SpringApplication.run(EpqapiApplication.class, args);
} }
@Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurer() {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**").allowedOrigins("*");
}
};
}
} }

View file

@ -0,0 +1 @@
package me.imsonmia.epqapi.config;

View file

@ -5,6 +5,7 @@ import java.util.ArrayList;
import java.util.Optional; import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
@ -38,8 +39,13 @@ public class UserController {
// malformed request // malformed request
return ResponseEntity.badRequest().build(); return ResponseEntity.badRequest().build();
} else { } else {
boolean exists = userRepository.existsByUserName(name.get());
// Filter by name branch // Filter by name branch
return ResponseEntity.ok().body(userRepository.findByUserName(name.get())); if (!exists) {
return ResponseEntity.notFound().build();
} else {
return ResponseEntity.ok().body(userRepository.findByUserName(name.get()).get());
}
} }
} else { } else {
// get by id branch // get by id branch
@ -85,13 +91,4 @@ public class UserController {
} }
return messages; return messages;
} }
// @PatchMapping("/user/{id}")
// boolean changeUserProperties(@PathVariable(value = "id") Long userId,
// @RequestBody User newUser) {
// if (!userRepository.existsById(userId)) {
// return false;
// }
// userRepository.save(newUser);
// return true;
// }
} }

View file

@ -1,11 +1,13 @@
package me.imsonmia.epqapi.repository; package me.imsonmia.epqapi.repository;
import java.util.Optional;
import org.springframework.data.repository.CrudRepository; import org.springframework.data.repository.CrudRepository;
import me.imsonmia.epqapi.model.User; import me.imsonmia.epqapi.model.User;
public interface UserRepository extends CrudRepository<User, Long> { public interface UserRepository extends CrudRepository<User, Long> {
User findByUserName(String userName); Optional<User> findByUserName(String userName);
boolean existsByUserName(String userName); boolean existsByUserName(String userName);
} }