【问题标题】:403 Forbidden post request spring boot not working403 Forbidden post request spring boot 不工作
【发布时间】: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


【解决方案1】:

您的代码正在运行,但我想再补充一点,使用这种类型的结构可以防止 SQL 注入并改善您的创建 API 响应。

        @CrossOrigin(origins = "http://localhost:3000")
        @PostMapping(value = "/createEmployee")
        public ResponseEntity<ClientsMetaModel> createEmployee(@Valid @RequestBody ClintsModel model) {
            ClientsMetaModel metaModel = new ClientsMetaModel();
            // set all your fields into metamodel by getting it from model
            return new ResponseEntity<ClientsMetaModel>(empRepo.save(metaModel), HttpStatus.CREATED);
        }

您必须将您的idtokendate 所有字段保留在元模型中,然后试试这个。
如果这不起作用,则使用@CrossOrigin(/*) 作为全局。正如@Ananthapadmanabhan 在他的回答中所说,但我建议你使用模型和元模型概念。

【讨论】:

  • 当我删除 @CrossOrigin 注释时它正在工作,而当我使用 CrossOrigin("/*") 但我使用 react 作为前端并且我将需要那个 CrossOrigin 以某种方式防止没有访问错误如何我要管理吗:/
  • 我有点困惑。你的意思是说你正在使用 reactjs 调用你的 API,而你只需要限制来源吗?
  • 是的,我使用跨源注释来启用从我的反应前端到 Spring Boot 后端的跨源共享,但由于我现在已经删除它,我肯定会得到那个跨源错误
【解决方案2】:

您能否尝试在您的端点的所有端口和 url 上启用 CORS,例如:

  @CrossOrigin
   @PostMapping("/clientslist")
   public ResponseEntity<Clients> createEmployee(@Valid @RequestBody Clients client) {

    Clients client1 = clientsRepository.save(client);

    return new ResponseEntity<Clients>(client1,HttpStatus.CREATED);
   }

【讨论】:

  • 当我删除 @CrossOrigin 注释时它正在工作,而当我使用 CrossOrigin("/*") 但我使用 react 作为前端并且我将需要那个 CrossOrigin 以防止没有访问错误时如何我要管理吗:/
  • @FayçalMekdour 抱歉,这是我的一个错误,允许所有来源上的交叉来源只需放置注释而不传递值,因为默认设置允许所有来源。但是你可以添加@CrossOrigin(origins = "http://localhost:9000"),如果你想只允许本地主机再试一次。
  • @FayçalMekdour 尝试在您的邮递员请求标头中将Access-Control-Allow-Origin 设置为http://localhost:3000
猜你喜欢
  • 2021-02-26
  • 2016-03-11
  • 2017-09-21
  • 2019-07-12
  • 1970-01-01
  • 1970-01-01
  • 2022-10-04
  • 2015-11-16
  • 2015-08-13
相关资源
最近更新 更多