Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/388.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring Boot\JWT:类型ResponseEntity中的方法ok(T)不适用于参数(JwtResponse)_Java_Spring_Spring Boot_Spring Security - Fatal编程技术网

Java Spring Boot\JWT:类型ResponseEntity中的方法ok(T)不适用于参数(JwtResponse)

Java Spring Boot\JWT:类型ResponseEntity中的方法ok(T)不适用于参数(JwtResponse),java,spring,spring-boot,spring-security,Java,Spring,Spring Boot,Spring Security,我下面有这个方法 JwtResponse是一个简单的POJO 有人能解释一下我为什么会在主题上出错吗 ResponseEntity.ok(new JwtResponse... 如何修复它 谢谢 @PostMapping("/signin") public ResponseEntity<?> authenticateUser(@Valid @RequestBody LoginRequest loginRequest) { Authentication a

我下面有这个方法

JwtResponse是一个简单的POJO

有人能解释一下我为什么会在主题上出错吗

ResponseEntity.ok(new JwtResponse...
如何修复它

谢谢

@PostMapping("/signin")
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(), 
                                             roles));
}
@PostMapping(/signin)
公共响应身份验证者(@Valid@RequestBody LoginRequest LoginRequest){
Authentication=authenticationManager.authenticate(
新的UsernamePasswordAuthenticationToken(loginRequest.getUsername(),loginRequest.getPassword());
SecurityContextHolder.getContext().setAuthentication(身份验证);
字符串jwt=jwtUtils.generateJwtToken(身份验证);
UserDetailsImpl userDetails=(UserDetailsImpl)身份验证。getPrincipal();
List roles=userDetails.getAuthorities().stream()
.map(项目->项目.getAuthority())
.collect(Collectors.toList());
返回响应正确(新jwt响应(jwt,
userDetails.getId(),
userDetails.getUsername(),
userDetails.getEmail(),
角色);;
}
找到了

应该是:

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

实际上,我正在寻找错误背后的原因,但我发现您编写的是正常的,例如,如果您进入
ok(T)
方法,您将发现与
ok().body(T)
完全相同的实现,这使您很难理解!