如何在 mac 上配置启用了 SASL 的 memcached 用户名和密码

How to configure SASL enabled memcached username and password on mac

我使用自制软件在 mac 上安装了 memcached 版本 1.4.34。我想配置用户名和密码以在与内存缓存交互时启用 SASL 支持。你能为此指出正确的方向吗? 运行 下面的命令在 mac.

上安装内存缓存
brew install memcached --enable-sasl-pwdb

以上命令安装了支持 sasl 的内存缓存。

echo "mech_list: plain" > memcached.conf
echo "myuser:mypass" > /tmp/memcached-sasl-db
export MEMCACHED_SASL_PWDB=/tmp/memcached-sasl-db
export SASL_CONF_PATH=`pwd`/memcached.conf
memcached -u myuser -m 1024 -p 8010 -S -B binary -vvv

Initialized SASL.

现在当我通过 memcache 客户端连接时,它说终端上的密码验证失败。

mech:  ``PLAIN'' with 15 bytes of data
INFO: User <myuser@mylocal-macbook.local> failed to authenticate
SASL (severity 2): Password verification failed
sasl result code:  -20
Unknown sasl response:  -20

这是我正在使用的 java 代码:

public class MemcacheTest {

public static void main(String[] args) {
    System.setProperty("net.spy.memcached.auth.AuthThreshold", "10");

    AuthDescriptor ad = new AuthDescriptor(new String[] { "PLAIN" },
            new PlainCallbackHandler(
                    "myuser", "mypass"));

    ConnectionFactory connFactory = new ConnectionFactoryBuilder()
            .setProtocol(ConnectionFactoryBuilder.Protocol.BINARY)
            .setAuthWaitTime(10000)
            .setOpTimeout(10000)
            .setShouldOptimize(true)
            .setAuthDescriptor(ad).build();

    List<InetSocketAddress> servers = AddrUtil
            .getAddresses("localhost:8010");
    MemcachedClient cacheClient = null;
    try {
        cacheClient = new MemcachedClient(connFactory, servers);
        cacheClient.set("foo", 50000, "bar");
        System.out.println("Value: " + cacheClient.get("foo"));
    } catch (IOException iox) {
        iox.printStackTrace();
    }
}

}

这是我的 intellij 日志:

    2017-02-23 15:19:04.223 INFO net.spy.memcached.MemcachedConnection:  Reconnection due to exception handling a memcached operation on {QA sa=localhost/127.0.0.1:11211, #Rops=0, #Wops=0, #iq=0, topRop=null, topWop=null, toWrite=0, interested=1}. This may be due to an authentication failure.
OperationException: SERVER: Auth failure.
    at net.spy.memcached.protocol.BaseOperationImpl.handleError(BaseOperationImpl.java:192)
    at net.spy.memcached.protocol.binary.OperationImpl.finishedPayload(OperationImpl.java:204)
    at net.spy.memcached.protocol.binary.SASLBaseOperationImpl.finishedPayload(SASLBaseOperationImpl.java:98)
    at net.spy.memcached.protocol.binary.OperationImpl.readPayloadFromBuffer(OperationImpl.java:196)
    at net.spy.memcached.protocol.binary.OperationImpl.readFromBuffer(OperationImpl.java:139)
    at net.spy.memcached.MemcachedConnection.readBufferAndLogMetrics(MemcachedConnection.java:861)
    at net.spy.memcached.MemcachedConnection.handleReads(MemcachedConnection.java:840)
    at net.spy.memcached.MemcachedConnection.handleReadsAndWrites(MemcachedConnection.java:720)
    at net.spy.memcached.MemcachedConnection.handleIO(MemcachedConnection.java:683)
    at net.spy.memcached.MemcachedConnection.handleIO(MemcachedConnection.java:436)
    at net.spy.memcached.MemcachedConnection.run(MemcachedConnection.java:1446)

耶!我明白了。 Couch base 客户端会自动在用户名末尾添加主机名。在我的例子中,当我将用户名设置为 myuser:mypassword 但是当我在我的 java 代码中传递 myuser 时,memcacheclient 将该信息作为 myuser@mylocalhost-mac 传递,这就是不匹配发生的地方。为了解决这个问题,我添加了带有 ssl 数据库本地主机的完整用户名。

而不是下行

echo "myuser:mypass" > /tmp/memcached-sasl-db

替换为

echo "myuser@mylocalhostMacBook-Pro-2.local:mypass" > /tmp/memcached-sasl-db

现在在 java 客户端中,我只传递 myuser 和客户端自动添加主机名。