有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

JavaSpring安全性:requireChannel=“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就可以工作了,但是用户可以使用http访问该站点

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

有什么想法吗?谢谢你的帮助


共 (1) 个答案

  1. # 1 楼答案

    My current workaround is to remove requires-channel="https" so that https work on WAS but then, the users may come to the site using 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以及任何其他容器一起使用