在Spring Security中的很多内容校验都是通过对HTTP请求中的数据项进行的判断
RestClient是VSCode的API测试插件,创建后缀为
.http
的文件则会被VSCode识别成Rest Client的工具文件
@GetMapping("/01") public String firstApi() { return "Hello World"; }
GET http://localhost:8080/test/01
GET http://localhost:8080/test/01 Authorization: Basic user:0baf3007-ffa6-4ce1-96ea-efbb356cf3bd
@PutMapping("/02") public String secApi(@RequestParam String name) { return "Hello " + name; }
PUT http://localhost:8080/test/02?name=Jack Authorization: Basic user a08a4450-a0ff-4bdb-9b96-d31835ff2b40
logging.level.org.springframework.security.web=DEBUG
Invalid CSRF token found for http://localhost:8080/test/02?name=Jack
@EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.csrf(AbstractHttpConfigurer::disable) .httpBasic(Customizer.withDefaults()) .formLogin(form -> form.loginPage("/")); } }
@PostMapping("/03") public String thiApi(@RequestBody User user) { return "Hello " + user.getName(); } @Data private class User { String name; String gender; }
POST http://localhost:8080/test/03 Authorization: Basic user 5c11e9f9-c9d8-4322-8e5e-03fc36d91e6e { "name": "Susan", "gender": "Man" }
Unsupported Media Type
POST http://localhost:8080/test/03 Authorization: Basic user 5c11e9f9-c9d8-4322-8e5e-03fc36d91e6e Content-Type: application/json { "name": "Susan", "gender": "Man" }