gpt4 book ai didi

java - 在 Struts 2 spring 应用程序中添加安全性

转载 作者:行者123 更新时间:2023-11-29 08:58:36 24 4
gpt4 key购买 nike

我正在使用 spring 3 struts 2 和 hibernate 开发一个应用程序。登录后我只需要显示页面

它工作正常。当我测试时我发现了一个大错误

那是我复制了只需要显示给登录用户的页面的url并将其粘贴到其他浏览器中意味着它显示的是无需登录的页面。

 <%
String userId= (String)session.getAttribute("userId");
System.out.println(userId);

if(userId == null || userId.equals("") ){
response.sendRedirect("login.jsp");
}

%>

我已经为所有 jsp 包含了这个。我知道这不是最佳做法。有没有更好的选择?

我该如何克服这个错误?

最佳答案

if(userId == null || userId.equals("") ){
response.sendRedirect("login.jsp");
}

可能应该有一个 return 以防止呈现页面内容:

if(userId == null || userId.equals("") ){
response.sendRedirect("login.jsp");
return;
}

javadoc 中没有任何内容建议 sendRedirect 导致突然退出或导致响应主体未发送到客户端。

可能发生的情况是您的响应包含重定向 header ,但也包含您可能不打算发送的页面内容。

关于java - 在 Struts 2 spring 应用程序中添加安全性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18863963/

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