【发布时间】:2020-05-27 03:56:38
【问题描述】:
我在使用邮递员请求 POST 时遇到 403 禁止请求,get 工作正常,我没有使用任何 spring 安全工具,只是 spring boot,因为我已经看到一些关于禁用 csrf 的答案,这不是我的情况,因为我不使用任何弹簧安全性。
这是我的实体类:
package com.example.demo.entity;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.PrePersist;
import javax.persistence.Table;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;
@Entity
@Table(name="clients")
public class Clients {
@Id
@Column(name="phone")
private Long phone;
@NotBlank(message="Required Field")
@Column(name="firstname")
private String firstname;
@NotBlank(message="Required Field")
@Column(name="lastname")
private String lastname;
@NotBlank(message="Required Field")
@Column(name="birthDate")
private String birthDate;
@NotBlank(message="Required Field")
@Column(name="email")
private String email;
@NotBlank(message="Required Field")
@Column(name="addressClient")
private String addressClient;
@NotBlank(message="Required Field")
@Column(name="gender")
private String gender;
@Column(name="inscriptionDate")
private Date inscriptionDate;
@NotBlank(message="Required Field")
@Size(min=8 , message="Password needs to be more than 8 characters")
@Column(name="passwordClient")
private String passwordClient;
public Clients() {
}
public Clients(Long phone, @NotBlank(message = "Required Field") String firstname,
@NotBlank(message = "Required Field") String lastname,
@NotBlank(message = "Required Field") String birthDate, @NotBlank(message = "Required Field") String email,
@NotBlank(message = "Required Field") String addressClient,
@NotBlank(message = "Required Field") String gender, Date inscriptionDate,
@NotBlank(message = "Required Field") @Size(min = 8, message = "Password needs to be more than 8 characters") String passwordClient) {
super();
this.phone = phone;
this.firstname = firstname;
this.lastname = lastname;
this.birthDate = birthDate;
this.email = email;
this.addressClient = addressClient;
this.gender = gender;
this.inscriptionDate = inscriptionDate;
this.passwordClient = passwordClient;
}
@PrePersist
public void newDate() {
this.inscriptionDate=new Date();
}
public Long getPhone() {
return phone;
}
public void setPhone(Long phone) {
this.phone = phone;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public String getBirthDate() {
return birthDate;
}
public void setBirthDate(String birthDate) {
this.birthDate = birthDate;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getAddressClient() {
return addressClient;
}
public void setAddressClient(String addressClient) {
this.addressClient = addressClient;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public Date getInscriptionDate() {
return inscriptionDate;
}
public void setInscriptionDate(Date inscriptionDate) {
this.inscriptionDate = inscriptionDate;
}
public String getPasswordClient() {
return passwordClient;
}
public void setPasswordClient(String passwordClient) {
this.passwordClient = passwordClient;
}
}
这是我的存储库接口:
package com.example.demo.repositories;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import com.example.demo.entity.Clients;
@Repository
public interface ClientsRepository extends JpaRepository<Clients , Long>{
}
我的控制器类:
package com.example.demo.controllers;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.example.demo.entity.Clients;
import com.example.demo.functions.ClientsFunctionsImpl;
import com.example.demo.repositories.ClientsRepository;
@RestController
public class ClientsController {
@Autowired
private ClientsRepository clientsRepository;
@CrossOrigin("http://localhost:3000")
@GetMapping(path="/clientslist")
public List<Clients> getAllClients(){
return clientsfunctionsimpl.list();
}
@CrossOrigin("http://localhost:3000")
@PostMapping("/clientslist")
public ResponseEntity<Clients> createEmployee(@Valid @RequestBody Clients client) {
Clients client1 = clientsRepository.save(client);
return new ResponseEntity<Clients>(client1,HttpStatus.CREATED);
}
}
【问题讨论】:
-
看来您正在传递模型保存该模型并引用该模型的实例。请尝试使用模型和元模型概念来创建任何东西。
-
我测试这一切工作正常,尽管我建议您使用 ClientsModel 获取数据并使用 ClientsMetaModel 保存数据,它非常有用,它还可以防止 SQL 注入。
-
当我删除 @CrossOrigin 注释时它正在工作,而当我使用 CrossOrigin("/*") 但我使用 react 作为前端并且我将需要那个 CrossOrigin 以某种方式防止没有访问错误如何我要管理吗:/
标签: spring-boot