jaks
jaks

Reputation: 4607

Passing a new header in Spring Interceptor

I want to add authentication logic to interceptor. When service is called, interceptor will authenticate. Once authenticated, I want to put a new header in the request say 'header-user': 'john-doe'. But in interceptor, I am unable to do that, when I add to response.setHeader(), nothing happens. I want to use this new header in actual REST service.

public class AuthInterceptor implements HandlerInterceptor {

  @Override
  public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

  // Authenticate
  // Add header
   response.setHeader("header-user", "john-doe"); // not working

    return true;
  }
...
}

If I add Filter, filter is called before Interceptor.

Upvotes: 1

Views: 5182

Answers (1)

jaks
jaks

Reputation: 4607

I figured out from Using Spring Interceptor that I can use setAttribute

request.setAttribute("user", "john-doe");

In controller side use,

public String testService(@RequestAttribute("user") String user){

Upvotes: 5

Related Questions