java 基于 Servlet 过滤器位置的设置

java Servlet Filter Location based settings

我的应用程序从不同的国家/地区访问,我使用通用的 servlet 过滤器 (MyFilter.java) 来控制所有请求。是否可以根据国家/地区的访问者重定向到其他 Servlet? 目前我的 web.xml 配置低于

 <filter>
    <filter-name>myfilter</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
    <filter-name>myfilter</filter-name>
    <url-pattern>*</url-pattern>
    <dispatcher>REQUEST</dispatcher>
</filter-mapping>

假设如果用户从美国访问我想将其重定向到 US_Filter.java ,或者如果用户从澳大利亚访问我想将其重定向到 AU_Filter.java ,或者如果用户从英国访问我想将其重定向到 UK_Filter.java。这可能来自 web.xml 吗?

我正在考虑在 web.xml 中进行国家/地区明智的配置,例如

country=US  (US_Filter)
country=AU  (AU_Filter)
country=UK  (UK_Filter)
country=None (MyFilter)

但是我不知道怎么办?

我需要这个是因为我们根据国家/地区执行不同的行为,例如他们的手机未验证、管理用户订阅服务等。

请给我建议。

谢谢,

http://examples.javacodegeeks.com/core-java/util/locale/java-util-locale-example/

请浏览此网站!!

使用该站点我建议您添加另一个过滤器,以便它检查语言环境并重定向到所需的 servlet。

我认为您的 web.xml 不可能用于此目的。但是,您可以通过仔细编写 MyFilter class 代码来完成您想要的,这样您就不需要在添加新国家/地区时对其进行修改。

我看到您正在使用 Spring。这是个好消息,因为 MyFilter 实际上是由 Spring 管理的 bean。这意味着可能会向其中注入其他 bean。我的建议是每个国家有一个 bean 和一个主过滤器,负责委派给正确的国家 bean。

首先,让你的web.xml保持现在的样子:

<filter>
    <filter-name>myfilter</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
    <filter-name>myfilter</filter-name>
    <url-pattern>*</url-pattern>
    <dispatcher>REQUEST</dispatcher>
</filter-mapping>

这将为每个请求调用名为 myfilter 的 bean。

然后,以与国家/地区无关的方式实施 MyFilter,以便在添加或删除国家/地区时无需修改:

@Component("myfilter")
public class MyFilter implements Filter {

    public static final String DEFAULT = "default";

    public static final String SUFFIX = "_Filter";

    // Autowire all beans that implement CountryFilter, mapped by bean name
    @Autowired
    private Map<String, CountryFilter> filters;

    @Override
    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain) throws IOException, ServletException {

        // Get country code from request
        Locale locale = request.getLocale();
        String countryCode = locale.getCountry().toUpperCase();

        // Format key to gey country-specific filter
        String key = countryCode + SUFFIX;

        // If bean doesn't exist for request country...
        if (!this.filters.containsKey(key)) {
            // ..fallback to default filter
            key = DEFAULT + SUFFIX;
        }

        // Get filter for country
        CountryFilter filter = this.filters.get(key);

        // Delegate to actual country (or default) filter
        boolean countinueChain = filter.doFilterForCountry(request, response);

        if (continueChain) {
            chain.doFilter(request, response);
        }
    }

    @Override
    public void init(FilterConfig config) throws ServletException {
    }

    @Override
    public void destroy() {
    }
}

这个class够通用了。添加或删除国家/地区时无需更改它。诀窍是对集合使用 Spring 自动装配行为。如果您自动装配 Map<String, T>,那么 Spring 将使用 class T 的所有 beans 实例填充此映射,键等于 bean 名称,值等于相应的 bean 实例.

然后,您需要 CountryFilter 接口:

public interface CountryFilter {

    boolean doFilterForCountry(ServletRequest request, ServletResponse response) 
        throws IOException, ServletException;
}

您需要为每个国家/地区实施 CountryFilter,使其成为名称与模式 CC_Filter 匹配的 Spring bean,其中 CC 代表2 位 ISO 国家/地区代码。例如,对于美国,您可能有:

@Component("US" + MyFilter.SUFFIX)
public class UsFilter implements CountryFilter {

    @Override
    public boolean doFilterForCountry(ServletRequest request, ServletResponse response) 
        throws IOException, ServletException {

        // TODO Handle US specifics here

        // VERY IMPORTANT: you might want to let the chain continue...
        return true;
        // ...or redirect to US page
        // ((HttpServletResponse) response).sendRedirect("US-url");
        // return false;
        // ONLY ONE of the options!
    }
}

对于英国:

@Component("UK" + MyFilter.SUFFIX)
public class UkFilter implements CountryFilter {

    @Override
    public boolean doFilterForCountry(ServletRequest request, ServletResponse response) 
        throws IOException, ServletException {

        // TODO Handle UK specifics here

        // VERY IMPORTANT: you might want to let the chain continue...
        return true;
        // ...or redirect to UK page
        // ((HttpServletResponse) response).sendRedirect("UK-url");
        // return false;
        // ONLY ONE of the options!
    }
}

其他国家也一样。

最后,您可能没有针对特定国家/地区的实施。在这种情况下,您可能希望使用默认过滤器作为后备情况:

@Component(MyFilter.DEFAULT + MyFilter.SUFFIX)
public class DefaultFilter implements CountryFilter {

    @Override
    public boolean doFilterForCountry(ServletRequest request, ServletResponse response) 
        throws IOException, ServletException {

        // TODO Handle DEFAULT specifics here

        // VERY IMPORTANT: you might want to let the chain continue...
        return true;
        // ...or redirect to DEFAULT page
        // ((HttpServletResponse) response).sendRedirect("DEFAULT-url");
        // return false;
        // ONLY ONE of the options!
    }
}

希望这能帮助您解决问题。我相信这是一种非常灵活的方法,它甚至有一个后备案例。要添加新国家/地区,您需要做的就是实施新的 CountryFilter.