拒绝 j_security_check 上的 GET 方法

发布于 2024-09-11 11:36:56 字数 120 浏览 4 评论 0原文

有没有办法只允许对 j_security_check 的 POST 请求?我想拒绝 GET。 我正在使用基于表单的安全性,并且只想允许帖子到 j_security_check。如果通过 GET 发出登录请求,则该请求应被拒绝。

Is there a way to only allow POST requests to j_security_check? I want to reject GETs.
I am using Form Based security and want to only allow Posts to j_security_check. If a login request is made via a GET, the request should be rejected.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

权谋诡计 2024-09-18 11:36:56

由于使用 GET 方法的 JAAS 的安全问题,我一直尝试在 JBOSS(Tomcat)服务器上执行相同的操作,我尝试了各种方法。

  1. 在 url 模式 /j_security_check 上使用 web.xml 安全约束以仅使用 POST - 这对于 JAAS 机制不起作用,因为它对于普通 servlet 会起作用。

  2. 将登录详细信息从登录页面传递到检查请求方法的中间 servlet,如果不是 GET,则转发到 j_security_check。 - 这不起作用,而且过于复杂。

  3. 过滤器来检查请求方法并仅调用 j_security_check 的 POST 消息 - 这不起作用,因为 JAAS 在 Web 容器中更深,并且在过滤器机制之前被调用。

  4. 创建一个 Valve,它会在 JAAS 之前被调用。

通过在调用方法中添加以下内容:

HttpServletRequest req = (HttpServletRequest) request;
if (req.getMethod().equals("GET")) {
 log.warn("Someone is trying to use a GET method to login!!");                       
 request.getRequestDispatcher("/login.jsp").forward(req, response);
 throw new ServletException("Using a GET method on security check!");
}

这确实有效。

Having been trying to do the same on a JBOSS(Tomcat) server due to security concerns of JAAS using GET methods I attempted various ways.

  1. Using a web.xml security constraint on the url pattern /j_security_check to only use POST - This doesn't work for JAAS mechanism as it would for normal servlets.

  2. Passing login details from the login page to an intermediate servlet which checked the request method and if not a GET then forwarding on to j_security_check. - This did'nt work and was over complicated.

  3. Creating a Filter that would check the request method and only invoke on a POST message to j_security_check - This didn't work as JAAS is deeper in web container and is called before the filter mechanism.

  4. Creating a Valve, which DOES get called before the JAAS.

By adding the following in the invoke method:

HttpServletRequest req = (HttpServletRequest) request;
if (req.getMethod().equals("GET")) {
 log.warn("Someone is trying to use a GET method to login!!");                       
 request.getRequestDispatcher("/login.jsp").forward(req, response);
 throw new ServletException("Using a GET method on security check!");
}

This does work.

孤檠 2024-09-18 11:36:56

是的,您可以拒绝 GET 请求。在 web.xml 文件的安全约束部分中,您可以指定允许的 http 方法。在以下 xml 中,此安全约束允许的唯一方法是 POST 方法。 j_security 检查将只允许 post 方法。

<security-constraint>
  <display-name>Your security constraint</display-name>
  <web-resource-collection>
     <web-resource-name>Your resource name</web-resource-name>
     <url-pattern>/The URL pattern</url-pattern>
     <http-method>POST</http-method>
  <web-resource-collection>
<security-constraint>

Yes you can reject the GET request. In the web.xml file in the security constraint section you can specifiy the http methods allowed. In the following xml the only method allowed for this security constraint is the POST method. j_security check will only allow the post method.

<security-constraint>
  <display-name>Your security constraint</display-name>
  <web-resource-collection>
     <web-resource-name>Your resource name</web-resource-name>
     <url-pattern>/The URL pattern</url-pattern>
     <http-method>POST</http-method>
  <web-resource-collection>
<security-constraint>
日久见人心 2024-09-18 11:36:56

您需要重新表述您的问题。

j_security 检查通常用在登录页面。

如果您请求安全资源并且未经身份验证,您将自动重定向到登录页面(假设应用程序配置为使用基于表单的安全性)。

如果您的资源不应受到 GET 请求的质询,请遵循 Doug 提到的内容。例如,如果您想保护对 myaccount(Servlet 的模式)的 POST 调用,那么仅当进行 HTTP Post 时您才会被重定向到登录页面,而即使没有用户身份验证,GET 请求也会被接受。

这意味着您希望允许经过身份验证的用户访问 POST 请求,同时允许所有人访问 GET 请求。

You would need to rephrase your question.

j_security check is typically used in the login page.

If you request a secured resource and you were not authenticated, you are automatically redirected to the login page (assuming the app is configured to use Form Based security)

If your resource should not be challenged for GET requests, follow what Doug has mentioned. For example, if you want to secure POST calls to myaccount (the pattern for a Servlet) then you would be redirected to the login page only when a HTTP Post is made while the GET request would be accepted even without a user authentication.

The implication is you want to allow authenticated users access to POST request while GET requests are permitted to everyone.

紫瑟鸿黎 2024-09-18 11:36:56

我正在考虑实施的另一种方法:

  • 在反向代理/负载平衡器(如 nginx/apache

) 中阻止除 POST 请求之外的所有 j_security_check 请求在 Apache 2.4 上,这是可行的:

<LocationMatch ".*j_security_check">
    AllowMethods POST
</LocationMatch>

An alternative approach I am considering to implement:

  • Blocking all but POST requests to j_security_check in a reverse-proxy/loadbalancer like nginx/apache

E.g. on Apache 2.4 this works:

<LocationMatch ".*j_security_check">
    AllowMethods POST
</LocationMatch>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文