the request header headers for front-end access carries token,. How can I transfer token to user into my method?
the following is the result I want:
User user is the user, transformed from token
@PostMapping("add")
@ResponseBody
public Comment commentAdd(@RequestBody CommentAddVO VO, User user) {
Comment comment = comRep.save(VO.toComment(user));
//
return comment;
}
what should I do?
the way I write it now is to get a static method, get the token, in the current request, and then convert the token to user and return it through the method, as follows:
public static String getUserName() {
//
HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
String cookieToken =CookieUtils.getCookie(request,"token");
System.out.println("cookieToken:" + cookieToken);
String headerToken = request.getHeader("token");
System.out.println("headerToken:" + headerToken);
return headerToken;
}
but I think it"s not as simple as converting token to user directly into the method input parameter, so I"d like to ask you how to do it.
or how to put the header request into the method input parameter?