我有多域应用程序。它的意思是: 1.具有主域的主应用程序用于向世界展示它,并供客户管理其数据。2.客户通过自己的域名向世界展示他们的数据,这些域名指向主要应用
我需要能够拦截请求,并以我自己的方式处理它,如果它来自除主域之外的每个其他域。我该怎么做呢?有没有完整的解决请求拦截和那种控制器的好例子?
发布于 2017-06-30 19:30:31
为此,您可以使用filter。
下面是一个简单的例子:
@SpringBootApplication
public class So44844042Application {
public static void main(String[] args) {
SpringApplication.run(So44844042Application.class, args);
}
@Component
public static class DomainFilter extends GenericFilterBean {
private final Map<String, String> redirect = new HashMap<>();
public DomainFilter() {
redirect.put("localhost", "http://www.google.com");
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
final String domain = request.getServerName();
if (redirect.containsKey(domain)) {
((HttpServletResponse) response).sendRedirect(redirect.get(domain));
} else {
chain.doFilter(request, response);
}
}
}
@RestController
public static class HomeController {
@GetMapping
public String home() {
return "Welcome";
}
}
}运行它并尝试打开http://localhost:8080,它会将您重定向到www.google.com,但仍然可以访问http://127.0.0.1:8080。
https://stackoverflow.com/questions/44844042
复制相似问题