如何在没有Spring Boot的情况下使用Spring Security设置ForwardedHeaderFilter



我希望在spring安全中设置ForwardedHeaderFilter,这样我就可以让spring知道登录后要使用哪个协议。我在负载均衡器后面有几个应用程序服务器(使用ssl终止(,spring安全性使用http(而不是https(重定向用户。正因为如此,我的用户现在收到了一条引人注目的警告消息。我能在网上找到的唯一例子是我没有实现的弹簧引导。

我想用";addFilterBefore((&;方法,但从未调用过筛选器。

有什么想法吗?

// Apply sameOrigin policy for iframe embeddings
http.headers().frameOptions().sameOrigin();
// ********* Add filter here?  *******
http.addFilterBefore(new ForwardedHeaderFilter(), ChannelProcessingFilter.class);
// Authorization filters
http.authorizeRequests().antMatchers("/sysAdmin/**", "/monitoring/**").access("isFullyAuthenticated() and hasRole('GOD')");
http.authorizeRequests().antMatchers("/app/**").authenticated();
http.authorizeRequests().antMatchers("/**").permitAll();
http.formLogin()
.loginPage("/public/login.jsp")
.loginProcessingUrl("/login")
.usernameParameter("username")
.passwordParameter("password")
.defaultSuccessUrl("/app/Dashboard.action", false)
.failureHandler(customAuthenticationFailureHandler());
// Disable so that logout "get" url works (otherwise you have to do a html form)
http.csrf().disable();
http.logout().logoutSuccessUrl("/public/login.jsp");
http.sessionManagement()
.invalidSessionUrl("/public/expiredSession.jsp?expiredId=2")
.maximumSessions(2)
.sessionRegistry(sessionRegistry())
.expiredUrl("/public/expiredSession.jsp?expiredId=3");

我最终添加了这样的过滤器,一切似乎都能正常工作

// Added for load balancer headers (X-Forwarded-For, X-Forwarded-Proto, etc)
http.addFilterBefore(new ForwardedHeaderFilter(), WebAsyncManagerIntegrationFilter.class);

最新更新