我想基於IP地址來保護我的REST API(jersey2),但沒有Spring Security等的麻煩。我只需要將被授予完全訪問權限的幾個IP列入白名單。使用來自application.properties的值初始化過濾器
爲了實現這個,我想到了將IP放入application.properties並使用過濾器強制實施限制。這在使用嵌入式Jetty服務器時非常合適,但在Tomcat上將應用程序部署爲war時會失敗。
我嘗試讀取Filter構造函數和init-method中的屬性(僅在代碼片段中顯示的構造函數示例)。然而,當訪問存儲IP的類字段(String ips)時,都會導致NullPointerException。同樣使用Enviroment變量似乎沒有幫助。
任何幫助表示讚賞。謝謝!
@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)
請發佈錯誤的堆棧跟蹤。 – ChristofferPass
您如何使用「環境變量」方法,以及將您的application.properties部署爲戰爭時位於何處? – ikumen
@ user2264997 application.properties位於'src/main/resources /'下,因此也位於類路徑中。此文件還包含數據庫的憑據,似乎工作正常。關於環境變量,我指的是'org.springframework.core.env.Environment',我使用DI包含了類似'@Autowired Environment env'類的成員,並試圖使用相應的getProperties方法 – mailman