How to preprocess the input parameters of the method of RequestMapping annotations in controller? Convert token to user input method

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?


if you want to fetch data from header, use RequestHeader , similar to RequestParam


Brother, you have all got the token, and then convert the token into a user object and return it according to the conversion rules of your system.

the first type:

@PostMapping("add")
    @ResponseBody
    public Comment commentAdd(@RequestBody CommentAddVO VO, HttpServletRequest request) {
        String headerToken = request.getHeader("token");       
        User user = .....;//tokenuser
         Comment comment =  comRep.save(VO.toComment(user));
        //
        return comment;
    }

if you find it troublesome, you can write a basic controller, such as the class name BaseController. Your controller inherits the BaseController,. Write a method to get the user logic in BaseController and call it when you use user.

second: use your UserUtil

public class UserUtil {
    
    public static User getUser() {
        //
        HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
        String headerToken = request.getHeader("token");
        User user = .....;//tokenuser
        return user;
    }
}

@PostMapping("add")
    @ResponseBody
    public Comment commentAdd(@RequestBody CommentAddVO VO) {
        String headerToken = request.getHeader("token");       
        User user = UserUtil.getUser();
         Comment comment =  comRep.save(VO.toComment(user));
        //
        return comment;
    }

third: if you have to do it your way, pass in the user object.
write a filter or interceptor to get the token, and convert it to a user object, and then put it into the request, as mentioned above.


if useful to spring security, you can use the @ AuthenticationPrincipal annotation to achieve this effect.

Menu