gpt4 book ai didi

java - Spring 安全 : requires-channel ="https" causes redirect loop

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:02:30 29 4
gpt4 key购买 nike

我在尝试获取 <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 上的问题。

有什么想法吗?感谢您的帮助。

最佳答案

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 以及任何其他容器一起工作。

关于java - Spring 安全 : requires-channel ="https" causes redirect loop,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24022125/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com