Skip to content
This repository has been archived by the owner on Sep 29, 2024. It is now read-only.

Commit

Permalink
Merge pull request #778 from SE-TINF22B6/cors-fix
Browse files Browse the repository at this point in the history
Fix CORS issues
  • Loading branch information
denniskp authored Jun 3, 2024
2 parents 5f8fa83 + c116149 commit f98462d
Show file tree
Hide file tree
Showing 17 changed files with 9 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/main/java/de/tinf22b6/dhbwhub/config/WebConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("http://localhost:3000", "https://www.dhbwhub.de")
.allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
//.allowedHeaders("Content-Type", "Authorization", "Access-Control-Allow-Origin", "Accept")
.allowedHeaders("Content-Type", "Authorization", "Accept")
.allowCredentials(true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import java.util.List;

@RestController
@CrossOrigin(origins = {"https://www.dhbwhub.de", "http://localhost:3000"})
@RequestMapping(value = "/account")
public class AccountController {
private final AccountService service;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import java.util.List;

@RestController
@CrossOrigin(origins = {"https://www.dhbwhub.de", "http://localhost:3000"})
@RequestMapping(value = "/administrator")
public class AdministratorController {
private final AdministratorService service;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import java.util.stream.Collectors;

@RestController
@CrossOrigin(origins = {"https://www.dhbwhub.de", "http://localhost:3000"})
@RequestMapping("/api/auth")
@RequiredArgsConstructor
public class AuthController {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import java.util.List;

@RestController
@CrossOrigin(origins = {"https://www.dhbwhub.de", "http://localhost:3000"})
@RequestMapping(value = "/comment")
public class CommentController {
private final CommentService service;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import java.util.List;

@RestController
@CrossOrigin(origins = {"https://www.dhbwhub.de", "http://localhost:3000"})
@RequestMapping(value = "/course")
public class CourseController {
private final CourseService service;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import java.util.List;

@RestController
@CrossOrigin(origins = {"https://www.dhbwhub.de", "http://localhost:3000"})
@RequestMapping(value = "/event")
public class EventController {
private final EventService service;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import java.util.List;

@RestController
@CrossOrigin(origins = {"https://www.dhbwhub.de", "http://localhost:3000"})
@RequestMapping(value = "/faculty")
public class FacultyController {
private final FacultyService service;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import java.util.List;

@RestController
@CrossOrigin(origins = {"https://www.dhbwhub.de", "http://localhost:3000"})
@RequestMapping(value = "/friendship")
public class FriendshipController {
private final FriendshipService service;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import java.util.List;

@RestController
@CrossOrigin(origins = {"https://www.dhbwhub.de", "http://localhost:3000"})
@RequestMapping(value = "/notification")
public class NotificationController {
private final NotificationService service;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import java.util.List;

@RestController
@CrossOrigin(origins = {"https://www.dhbwhub.de", "http://localhost:3000"})
@RequestMapping(value = "/picture")
public class PictureController {
private final PictureService service;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import java.util.List;

@RestController
@CrossOrigin(origins = {"https://www.dhbwhub.de", "http://localhost:3000"})
@RequestMapping(value = "/post")
public class PostController {
private final PostService service;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import java.util.List;

@RestController
@CrossOrigin(origins = {"https://www.dhbwhub.de", "http://localhost:3000"})
@RequestMapping(value = "/saved-post")
public class SavedPostController {
private final SavedPostService service;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import java.util.List;

@RestController
@CrossOrigin(origins = {"https://www.dhbwhub.de", "http://localhost:3000"})
@RequestMapping(value = "/user")
public class UserController {
private final UserService service;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
import org.springframework.security.config.Customizer;
import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration;
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
Expand Down Expand Up @@ -71,15 +72,15 @@ public PasswordEncoder passwordEncoder() {

@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http.csrf(AbstractHttpConfigurer::disable) // TODO: CodeQL doesn't like that
http.csrf(AbstractHttpConfigurer::disable)
// CSRF protection is disabled as this is a stateless API. The application uses token-based authentication, making CSRF less relevant.
.authorizeHttpRequests(authorizeRequests ->
authorizeRequests.requestMatchers(PUBLIC_ENDPOINTS).permitAll()
.anyRequest().authenticated()
).exceptionHandling(exceptionHandling -> exceptionHandling.authenticationEntryPoint(unauthorizedHandler))
.addFilterBefore(authenticationJwtTokenFilter(), UsernamePasswordAuthenticationFilter.class);
.addFilterBefore(authenticationJwtTokenFilter(), UsernamePasswordAuthenticationFilter.class)
.cors(Customizer.withDefaults());

return http.build();
}

}

3 changes: 1 addition & 2 deletions src/main/web/src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ const config = {
apiUrl: 'https://56e66ce8-2ac2-4635-982a-f19f20896303.ka.bw-cloud-instance.org:8443/',
googleClientId: '973066251162-r60h517iddja3k756d2f6n8sng5nn24q.apps.googleusercontent.com',
tooltipMessage: "Please sign up or log in to use this feature",
adsOn: true,
adsOn: false,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
'Accept': 'application/json'
}
};
Expand Down
4 changes: 2 additions & 2 deletions src/main/web/src/services/LikeService.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const handleLike = async (
localStorage.setItem(`liked_${postId}`, 'true');

await fetch(config.apiUrl + `post/increase-likes`, {
method: 'POST',
method: 'PUT',
headers: headersWithJwt,
body: JSON.stringify({
userId: userId,
Expand All @@ -38,7 +38,7 @@ const handleLike = async (
localStorage.removeItem(`liked_${postId}`);

await fetch(config.apiUrl + `post/decrease-likes`, {
method: 'POST',
method: 'PUT',
headers: headersWithJwt,
body: JSON.stringify({
userId: userId,
Expand Down

0 comments on commit f98462d

Please sign in to comment.