Skip to content

Commit c45a14f

Browse files
committed
Add axios CSRF interceptor and update CORS config
Introduced a new axios utility in the React app to automatically handle CSRF tokens and redirect on 401 errors. Updated TurStaticResourceConfiguration to support multiple allowed origins, improved CORS settings, and added jackson-datatype-jsr310 dependency in the backend.
1 parent 4baa5f0 commit c45a14f

File tree

4 files changed

+60
-4
lines changed

4 files changed

+60
-4
lines changed

turing-app/pom.xml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
<packaging>jar</packaging>
1515
<properties>
1616
<elasticsearch.version>9.2.3</elasticsearch.version>
17+
<jackson.version>2.20.1</jackson.version>
1718
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1819
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
1920
<maven.compiler.release>21</maven.compiler.release>
@@ -88,7 +89,12 @@
8889
<dependency>
8990
<groupId>com.fasterxml.jackson.datatype</groupId>
9091
<artifactId>jackson-datatype-hibernate7</artifactId>
91-
<version>2.20.1</version>
92+
<version>${jackson.version}</version>
93+
</dependency>
94+
<dependency>
95+
<groupId>com.fasterxml.jackson.datatype</groupId>
96+
<artifactId>jackson-datatype-jsr310</artifactId>
97+
<version>${jackson.version}</version>
9298
</dependency>
9399
<dependency>
94100
<groupId>com.viglet.turing</groupId>

turing-app/src/main/java/com/viglet/turing/spring/TurStaticResourceConfiguration.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,17 @@ public class TurStaticResourceConfiguration implements WebMvcConfigurer {
4444
public static final String FORWARD_SN_TEMPLATES_BROWSER_INDEX_HTML = "forward:/sn/templates/browser/index.html";
4545
public static final String FORWARD_WELCOME_BROWSER_INDEX_HTML = "forward:/welcome/browser/index.html";
4646
public static final String FORWARD_CONSOLE_BROWSER_INDEX_HTML = "forward:/console/browser/index.html";
47-
@Value("${turing.allowedOrigins:localhost}")
47+
@Value("${turing.allowedOrigins:http://localhost:5173,http://localhost:2700}")
4848
private String allowedOrigins;
4949

5050
@Override
5151
public void addCorsMappings(CorsRegistry registry) {
52-
registry.addMapping("/api/**").allowedOrigins(allowedOrigins).allowedMethods("PUT", "DELETE", "GET", "POST")
53-
.allowCredentials(false).maxAge(3600);
52+
registry.addMapping("/api/**")
53+
.allowedOrigins(allowedOrigins.split(","))
54+
.allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS", "PATCH")
55+
.allowedHeaders("*")
56+
.allowCredentials(true)
57+
.maxAge(3600);
5458
}
5559

5660
@Override

turing-react/src/lib/axios.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import axios from "axios";
2+
3+
// Function to get CSRF token from cookies
4+
function getCsrfToken(): string | null {
5+
const match = document.cookie.match(/XSRF-TOKEN=([^;]+)/);
6+
return match ? decodeURIComponent(match[1]) : null;
7+
}
8+
9+
// Configure axios defaults
10+
axios.defaults.withCredentials = true;
11+
12+
// Add request interceptor to include CSRF token
13+
axios.interceptors.request.use(
14+
(config) => {
15+
const csrfToken = getCsrfToken();
16+
if (
17+
csrfToken &&
18+
["post", "put", "delete", "patch"].includes(
19+
config.method?.toLowerCase() || "",
20+
)
21+
) {
22+
config.headers["X-XSRF-TOKEN"] = csrfToken;
23+
}
24+
return config;
25+
},
26+
(error) => {
27+
return Promise.reject(error);
28+
},
29+
);
30+
31+
// Add response interceptor to handle errors
32+
axios.interceptors.response.use(
33+
(response) => response,
34+
(error) => {
35+
if (error.response?.status === 401) {
36+
// Handle unauthorized - redirect to login if not already on login page
37+
if (!window.location.pathname.startsWith("/login")) {
38+
window.location.href = "/login";
39+
}
40+
}
41+
return Promise.reject(error);
42+
},
43+
);
44+
45+
export default axios;

turing-react/src/main.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import '@/lib/axios'; // Configure axios interceptors for CSRF
12
import axios from 'axios'
23
import React from 'react'
34
import { createRoot } from 'react-dom/client'

0 commit comments

Comments
 (0)