我正在尝试使用 jwt 和 mongodb 进行 Spring Boot 身份验证,并且我正在发送带有 angular 的请求正文,我什至尝试使用邮递员,但出现此错误:
<块引用>未授权错误:无法使用带参数的构造函数 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");
}
答案 0 :(得分:0)
解决方案是伟大的空用户承包商