【问题标题】:Using Basic Authentication (htaccess) to restrict access to a specific URL使用基本身份验证 (htaccess) 限制对特定 URL 的访问
【发布时间】:2023-03-20 21:30:02
【问题描述】:
我需要限制对特定 URL 的访问,例如http://mydomain.com/this/is/the/url 在我的网络服务器上使用通过 Apache 的基本身份验证。任何其他 URL 都应该是可公开访问的。我已经看到您可以使用以下方法向文件添加特定规则:
<Files "mypage.html">
Require valid-user
</Files>
我的问题是所有请求都使用 mod-rewrite 路由到控制器,所以我认为我不能根据文件限制访问。任何想法都会很有帮助!
【问题讨论】:
标签:
apache
.htaccess
basic-authentication
【解决方案1】:
在 .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/
【解决方案2】:
我不确定这是否可行/有帮助,但您可以在应用程序 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>