基于浏览器用户代理重定向的正确方法?

发布于 2024-12-10 00:59:35 字数 913 浏览 0 评论 0原文

我正在构建一个基于 Spring MVC 的应用程序,我想根据用户的浏览器将用户重定向到网站的特定部分。

我使用应用于 /site/home.jsp 的过滤器来读取用户代理以确定浏览器类型。

HttpServletRequest req = (HttpServletRequest) request;
String uaString = req.getHeader("User-Agent");

此外,我想重定向用户如下:

  • Firefox:重定向到 /site/firefox/home.jsp
  • IE:重定向到 /site/ie/home.jsp
  • 未知:重定向到 /site/UnsupportedBrowser.jsp

我的困惑是什么是正确的如何从我的 BrowserDetector 过滤器重定向用户?

1)简单地重定向用户?

resp.sendRedirect("/AppName/site/ie/home.jsp");

2) 使用 HTTP 临时重定向?

resp.setStatus(HttpServletResponse.SC_TEMPORARY_REDIRECT);
resp.setHeader("Location", "/AppName/site/ie/home.jsp");

3)服务器端重定向?

RequestDispatcher request_Dispatcher=request.getRequestDispatcher("/ie/home.jsp");
request_Dispatcher.forward(request,response);

4)还有其他正确的方法吗?

I am building a Spring MVC based application where I want to redirect user's to specific portion of the site based on their browser.

I am using a Filter applied to /site/home.jsp to read the User-Agent to determine the browser type.

HttpServletRequest req = (HttpServletRequest) request;
String uaString = req.getHeader("User-Agent");

Further I want to redirect users as below:

  • Firefox: redirect to /site/firefox/home.jsp
  • IE: redirect to /site/ie/home.jsp
  • Unknown: redirect to /site/UnsupportedBrowser.jsp

My confusion is what is the correct way to redirect the user from my BrowserDetector filter?

1) Simply redirect the user?

resp.sendRedirect("/AppName/site/ie/home.jsp");

2) Use an HTTP Temp redirect?

resp.setStatus(HttpServletResponse.SC_TEMPORARY_REDIRECT);
resp.setHeader("Location", "/AppName/site/ie/home.jsp");

3) Server side redirect?

RequestDispatcher request_Dispatcher=request.getRequestDispatcher("/ie/home.jsp");
request_Dispatcher.forward(request,response);

4) Any other correct way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

落在眉间の轻吻 2024-12-17 00:59:35

第三个想法可能是一个坏主意,因为它很可能会导致您必须对每个请求执行此检查,这是低效的(只是轻微的,但小的低效率可能会增加)。

想法的第 1 号和第 1 号2 也不是最好的方法,因为两者都会导致临时重定向 (307) 响应,而您可能想要的是永久重定向 (301)。这是因为有问题的浏览器始终是相同的 - FF 和 IE 从不共享永久移动列表,因此即使两个浏览器由同一客户端计算机使用,也不会导致问题。您应该使用 301 再次考虑到效率 - 如果浏览器总是直接转到正确的位置,那么您的服务器要做的工作就会减少。

总而言之,我认为想法 2 是最接近的,但您应该使用它:

resp.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
resp.setHeader("Location", "/AppName/site/ie/home.jsp");

请记住,用户代理字符串可能会被欺骗,并且不能 100% 依赖。

这是我个人的看法,YMMV...

Idea number 3 is probably a bad idea, as it will most likely result in you having to perform this check for every request, which is inefficient (only slightly, but small inefficiencies can mount up).

Idea's number 1 & 2 are also not the best approach, because both will result in a Temporary redirect (307) response, wheras what you probably want is a permanent redirect (301). This is because the browser in question will always be the same - FF and IE never share there list of permanent moves, so even if both browsers are used by the same client machine this will not cause a problem. You should use 301 for reasons of, again, efficiency - if the browser always goes directly to the correct place, it is less work for your server to do.

To sum up, I think idea 2 is the closest, but you should use this instead:

resp.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
resp.setHeader("Location", "/AppName/site/ie/home.jsp");

Keep in mind that User-Agent strings can be spoofed and cannot be 100% relied upon.

This is my personal opinion, YMMV...

溺渁∝ 2024-12-17 00:59:35

首先也是最重要的一点:不要进行服务器端浏览器嗅探!

由于一堆不同的原因,服务器端浏览器嗅探是一个坏主意,但这只是其中的几个:

  1. 数百种不同的浏览器,而且这个数字一直在变化,因此检测所有浏览器是一场你无法赢得
  2. 搜索引擎的竞赛真的不喜欢当你给他们提供与真实浏览器不同的东西时(他们确实测试了它)
  3. 它不能可靠地告诉你用户浏览器的功能,因为他们可能欺骗了他们的用户代理字符串(出于各种原因) )或者可能安装了其他东西来增加或减少其指定浏览器本身具有的功能

更好的方法是使用渐进增强优雅降级

话虽这么说,一种更可靠的选择是在浏览器中进行嗅探,然后相应地将它们重定向到客户端。 Modernizr 将告诉您他们的浏览器具有哪些功能,然后您可以决定为他们提供哪些服务。

First and foremost: DON'T DO SERVER-SIDE BROWSER SNIFFING!

Server-side browser sniffing is a bad idea for a bunch of different reasons, but here's just a few of them:

  1. There are hundreds of different browsers and that number changes all the time, so detecting them all is a race you can't win
  2. Search engines really don't like it when you give them different stuff than you give to real browsers (and they do test it)
  3. It doesn't reliably tell you the capabilities of the user's browser because they may have spoofed their user agent string (for various reasons) or may have other things installed that increase or decrease the capabilities their indicated browser has natively

A much better approach is to use Progressive Enhancement or Graceful Degradation.

That being said, one option that works more reliably is to do the sniffing in the browser and then redirect them client-side accordingly. Modernizr will tell you what capabilities their browser has, and from there you can decide what to serve them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文