Spring Boot - 确定一个子域(带通配符)?

时间:2017-04-19 16:44:05

标签: java spring spring-mvc spring-boot

这里是Spring Boot的新手。 Spring MVC提供了@SubdomainMapping注释,这在Spring Boot中看起来似乎没有。我见过一些人讨论使用过滤器来处理这个问题。或者其他似乎过于复杂的方法。

是否没有(简单/清洁)方法来处理标准控制器中的所有子域,例如:

@SubdomainMapping(value = {"**"}
public String data(ModelMap modelMap, HttpServletRequest request) {

//Code to handles subdomain logic here ....

}

这将是一个简单的方法,其中所有值都被平等对待,但有微小差异。

任何建议都会有所帮助!

1 个答案:

答案 0 :(得分:0)

我自己也做过这方面的工作,我的回答并不是你想要的那么简单,但我认为没有一个简单。

因此,您可以创建一个处理程序拦截器适配器,它将在到达您的控制器之前获取每个请求并抓取并处理子域。这需要这样的事情:

@Component
public class SubDomainInterceptor extends HandlerInterceptorAdapter {


    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object object, Exception arg3)
            throws Exception {

    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object object, ModelAndView model)
            throws Exception {

    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object object) throws Exception {
        String mysub = request.getRequestURL().toString();
        //...
        // Do whatever you need to do with the subdomain
        //
        if (isGoodSubdomain){
             session.sendAttribute("subdomain", mysub);
        } else {
             response.sendRedirect("http://www.basesite.com"):
        }
        return true;
    }

然后,您可以在控制器中使用该会话变量来过滤值或您需要使用它们的任何内容。我知道这不是你想要的简单答案,但它是迄今为止我发现的最好的答案。