最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

mongodb - Postman is sending me 401 even though the use is saved to database - Stack Overflow

programmeradmin2浏览0评论

I have made an API endpoint to register a new user. But when I try hitting that API through Postman, it gives me a 401 error.

The user is successfully created and saved in the database, but I get a 401 error while returning ResponseEntity from the controller.

Controller:

package com.classroomconnect.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.classroomconnect.dto.UserRegistrationReqDto;
import com.classroomconnect.dto.UserRegistrationResDto;
import com.classroomconnect.service.UserService;

@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping(value="/user/register", produces = "application/json")
    public ResponseEntity<UserRegistrationResDto> registerNewUser(@RequestBody UserRegistrationReqDto userDto) {
        UserRegistrationResDto userRegistrationResDto = userService.register(userDto);
        return ResponseEntity.status(HttpStatus.CREATED).body(userRegistrationResDto); 
    }
}

Security Configuration:

package com.classroomconnect.security;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.Customizer;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
import org.springframework.security.web.SecurityFilterChain;

@Configuration
@EnableWebSecurity
public class SecurityConfig {

    @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity httpSecurity) throws Exception {
        httpSecurity.authorizeHttpRequests(auth -> 
                auth.requestMatchers(HttpMethod.POST, "/user/register").permitAll()
                    .anyRequest().authenticated())
                .httpBasic(Customizer.withDefaults());

        return httpSecurity.build();
    }

    @Bean
    public UserDetailsService userDetailsService() {
        UserDetails yax = User.withUsername("yax").password("{noop}yax").roles("USER").build();
        UserDetails hardik = User.withUsername("hardik").password("{noop}hardik").roles("USER").build();
        return new InMemoryUserDetailsManager(yax, hardik);
    }
}

Here is the error I'm seeing in Postman: What I have tried:

  • Disabling CSRF in SecurityConfig.
  • Ensuring the correct headers are sent in Postman (Accept: application/json).
  • Checking Spring Security logs for insights.

Expected behavior: I expect the endpoint to return 201 Created with the newly registered user details.

How can I resolve this 401 error?

发布评论

评论列表(0)

  1. 暂无评论