简体   繁体   English

对.htaccess文件中的不同URI使用基本身份验证

[英]Use basic auth for different URIs in a .htaccess file

My problem is the following. 我的问题如下。 I have several URIs that I need to put behind a basic authentication, preferably using the .htaccess file. 我有一些需要进行基本身份验证的URI,最好使用.htaccess文件。 I have done this using the Location directive in the httpd.conf file, but this is far from ideal. 我已经使用httpd.conf文件中的Location指令完成了此操作,但这远非理想。

So my ideal world would be this: 所以我的理想世界是这样的:

URI1 => user1, pwd1 URI1 =>用户1,pwd1

URI2 -> user2, pwd2 URI2-> user2,pwd2

URI3 -> no auth URI3->无认证

URI4 -> user3, pwd3 URI4-> user3,pwd3

I've seen some solutions using an env variable, but it seems to me that those can only be of binary use, either you get authenticated by a general htpasswd file, or you don't. 我已经看到一些使用env变量的解决方案,但是在我看来,这些解决方案只能是二进制的,或者通过常规的htpasswd文件进行身份验证,否则就不能。 My solution would have to have different users per different URI. 我的解决方案必须针对每个不同的URI具有不同的用户。

If my problem isn't thoroughly explained let me know. 如果我的问题没有得到充分解释,请告诉我。

Instead of using the <Location> container, you can use the <Files> container instead to match a specific file that is inside the directory that the request is for . 代替使用<Location>容器,可以使用<Files>容器来匹配请求所针对的目录内的特定文件。 It'll work the same way. 它将以相同的方式工作。

For example, if you have a directory "foo" and "bar", and inside those directories, you have files "type1.php" and "type2.php", then you'd have an htaccess file in "foo" that looks like: 例如,如果您有一个目录“ foo”和“ bar”,并且在这些目录中,则有文件“ type1.php”和“ type2.php”,那么您在“ foo”中会有一个htaccess文件,该文件看起来喜欢:

<Files "type1.php">
# htpasswd stuff
Require user user1
</Files>

<Files "type2.php">
# htpasswd stuff
Require user user2
</Files>

then in the "bar" directory: 然后在“酒吧”目录:

<Files "type1.php">
# htpasswd stuff
Require user user3
</Files>

<Files "type2.php">
# htpasswd stuff
Require user user4
</Files>

You just need to put a .htaccess file where each different URI is so... 您只需要将.htaccess文件放在每个不同的URI所在的位置...

AuthUserFile /path/to/file/.htpasswd
AuthName "Password Protected"
AuthType Basic
Require valid-user

Then inside the .htpasswd file you have: 然后在.htpasswd文件中,您具有:

user:password

Obviously you would want to encrypt the password 显然,您希望对密码进行加密

I think this is what you are asking for? 我认为这就是您要的?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM