I'm running a spring boot and a few of my domain services need to know the domain name.
I know that I could capture it at the application layer in the controller method like this:
@RequestMapping(value="/myMapping",method = RequestMethod.POST)
public ModelandView myAction(HttpServletRequest request) {
}
OR that, if I were running a traditional web application with a war that I could configure a listener like this:
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
And access it like this:
((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest()
But I do not want to pass around arguments like I would if I captured it at the controller, and I am not running a war with a web.xml.
How can I have my cake and eat it too?
If you use Spring Security, you could store this info in SecurityContext. The trick is getDetails() method, you can put anything you want in there. I personally use a custom object that stores basic info I need for the current user. This example just puts a simple string representing your domain:
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
@Component
public class DomainInterceptor extends HandlerInterceptorAdapter {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
String domain = null; // TODO extra domain from request here
SecurityContext context = SecurityContextHolder.getContext();
AbstractAuthenticationToken authentication = (AbstractAuthenticationToken) context.getAuthentication();
authentication.setDetails(domain);
return true;
}
}
Then to retrieve the domain (for the current request) anywhere in your app you'd do this:
String domain = SecurityContextHolder.getContext().getAuthentication().getDetails().toString();
This is what I did:
import javax.servlet.http.HttpServletRequest;
import org.springframework.util.Assert;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
public class RequestFetcher {
public static HttpServletRequest getCurrentRequest() {
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
Assert.state(requestAttributes != null, "Could not find current request via RequestContextHolder");
Assert.isInstanceOf(ServletRequestAttributes.class, requestAttributes);
HttpServletRequest servletRequest = ((ServletRequestAttributes) requestAttributes).getRequest();
Assert.state(servletRequest != null, "Could not find current HttpServletRequest");
return servletRequest;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With