【问题标题】:Unauthorized JWT Authentication未经授权的 JWT 身份验证
【发布时间】:2021-07-17 03:20:38
【问题描述】:

我正在尝试使用 jwt 和 mongodb 进行 Spring Boot 身份验证,并且我正在发送带有角度的请求正文,我什至尝试使用邮递员,但我收到了这个错误:

未经授权的错误:无法使用带参数的构造函数 NO_CONSTRUCTOR 实例化 com.example.Educart.models.User 控制器

    @PostMapping("/signin")
    @CrossOrigin("http://localhost:4200")
    public ResponseEntity<?> authenticateUser(@Valid @RequestBody LoginRequest loginRequest) {

        Authentication authentication = authenticationManager.authenticate(
                new UsernamePasswordAuthenticationToken(loginRequest.getUsername(), loginRequest.getPassword()));

        SecurityContextHolder.getContext().setAuthentication(authentication);
        String jwt = jwtUtils.generateJwtToken(authentication);

        UserDetailsImpl userDetails = (UserDetailsImpl) authentication.getPrincipal();
        List<String> roles = userDetails.getAuthorities().stream()
                .map(item -> item.getAuthority())
                .collect(Collectors.toList());

        return ResponseEntity.ok(new JwtResponse(jwt,
                userDetails.getId(),
                userDetails.getUsername(),
                userDetails.getEmail(),
                userDetails.getAdress(),
                userDetails.getnum(),
                roles));

    }

jwt.ts

    
    private static final Logger logger = LoggerFactory.getLogger(AuthEntryPointJwt.class);

    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response,
                         AuthenticationException authException) throws IOException, ServletException {
        logger.error("Unauthorized error: {}", authException.getMessage());
        response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Error: Unauthorized");
    }

【问题讨论】:

  • 即使我改错了密码,也会出现同样的错误

标签: angular mongodb spring-boot jwt


【解决方案1】:

解决方案是给空的用户承包商

【讨论】:

    猜你喜欢
    • 2021-02-16
    • 2018-06-13
    • 2021-07-23
    • 1970-01-01
    • 2018-07-09
    • 2018-10-16
    • 2016-08-15
    • 1970-01-01
    相关资源
    最近更新 更多