给Squid代理添加HTTP basic认证

HTTP basic认证是一种简单的认证机制,要求用户在请求资源前提供有效的用户名和密码。

实例: 给Squid代理添加HTTP basic认证

要求: 只允许用户名为peter,密码为123的请求通过认证, 其他请求返回407(Proxy认证失败)

步骤

1 使用htpasswd工具,生成用户名密码。 例如这里添加用户名peter, 密码123.

复制代码
yum install httpd-tools
htpasswd -c /etc/squid/squid_user peter
New password: 123
Re-type new password: 123
Adding password for user peter

检查密码文件/etc/squid/squid_user,可以找到刚才添加的用户peter

复制代码
cat /etc/squid/squid_user
peter:$XXXXXXXXXXXXXXXXXXX

对密码文件设置适当权限

复制代码
chown squid /etc/squid/squid_user

验证用户名和密码是否正确, 执行basic_ncsa_auth程序,输入peter 123,显示OK说明正确。

复制代码
/usr/lib64/squid/basic_ncsa_auth /etc/squid/squid_user 
peter 123
OK

2 修改squid配置文件/etc/squid/squid.conf,添加认证相关的配置

复制代码
# Insert your own rules here to allow access from your clients

# http_access allow localhost 加注释,表示localhost也需要认证

auth_param basic program /usr/lib64/squid/basic_ncsa_auth /etc/squid/squid_user
auth_param basic children 5
auth_param basic realm Proxy Authentication Required
auth_param basic credentialsttl 2 hours
auth_param basic casesensitive on

acl authUsers proxy_auth REQUIRED
http_access allow authUsers

http_access deny all

修改配置完成后,重启Squid (systemctl restart squid)

验证

使用curl测试Squid用户名密码认证配置

不使用用户名密码认证,访问失败,返回407

使用正确的用户名密码认证,访问成功

通过编写Auth程序,自定义灵活的认证策略

最基础的basic认证方式,不一定能满足实际使用需求,考虑如下需求:

对于请求的USER, 用SHA1算法加密, 明文为(USER + 一个固定key), 如果请求的PASSWORD和加密后的字符串一致,就认为认证通过,否则不通过。 如何实现这种自定义认证?

复制代码
- Username: GUID
- Password: Digest = SHA1 ( GUID + KEY )

Squid支持自定义认证,只需要编写自定义认证程序,再修改Squid配置项auth_param即可,方法如下:

编写自定义认证程序

写一个Python脚本, 主流程是一个死循环, 每次从标准输入读取请求中的user,passwd, 再通过SHA1算法判断密码是否正确。 如密码正确输出OK, 否则输出ERR

python 复制代码
#!/usr/bin/env python3

import hashlib
import sys
import logging
logging.basicConfig(level=logging.DEBUG,
                    format='%(asctime)s %(levelname)s %(message)s',
                    filename='/var/log/squid/auth.log', filemode='a')

def sha1_encrypt(username):
    combined = username + "squid"
    return hashlib.sha1(combined.encode('utf-8')).hexdigest()

def authenticate(username, password):
    encrypted_password = sha1_encrypt(username)
    return encrypted_password == password

if __name__ == '__main__':
    while True:
        line = sys.stdin.readline()
        auth = line.split()
        user, passwd = auth[0], auth[1]
        logging.debug("Auth user={}, pwd={}".format(user, passwd))
        if authenticate(user, passwd):
            sys.stdout.write('OK\n')
        else:
            sys.stdout.write('ERR\n')
        sys.stdout.flush()

把Python脚本安装到/my_auth, 再修改squid.conf, 添加如下内容:

复制代码
http_access allow localhost # 允许localhost直接访问,无需认证

auth_param basic program /my_auth # 自定义认证程序
auth_param basic key_extras "%>rd"
auth_param basic children 5
auth_param basic realm Please enter your device credential
auth_param basic credentialsttl 24 hours # 24小时内不需要再认证
auth_param basic casesensitive on # 大小写敏感
acl AuthUsers proxy_auth REQUIRED

http_access allow AuthUsers

http_access deny all # 只允许localhost请求, 或者认证通过的请求

注:

  • 允许localhost不带认证直接访问
  • 别的机器访问必须通过认证,否则返回407

测试

用户名和密码正确, 返回200

复制代码
curl -x peter:47ae7e2352e92154c82669de2a99dd2091e60faa@192.168.52.202:3128 https://www.baidu.com
...
200 OK 

密码错误,返回407

复制代码
curl -x peter123:123@192.168.52.202:3128 https://www.baidu.com
curl: (56) Received HTTP code 407 from proxy after CONNECT

调试认证程序, 可以查看日志/var/log/squid/auth.log

参考

https://wiki.squid-cache.org/Features/Authentication

相关推荐
en-route3 小时前
HTTP 缓存
网络协议·http·缓存
隆里卡那唔11 小时前
在dify中通过http请求neo4j时为什么需要将localhost变为host.docker.internal
http·docker·neo4j
~山有木兮12 小时前
LiteHub中间件之限流实现
网络·http·中间件
游戏开发爱好者813 小时前
iOS App首次启动请求异常调试:一次冷启动链路抓包与初始化流程修复
websocket·网络协议·tcp/ip·http·网络安全·https·udp
2501_9151063213 小时前
频繁迭代下完成iOS App应用上架App Store:一次快速交付项目的完整回顾
websocket·网络协议·tcp/ip·http·网络安全·https·udp
00后程序员张20 小时前
免Mac上架实战:全平台iOS App上架流程的工具协作经验
websocket·网络协议·tcp/ip·http·网络安全·https·udp
笑衬人心。1 天前
HTTPS详解:原理 + 加解密过程 + 面试问答
java·网络协议·http·面试·https
bing_1581 天前
MQTT 和 HTTP 有什么本质区别?
网络·网络协议·http
代码讲故事1 天前
多种方法实现golang中实现对http的响应内容生成图片
开发语言·chrome·http·golang·图片·快照·截图
未来之窗软件服务1 天前
通过网页调用身份证阅读器http websocket方法-华视电子————仙盟创梦IDE
网络·网络协议·http·仙盟创梦ide·东方仙盟·硬件接入