分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 前端开发

apache用户认证

发布时间:2023-09-06 01:31责任编辑:苏小强关键词:apache
1.apache用户认证

当用户访问某些页面的时候,可以设置用户密码的路后显示内容。但是为了更进一步提升安全性,可以将页面本身加密,让部分制定用户输入账号密码后才显示页面本身的内容。

在虚拟主机配置文件/usr/local/apache2.4/conf/extra/httpd-vhosts.conf中已经配置好了一台主机名为111.com的虚拟主机。在浏览器中输入该主机名可以得到如下页面:

①将虚拟主机配置文件/usr/local/apache2.4/conf/extra/httpd-vhosts.conf配置如下:

<VirtualHost *:80>    DocumentRoot "/data/wwwroot/111.com"    ServerName 111.com    ErrorLog "logs/111.com-error_log"    CustomLog "logs/111.com-access_log" common    <Directory /data/wwwroot/111.com>        AllowOverride AuthConfig        AuthName "111.com user auth"        AuthType Basic        AuthUserFile /data/.htpasswd        require valid-user    </Directory></VirtualHost>

说明:

<Directory /data/wwwroot/www.123.com>  ? 指定认证的目录

        AllowOverride AuthConfig  ? 这个相当于打开认证的开关

        AuthName "123.com user auth"  ? 自定义认证的名

        AuthType Basic  ? 认证的类型,一般为Basic

        AuthUserFile /data/.htpasswd   ? 指定密码文件所在位置

        require valid-user  ? 指定需要认证的用户为全部可用用户

</Directory>

②创建认证用户

-c:创建,只有第一次创建文件的时候需要加上该选项

-m:以MD5方式对密码加密

[root@test_01 ~]# /usr/local/apache2.4/bin/htpasswd -cm /data/.htpasswd wennanNew password:Re-type new password:Adding password for user wennan[root@test_01 ~]# cat /data/.htpasswdwennan:$apr1$GxAVVmNA$fb12xm6BUmPR4N1Ro2S2O/

③检查配置文件,重新启动服务

[root@test_01 ~]# /usr/local/apache2.4/bin/apachectl -tSyntax OK[root@test_01 ~]# /usr/local/apache2.4/bin/apachectl graceful

④测试

使用curl测试,得到401的错误代码,表示需要经过用户认证。

[root@test_01 ~]# curl -x127.0.0.1:80 111.com -IHTTP/1.1 401 UnauthorizedDate: Thu, 21 Dec 2017 12:02:16 GMTServer: Apache/2.4.28 (Unix) PHP/5.6.30WWW-Authenticate: Basic realm="111.com user auth"Content-Type: text/html; charset=iso-8859-1

使用curl命令的-u选项输入用户密码则得到了200的反馈代码。

[root@test_01 ~]# curl -x192.168.231.128:80 -uwennan:P@ssw0rd 111.com -IHTTP/1.1 200 OKDate: Thu, 21 Dec 2017 12:43:25 GMTServer: Apache/2.4.28 (Unix) PHP/5.6.30X-Powered-By: PHP/5.6.30Content-Type: text/html; charset=UTF-8

2.对个别文件进行加密

除了针对整个页面内容进行加密之外,也可以针对某些关联的文件进行加密。

①将虚拟主机配置文件/usr/local/apache2.4/conf/extra/httpd-vhosts.conf配置如下,只针对123.php文件进行加密

<VirtualHost *:80>    DocumentRoot "/data/wwwroot/111.com"    ServerName 111.com    ErrorLog "logs/111.com-error_log"    CustomLog "logs/111.com-access_log" common    #<Directory /data/wwwroot/111.com>    #    AllowOverride AuthConfig    #    AuthName "111.com user auth"    #    AuthType Basic    #    AuthUserFile /data/.htpasswd    #    require valid-user    #</Directory>    <FilesMatch 123.php>         AllowOverride AuthConfig         AuthName "111.com user auth"         AuthType Basic         AuthUserFile /data/.htpasswd         require valid-user    </FilesMatch></VirtualHost>

②测试

[root@test_01 ~]# curl -x127.0.0.1:80 111.com/123.php -IHTTP/1.1 401 UnauthorizedDate: Thu, 21 Dec 2017 12:53:21 GMTServer: Apache/2.4.28 (Unix) PHP/5.6.30WWW-Authenticate: Basic realm="111.com user auth"Content-Type: text/html; charset=iso-8859-1[root@test_01 ~]# curl -x192.168.231.128:80 -uwennan:P@ssw0rd 111.com/123.php -IHTTP/1.1 200 OKDate: Thu, 21 Dec 2017 12:53:36 GMTServer: Apache/2.4.28 (Unix) PHP/5.6.30X-Powered-By: PHP/5.6.30Content-Type: text/html; charset=UTF-8[root@test_01 ~]# curl -x192.168.231.128:80 -uwennan:P@ssw0rd 111.com/123.php123.php authorization passed



apache用户认证

原文地址:http://blog.51cto.com/lavender7n/2053089

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved