【发布时间】: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