简体   繁体   English

Spring Security:requires-channel="https" 导致重定向循环

[英]Spring Security: requires-channel=“https” causes redirect loop

I'm having a problem trying to get <security:intercept-url ... requires-channel="https"/> to work properly on WAS.我在尝试让<security:intercept-url ... requires-channel="https"/>在 WAS 上正常工作时遇到问题。 The application server is SSL-enabled.应用程序服务器启用了 SSL。

When I have my configuration like this:-当我有这样的配置时:-

<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>

... I can hit both http://server/myapp and https://server/myapp . ...我可以同时点击http://server/myapphttps://server/myapp In both cases, Spring Security was able to intercept this URL and present me the login page.在这两种情况下,Spring Security 都能够拦截此 URL 并向我显示登录页面。

Now, what I want to do is to redirect all http URLs to https URLs.现在,我想要做的是将所有 http URL 重定向到 https URL。 So, I added requires-channel="https" to <security:intercept-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>

... now, when I try to hit http://server/myapp , I'm seeing http://server/myapp/myapp/myapp/myapp/myapp/myapp and it goes into a redirect loop. ...现在,当我尝试点击http://server/myapp ,我看到http://server/myapp/myapp/myapp/myapp/myapp/myapp并进入重定向循环。

So, I redefined the port mappings:-所以,我重新定义了端口映射:-

<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>

... when I try to hit http://server/myapp , the URL doesn't change in the browser bar, but I still get the "redirect loop" problem. ...当我尝试点击http://server/myapp ,浏览器栏中的 URL 不会更改,但我仍然遇到“重定向循环”问题。 Even if I try to hit https://server/myapp , I still get the same problem.即使我尝试点击https://server/myapp ,我仍然遇到同样的问题。

I'm running out of ideas on how to debug this problem.我已经没有关于如何调试这个问题的想法了。 It seems like when I add requires-channel="https" , it breaks on WAS but it works just fine on Jetty.似乎当我添加requires-channel="https" ,它在 WAS 上中断,但在 Jetty 上运行良好。 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.我目前的解决方法是删除requires-channel="https"以便 https 在 WAS 上工作,但随后,用户可能会使用 http 访问该站点。

Just to throw another thing out, adding port 9080 for http and port 9443 for https doesn't fix the problem either on WAS.只是抛出另一件事,为 http 添加端口 9080 和为 https 添加端口 9443 也不能解决 WAS 上的问题。

Any ideas?有任何想法吗? Thank you for your help.感谢您的帮助。

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

I don't have a solution to the problem, but here's a workaround that fixes this:我没有解决问题的方法,但这里有一个解决方法:

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); 
        } 
    }
} 

This is platform independent, so should work with WAS as well as any other container.这是独立于平台的,因此应该与 WAS 以及任何其他容器一起使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM