Spring Security: IP Address Whitelist Before Deferring to HTTP Basic Auth

只愿长相守 提交于 2019-12-22 06:07:05

问题


I have a single URL accessible through a servlet that I have locked down using Spring Security's DaoAuthenticationProvider. I now have the requirement that certain incoming IP addresses must be whitelisted and so are not requested to authenticate.

I can hack around this easily enough by overriding DaoAuthenticationProvider's authenticate method and bypassing the superclasses's implementation if the IP address matches a known IP address but this then only works when the sender of the request supplies a username and password (even if it's nonsense). Otherwise the provider doesn't get called.

What would be the best way to do this? Should I be using a filter to bypass the authentication procedure if a known IP address is incoming?


回答1:


I think the idiomatic Spring Security way to do it is to implement a pre-authentication filter that would populate security context with a valid Authentication object when client is in the whitelist. You can implement such a filter from scratch (for example, as here) or use AbstractPreAuthenticatedProcessingFilter (though it seems to be overcomplicated for your task).




回答2:


Could you just use the hasIpAddress() expression? We're doing that for what appears to be a similar case.

<security:intercept-url pattern="/services/**" access="hasIpAddress('192.168.1.0/24')"/>


来源:https://stackoverflow.com/questions/5142690/spring-security-ip-address-whitelist-before-deferring-to-http-basic-auth

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!