【问题标题】:Initializing filter with values from application.properties使用 application.properties 中的值初始化过滤器
【发布时间】:2015-07-15 12:17:46
【问题描述】:

我想根据 IP 地址保护我的 REST API (jersey2),但又不想遇到 Spring Security 等问题。我只需要将一些被授予完全访问权限的 IP 列入白名单。

为了实现这一点,我考虑将 IP 放入 application.properties 并使用过滤器强制执行限制。这在使用嵌入式 Jetty 服务器时工作得非常好,但在将应用程序部署为 Tomcat 上的战争时失败。

我尝试在 Filter 构造函数和 init-method 中读取属性(仅在 sn-p 中显示的构造函数示例)。然而,在访问存储 IP 的类字段(String ips)时,两者都会导致 NullPointerException。也使用环境变量似乎没有帮助。

感谢任何帮助。谢谢!

@Component("RestAuthFilter")
public class RestAuthFilter implements Filter {

    private String ips;

    public RestAuthFilter() {
        try {
            final Properties p;
            final InputStream input = Thread.currentThread().getContextClassLoader().getResourceAsStream("application.properties");
            p = new Properties();
            p.load(input);
            ips = p.getProperty("whitelist.rest.ips");
        } catch(IOException e) {
            ips = "127.0.0.1";
        }
    }

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

    @Override
    public void doFilter(final ServletRequest req, final ServletResponse res,
                     final FilterChain chain) throws ServletException,     IOException {

        final List<String> allowedIPs = Arrays.asList(ips.split("[,]"));

        if(!allowedIPs.contains(req.getRemoteAddr())) {
            ((HttpServletResponse) res).setStatus(HttpServletResponse.SC_FORBIDDEN, "Not allowed to use REST API!");
        } else {
            chain.doFilter(req, res);
       }
    }

    @Override
   public void destroy() {
   }

}

堆栈跟踪:

java.lang.NullPointerException: null
    at com.example.RestAuthFilter.doFilter(RestAuthFilter.java:44)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:85)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.springframework.boot.context.web.ErrorPageFilter.doFilter(ErrorPageFilter.java:113)
    at org.springframework.boot.context.web.ErrorPageFilter.access$000(ErrorPageFilter.java:59)
    at org.springframework.boot.context.web.ErrorPageFilter$1.doFilterInternal(ErrorPageFilter.java:88)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.springframework.boot.context.web.ErrorPageFilter.doFilter(ErrorPageFilter.java:106)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:313)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:745)

【问题讨论】:

  • 请发布错误的堆栈跟踪。
  • 你是如何使用“环境变量”方法的,当部署为war时你的application.properties在哪里?
  • @user2264997 application.properties 位于src/main/resources/ 下,因此也在类路径中。该文件还包含数据库的凭据,并且似乎可以正常工作。关于环境变量,我指的是 org.springframework.core.env.Environment,我使用 DI 将其包括在内,例如 @Autowired Environment env 作为类成员,我尝试使用相应的 getProperties-method
  • 在 WAR 中当然是WEB-INF/classes ...
  • 你使用 Maven 创建 WAR 文件吗?

标签: java tomcat spring-boot servlet-filters jersey-2.0


【解决方案1】:

由于您使用的是 Spring Boot 并且您的设置非常标准,因此我会坚持使用 PropertySource(通过 @Value 或 Environment)从 application.properties 加载 IP。话虽如此,并注意到您对使用环境的评论......

关于我所指的环境变量 org.springframework.core.env.Environment,我使用 DI 包括在内, 类似于@Autowired Environment env 作为类成员

你不能将它作为类成员注入并在构造函数中进行初始化,你会遇到范围问题——调用构造函数时不会设置环境。您需要通过构造函数注入环境,而不是作为类成员,例如:

@Component("RestAuthFilter")
public class RestAuthFilter implements Filter {

  private final List<String> restClientIps;

  @Autowired
  public RestAuthFilter(Environment env) {
    String restClientIpsProperty = env.getRequiredProperty("whitelist.rest.ips");
    restClientIps = // parse/split from restClientIpsProperty
  }
  ...
}

【讨论】:

    猜你喜欢
    • 2016-06-14
    • 1970-01-01
    • 2011-12-17
    • 1970-01-01
    • 1970-01-01
    • 2014-07-26
    • 2018-08-31
    • 2013-10-03
    • 1970-01-01
    相关资源
    最近更新 更多