使用基本身份验证 (htaccess) 限制对特定 URL 的访问

发布于 2024-10-25 20:34:34 字数 359 浏览 2 评论 0原文

我需要限制对特定 URL 的访问,例如 http://mydomain.com/this/is/我的网络服务器上的 /url 通过 Apache 使用基本身份验证。任何其他 URL 都应该可以公开访问。我已经看到您可以使用以下命令向文件添加特定规则:

<Files "mypage.html">
  Require valid-user
</Files>

我的问题是所有请求都使用 mod-rewrite 路由到控制器,因此我认为我无法根据文件限制访问。任何想法都会非常有帮助!

I need to restrict access to a particular URL, e.g. http://mydomain.com/this/is/the/url on my webserver using Basic Authentication through Apache. Any other URL should be openly accessible. I have seen that you can add specific rules to files using:

<Files "mypage.html">
  Require valid-user
</Files>

My problem is that all requests are routed to controllers using mod-rewrite and so I don't think that I can restrict access based on the file. Any ideas would be most helpful!

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

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

发布评论

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

评论(2

夜访吸血鬼 2024-11-01 20:34:36

在 .htacess 文件中,您应该输入:

AuthType Basic
AuthName "Need to login"
AuthUserFile .htpasswd file location ;
Require user USER

//AuthName is login prompt message
//AuthUserFile  is physical .htpasswd file location i.e.
C:/xampp/htdocs/basic/.htpasswd
//Require user is for a specific user i.e. the username you want to
authenticate

要生成 .htpasswd 文件,您可以使用:
- http://www.htaccesstools.com/htpasswd-generator/

In .htacess file you should put :

AuthType Basic
AuthName "Need to login"
AuthUserFile .htpasswd file location ;
Require user USER

//AuthName is login prompt message
//AuthUserFile  is physical .htpasswd file location i.e.
C:/xampp/htdocs/basic/.htpasswd
//Require user is for a specific user i.e. the username you want to
authenticate

To generate .htpasswd file you can use :
- http://www.htaccesstools.com/htpasswd-generator/

伤痕我心 2024-11-01 20:34:36

我不确定这是否有效/有帮助,但您可以在应用程序 web.xml 中指定某些内容。

  <security-constraint>
    <display-name>Public access</display-name>
    <web-resource-collection>
      <web-resource-name>PublicPages</web-resource-name>
      <description>Public</description>
      <url-pattern>/servlet/*</url-pattern>
    </web-resource-collection>
    <user-data-constraint>
      <transport-guarantee>NONE</transport-guarantee>
    </user-data-constraint>
  </security-constraint>
  <security-constraint>
    <display-name>Secured access</display-name>
    <web-resource-collection>
      <web-resource-name>SecuredPages</web-resource-name>
      <description>Secured pages</description>
      <url-pattern>/services/*</url-pattern>
    </web-resource-collection>
    <auth-constraint>
      <description>General Access</description>
      <role-name>*</role-name>
    </auth-constraint>
    <user-data-constraint>
      <description>SSL not required</description>
      <transport-guarantee>NONE</transport-guarantee>
    </user-data-constraint>
  </security-constraint>
  <login-config>
    <auth-method>BASIC</auth-method>
    <realm-name>SecurePages</realm-name>
  </login-config>
  <security-role>
    <description>General Access</description>
    <role-name>*</role-name>
  </security-role>

I'm not sure if this would work/help, but you could specify something in your application web.xml.

  <security-constraint>
    <display-name>Public access</display-name>
    <web-resource-collection>
      <web-resource-name>PublicPages</web-resource-name>
      <description>Public</description>
      <url-pattern>/servlet/*</url-pattern>
    </web-resource-collection>
    <user-data-constraint>
      <transport-guarantee>NONE</transport-guarantee>
    </user-data-constraint>
  </security-constraint>
  <security-constraint>
    <display-name>Secured access</display-name>
    <web-resource-collection>
      <web-resource-name>SecuredPages</web-resource-name>
      <description>Secured pages</description>
      <url-pattern>/services/*</url-pattern>
    </web-resource-collection>
    <auth-constraint>
      <description>General Access</description>
      <role-name>*</role-name>
    </auth-constraint>
    <user-data-constraint>
      <description>SSL not required</description>
      <transport-guarantee>NONE</transport-guarantee>
    </user-data-constraint>
  </security-constraint>
  <login-config>
    <auth-method>BASIC</auth-method>
    <realm-name>SecurePages</realm-name>
  </login-config>
  <security-role>
    <description>General Access</description>
    <role-name>*</role-name>
  </security-role>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文