Spring 安全性:需求通道 = "https"导致重定向循环



我在试图让<security:intercept-url ... requires-channel="https"/>在WAS上正常工作时遇到了问题。

应用服务器启用ssl。

当我有这样的配置:-

<security:http auto-config="true">
    <security:form-login .../>
    <security:logout .../>
    <security:intercept-url pattern="/admin/**" access="ROLE_ADMIN" />
    <security:intercept-url pattern="/**" access="ROLE_ADMIN,ROLE_USER" />
</security:http>

…我可以同时得到http://server/myapphttps://server/myapp。在这两种情况下,Spring Security都能够拦截这个URL并显示登录页面。

现在,我要做的是将所有http url重定向到https url。因此,我将requires-channel="https"添加到<security:intercept-url />

<security:http auto-config="true">
    <security:form-login .../>
    <security:logout .../>
    <security:intercept-url pattern="/admin/**" access="ROLE_ADMIN" requires-channel="https" />
    <security:intercept-url pattern="/**" access="ROLE_ADMIN,ROLE_USER" requires-channel="https" />
</security:http>

…现在,当我尝试点击http://server/myapp时,我看到的是http://server/myapp/myapp/myapp/myapp/myapp/myapp,它进入了一个重定向循环。

所以,我重新定义了端口映射:-
<security:http auto-config="true">
    <security:form-login .../>
    <security:logout .../>
    <security:intercept-url pattern="/admin/**" access="ROLE_ADMIN" requires-channel="https" />
    <security:intercept-url pattern="/**" access="ROLE_ADMIN,ROLE_USER" requires-channel="https" />
    <security:port-mappings>
        <security:port-mapping http="80" https="443"/>
    </security:port-mappings>
</security:http>

…当我尝试点击http://server/myapp时,URL在浏览器栏中没有改变,但我仍然得到"重定向循环"问题。即使我尝试点击https://server/myapp,我仍然得到同样的问题。

我不知道如何调试这个问题。似乎当我添加requires-channel="https"时,它在WAS上中断,但在Jetty上工作得很好。我目前的解决方法是删除requires-channel="https",以便https在WAS上工作,但随后,用户可能会使用http来访问网站。

只是抛出另一件事,为http添加端口9080和为https添加端口9443也不能解决WAS上的问题。

任何想法?谢谢你的帮助。

我目前的解决方法是删除requires-channel="https",以便https可以在WAS上工作,但随后,用户可能会使用http访问站点。

我没有解决这个问题的方法,但是这里有一个解决这个问题的方法:

import java.io.IOException;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
import org.springframework.stereotype.Component;     
import org.springframework.web.filter.OncePerRequestFilter; 
@Component
public class UnsecureRequestFilter extends OncePerRequestFilter { 
    @Override 
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) 
                    throws ServletException, IOException { 
        if (!request.isSecure()) {
            response.sendRedirect("https://domain.example.com/");
        } else { 
            filterChain.doFilter(request, response); 
        } 
    }
} 

这是平台无关的,所以应该与WAS以及任何其他容器一起工作。

最新更新