Added server-side CORS support for API endpoints
This commit is contained in:
parent
2e73088d2a
commit
7992705e9c
4 changed files with 23 additions and 11 deletions
|
@ -4,6 +4,9 @@ import org.slf4j.Logger;
|
|||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
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
|
||||
public class EpqapiApplication extends Thread {
|
||||
|
@ -14,4 +17,13 @@ public class EpqapiApplication extends Thread {
|
|||
SpringApplication.run(EpqapiApplication.class, args);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public WebMvcConfigurer corsConfigurer() {
|
||||
return new WebMvcConfigurer() {
|
||||
@Override
|
||||
public void addCorsMappings(CorsRegistry registry) {
|
||||
registry.addMapping("/**").allowedOrigins("*");
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
package me.imsonmia.epqapi.config;
|
|
@ -5,6 +5,7 @@ import java.util.ArrayList;
|
|||
import java.util.Optional;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
|
@ -38,8 +39,13 @@ public class UserController {
|
|||
// malformed request
|
||||
return ResponseEntity.badRequest().build();
|
||||
} else {
|
||||
boolean exists = userRepository.existsByUserName(name.get());
|
||||
// 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 {
|
||||
// get by id branch
|
||||
|
@ -85,13 +91,4 @@ public class UserController {
|
|||
}
|
||||
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;
|
||||
// }
|
||||
}
|
||||
|
|
|
@ -1,11 +1,13 @@
|
|||
package me.imsonmia.epqapi.repository;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
|
||||
import me.imsonmia.epqapi.model.User;
|
||||
|
||||
public interface UserRepository extends CrudRepository<User, Long> {
|
||||
User findByUserName(String userName);
|
||||
Optional<User> findByUserName(String userName);
|
||||
|
||||
boolean existsByUserName(String userName);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue