【问题标题】:Juste GET Request work with POSTMAN, POST Request return 403Forbidden : Spring Boot mongoDB (Postman Test)Juste GET 请求与 POSTMAN 一起使用,POST 请求返回 403Forbidden:Spring Boot mongoDB(邮递员测试)
【发布时间】:2020-05-15 04:50:37
【问题描述】:

您能否向我解释一下如何在邮递员中发出 POST 请求以将新用户添加到我的用户集合中。

问题是我可以使用 Get Request 添加用户,但 POST 请求返回 403 Forbidden。

你能告诉我如何授权 POST 请求吗?

我在我的控制器中尝试了这个解决方案,但没有解决任何问题:

@RequestMapping(value = "/users", method = { RequestMethod.GET, RequestMethod.DELETE, RequestMethod.POST })

在 spring-boot 中我有 3 个实体:用户、中心和角色

它们与以下的关系:

班级用户:

@Document
public class User {

    @Id
    private String id;
    private String login;
    private String password;
    private String nom;
    private String prenom;
    private long telephone;
    private long idCCMS;
    private String matricule;

    @DBRef
    private Centre centres;

    @DBRef
    private Role roles;

类角色:

@Document
public class Role {

    @Id
    private String id;
    private String role;
    private String description;

    @DBRef
    private Collection<User> users = new ArrayList<>();

课堂中心

@Document
public class Centre {

    @Id
    private String id;
    private String reference;
    private String libelle;

    @DBRef
    private Collection<User> users = new ArrayList<>();

我的仓库:

@Repository
public interface UserRepository extends MongoRepository<User, String> {

    List<User>findByMatricule(String matricule);
    User findByLogin(String login);
    List<User>findByNom(String nom);
    List<User>findByPrenom(String prenom);
}

我的控制器:

@RestController
@RequestMapping(value = "/users", method = { RequestMethod.GET, RequestMethod.DELETE, RequestMethod.POST })
@CrossOrigin(origins = { "http://localhost:4200" })
public class UserController {

    @Autowired
    private UserRepository userRepo;

//  @Autowired
//  private MongoTemplate mtUser;

    @GetMapping("/all")
    @Query(value = "{}", fields = "{'objectContentAsJson':0}")
    public Iterable<User> getall() {
        return (userRepo.findAll());
    }

    @PostMapping("/add")
    public User save(@RequestBody User user) {
        return userRepo.save(user);
    }

为了在我的数据库中添加一个用户,我在邮递员的正文中添加了这段代码:

{
    "id": "5e319750fcf3c73b589b6211",
    "login": "aaaa",
    "password": "aaaa",
    "nom": "aaaa",
    "prenom": "aaaa",
    "telephone": 97408105,
    "idCCMS": 123456,
    "matricule": "mat123",
    "centres": "5e319dd0730b1659d5910e0c",
    "roles": "5e319dd0730b1659d5910e0d"
}

在json代码的center和role字段中添加Center ID和Role ID是否正确?

在 Postman 中,我在“授权”类型“基本身份验证”部分添加了 Spring Security“使用”的默认用户名和我的应用程序服务器生成的密码:

【问题讨论】:

  • 响应为401 Unauthorized,这意味着您需要在请求中添加用户名和密码。
  • 我相信你已经在 pom.xml 中添加了 Spring Security 依赖。如果是这样,请检查控制台中打印的日志以生成随机密码。使用生成的密码和用户名user
  • @Mansoor 是的,我使用 Spring Security,我有这种依赖关系。在 Postman 中,我在“授权”类型“基本身份验证”部分添加了 Spring Security“使用”的默认用户名和我的应用程序服务器生成的密码
  • 你能添加你的控制器方法吗?

标签: spring mongodb post spring-data postman


【解决方案1】:

响应为401 Unauthorized,这意味着您需要在请求中添加用户名和密码。

在你的 pom.xml 中查看你是否有依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>

如果以上为真,Spring security 将生成一个随机密码。将其与用户名一起用作user 在 Postman 中进行身份验证。

【讨论】:

  • 是的,我使用 Spring Security,我有这个依赖。在 Postman 中,我在“授权”类型“基本身份验证”部分添加了 Spring Security“使用”的默认用户名和我的应用程序服务器生成的密码。
  • user 是默认用户名。
  • 是的,我使用了 Spring Security 的默认名称,因为到目前为止我还没有对 Spring Security 进行任何配置,所以我保持原样
  • 我的方法正确吗? @PostMapping("/add") public User save(@RequestBody User user) { return userRepo.save(user); }
  • 请编辑您的问题并在那里添加控制器方法。新访问者会知道这些变化。
猜你喜欢
  • 1970-01-01
  • 2018-09-15
  • 2021-12-31
  • 1970-01-01
  • 2020-12-05
  • 2017-12-30
  • 1970-01-01
  • 2021-01-21
  • 2019-01-07
相关资源
最近更新 更多