Skip to content

Commit

Permalink
add: csrf token demo
Browse files Browse the repository at this point in the history
  • Loading branch information
thutasann committed Feb 16, 2025
1 parent 67eb5c6 commit bdf9c17
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.thuta.spring_security_exercise.Controllers;

import java.util.ArrayList;
import java.util.List;

import org.springframework.security.web.csrf.CsrfToken;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.thuta.spring_security_exercise.Entity.Student;

import jakarta.servlet.http.HttpServletRequest;

@RestController
public class StudentController {
private List<Student> students = new ArrayList<>(List.of(
new Student(1, "Navin", 60),
new Student(2, "Kiran", 65)));

@GetMapping("/students")
public List<Student> getStudents() {
return this.students;
}

@GetMapping("/csrf-token")
public CsrfToken getCsrfToken(HttpServletRequest request) {
return (CsrfToken) request.getAttribute("_csrf");
}

@PostMapping("/students")
public Student addStudent(@RequestBody Student student) {
students.add(student);
return student;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.thuta.spring_security_exercise.Entity;

public class Student {
private int id;
private String name;
private int marks;

public Student(int id, String name, int marks) {
this.id = id;
this.name = name;
this.marks = marks;
}

public int getId() {
return this.id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return this.name;
}

public void setName(String name) {
this.name = name;
}

public int getMarks() {
return this.marks;
}

public void setMarks(int marks) {
this.marks = marks;
}
}

0 comments on commit bdf9c17

Please sign in to comment.