apache php5 用户认证,秒懂系列|Apache用户认证配置之Basic认证

很多时候我们可能需要对服务器资源进行保护,通常的做法是在应用层通过鉴权来实现,如果你嫌自己去实现鉴权太麻烦,那就直接让Apache去帮你实现吧!

Apache常见的用户认证可以分为下面三种:

– 基于IP,子网的访问控制(ACL)

– 基本用户验证(Basic Authentication)

– 消息摘要式身份验证(Digest Authentication)

基于IP的访问控制可以通过配置 Allow From实现!这里不多讲。

一般的,我们还会在IP的基础上,再增加一层 Basic Authentication,实现一个基本的服务器用户认证!

1、生成用户名密码文件

Adding password for user test

Adding password for user test2

test:$apr1$4R3foyQ5$1KGHVA5HQL8M9b0K/2UWO0

test2:$apr1$pKLy86CD$W9hFUvs4F06OBXtQhCbPV/

可以看到用户名密码文件已经生成了,一行一个!

2、配置 VirtualHost,如:

AllowOverride 表示通过配置文件进行身份验证

AuthName 发送给客户端报文头内容:WWW-Authenticate

AuthType 认证类型

AuthUserFile 这个就是刚刚生成的用户名密码文件

Require 指定哪些用户或组才能被授权访问。如:

require user test test2(只有用户 test 和 test2 可以访问)

requires groups managers (只有组 managers 中成员可以访问)

require valid-user (在 AuthUserFile 指定的文件中任何用户都可以访问)

我们来看一下效果:

在浏览器访问:

2cf389caca9a427093f23d4a9b9c7194.png

cURL请求:

* Trying 10.223.28.1…

* Connected to pma.979137.com (10.223.28.1) port 80 (#0)

> GET /test.php HTTP/1.1

> Host: pma.979137.com

> User-Agent: curl/7.43.0

> Accept: */*>

< HTTP/1.1 401 Authorization Required

< Date: Fri, 06 Jan 2017 07:02:15 GMT

< Server: Apache/2.2.27 (Unix) PHP/5.3.29

< WWW-Authenticate: Basic realm=" PMA Contents."

< Content-Length: 490

< Content-Type: text/html; charset=iso-8859-1

<

>401 Authorization Required

>Authorization Required

This server could not verify that you

are authorized to access the document

requested. Either you supplied the wrong

credentials (e.g., bad password), or your

browser doesn’t understand how to supply

the credentials required.

在没有携带用户名和密码时,HTTP Code 返回了 401,并输出了 Authorization Required!

表示需要该请求需要进行认证!

我们再来看下,携带密码请求:

* Trying 10.223.28.1…

* Connected to pma.979137.com (10.223.28.1) port 80 (#0)

* Server auth using Basic with user ‘test’

> GET /test.php HTTP/1.1

> Host: pma.979137.com

> Authorization: Basic c2hpbGlhbmd4aWU6YWl5aTEzMTQ=

> User-Agent: curl/7.43.0

> Accept: */*

>

< HTTP/1.1 200 OK

< Date: Fri, 06 Jan 2017 07:14:14 GMT

< Server: Apache/2.2.27 (Unix) PHP/5.3.29

< X-Powered-By: PHP/5.3.29

< Content-Length: 25

< Content-Type: text/html

<

string(11) "hello word!"

HTTP Code 已经是 200 了,并且返回了正确的内容!

至此,一个简单的 Basic 认证就OK了!这种认证一般可用于浏览器访问,也可以用于 API 认证!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于您的问题,我可以告诉您如何实现员工账号认证和存储在LDAP中。 为了实现员工账号认证,您可以使用一种称为基本身份验证(Basic Authentication)的方法。您可以在Apache服务器的配置文件中进行设置。首先,将以下代码添加到您的.htaccess文件中: ``` AuthType Basic AuthName "Staff Authentication" AuthUserFile /path/to/htpasswd Require valid-user ``` 然后,您需要创建一个.htpasswd文件来存储员工账号。使用以下命令创建.htpasswd文件: ``` htpasswd -c /path/to/htpasswd zsuser htpasswd /path/to/htpasswd lsuser ``` 这将为每个员工创建一个账号,并将其存储在.htpasswd文件中。 接下来,您可以通过在staff.html文件的开头添加以下代码来限制只有经过身份验证的用户才能访问该页面: ``` <?php if (!isset($_SERVER['PHP_AUTH_USER'])) { header('WWW-Authenticate: Basic realm="Staff Authentication"'); header('HTTP/1.0 401 Unauthorized'); echo 'Authorization required.'; exit; } else { $username = $_SERVER['PHP_AUTH_USER']; $password = $_SERVER['PHP_AUTH_PW']; // 验证用户名和密码是否匹配 if ($username === 'zsuser' && $password === 'password1') { // 认证成功,继续访问页面内容 } else if ($username === 'lsuser' && $password === 'password2') { // 认证成功,继续访问页面内容 } else { header('WWW-Authenticate: Basic realm="Staff Authentication"'); header('HTTP/1.0 401 Unauthorized'); echo 'Authorization required.'; exit; } } ?> ``` 请注意,这只是一个示例代码,您需要根据实际情况修改用户名和密码的验证逻辑。 关于员工账号存储在LDAP中,您可以使用PHP的LDAP扩展来连接和操作LDAP服务器。您可以使用以下代码示例连接到LDAP服务器: ***

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值