我已经使用angular 9创建了一个登录模块。当我尝试使用正确的凭据登录时,我收到了json响应200,并在inspect元素中对其进行了检查。但是,当我调试角度服务时,响应为空。请有人帮助我,为什么会这样?
login.service.ts
load(userLogin : UserLogin): Promise<any> {
return new Promise((resolve, reject) => {
this._http
.post('login',userLogin)
.pipe(
catchError(res => {
resolve();
return throwError(res);
})
)
.subscribe(
(res: any) => {
console.log('result', res);
},
() => {
reject();
},
() => {
resolve();
}
);
});
}
LoginResource.java
@PostMapping
public Map<String, Object> login(@RequestBody @Validated LoginDto loginDto) throws IOException {
log.info("=>Login POST");
UsernamePasswordAuthenticationToken authReq =
new UsernamePasswordAuthenticationToken(loginDto.getEmail(), loginDto.getPassword());
Authentication auth = authenticationManager.authenticate(authReq);
SecurityContextHolder.getContext().setAuthentication(auth);
String jwt = jwtUtils.generateJwtToken(auth);
Registration userDetails = (Registration) auth.getPrincipal();
List<String> roles =
userDetails.getRoles().stream()
.map(Role::getPrivileges)
.flatMap(Collection::stream)
.map(Privilege::getName)
.collect(Collectors.toList());
return new HashMap<String, Object>() {
{
put("token", jwt);
put("id", userDetails.getId());
put("username", userDetails.getUserName());
put("roles", roles);
}
};
}