关于 java:IP 过滤器使用 Spring Security
IP filter using Spring Security
我想知道如何使用 Spring Security 通过 IP 过滤用户对我的 Web 应用程序的访问。
我应该扩展
如果是这样,您能否在
提前致谢。
附言在我的应用程序中,我也有 Spring Security 支持(使用默认
相关讨论
- stackoverflow.com/questions/10147161/的可能重复项...
您可以这样做的一种方法是使用 Spring Security 的 Web 安全表达式。例如:
|
1
2 3 4 5 |
<http use-expressions="true">
<intercept-url pattern="/admin*" access="hasRole('admin') and hasIpAddress('192.168.1.0/24')"/> ... </http> |
相关讨论
- 工作得很好。只要确保你有 use-expressions="true" ,因为它是 web 安全表达式工作所必需的。虽然解决方案确实有,但它没有明确说这是必要的(这对我有帮助;)
- 然而,这不是一个在实际场景中运行良好的解决方案,因为它被硬编码到 XML 文件中并且需要重新启动应用服务器。
Anshu 的回答是通过 ip 对用户进行身份验证是一个好主意,但它可能不适用于 cas 身份验证。我有另一种解决方案,使用过滤器更适合这种情况。
|
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
public class IPAuthenticationFilter extends AbstractAuthenticationProcessingFilter {
private AuthenticationUserDetailsService<CasAssertionAuthenticationToken> authenticationUserDetailsService; private static Set<String> ipWhitelist; @Autowired @PostConstruct public IPAuthenticationFilter() { public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) CasAuthenticationToken result = new CasAuthenticationToken( protected boolean requiresAuthentication(HttpServletRequest request, HttpServletResponse response) { protected void successfulAuthentication( public AuthenticationUserDetailsService<CasAssertionAuthenticationToken> getAuthenticationUserDetailsService() { public void setAuthenticationUserDetailsService( |
你可以像这样在 cas 之前添加这个过滤器:
|
1
|
http.addFilterBefore(ipAuthenticationFilter(), CasAuthenticationFilter.class)
|