internetitem / logback-elasticsearch-appender

Logback Elasticsearch Appender
Other
233 stars 137 forks source link

How to solve the problem if es password contains @ #77

Open aishuhaon opened 4 years ago

aishuhaon commented 4 years ago

How to solve the problem if es password contains @

yoesoff commented 3 years ago

I think you can just url_encode it using https://www.urlencoder.org/

18855441015 commented 3 years ago

I set the url property like “http://${ELASTIC_NAME}:${ELASTIC_PASSWORD}@${ELASTIC_URL}/_bulk”,used System's environment variable to configure es's parameters Dynamically,my password contains '@' too, how to deal with this kind of problem,I would appreciate it if you could answer。

jonahbenton commented 2 years ago

See https://stackoverflow.com/questions/20574841/http-basic-authentication-url-with-in-password

frdrolland commented 2 years ago

Hi,

To fix that I made an URL encoding as yoesoff said, but there was still an issue because Elasticsearch rejects the authentication with 401 error.

Reason is that in class com.internetitem.logback.elasticsearch.config.BasicAuthentication, login/password is read from URL and passed to basic auth header without decoding the password.

So my solution was to implement a custom BasicAuthentication class to decode the password before encoding it in base64 and putting it in Authorization header. This custom BasicAuthentication class can be changed in logback configuration, by changing value of : <authentication class="com.internetitem.logback.elasticsearch.config.BasicAuthentication" /><authentication class="com.internetitem.logback.elasticsearch.config.BasicAuthentication" />

leopold7 commented 2 years ago

Thx to frdrolland. This is my code and it works for me.

  1. Encoding your password as yoesoff said, then your url like elastic:abc%40123@127.0.0.1:9200
  2. Changing your logback configuration such as <authentication class="com.xxx.config.BasicAuthentication" />
  3. Decoding your password and encoding it
    
    package com.xxx.config;

import com.internetitem.logback.elasticsearch.config.Authentication; import com.internetitem.logback.elasticsearch.util.Base64; import org.yaml.snakeyaml.util.UriEncoder;

import java.net.HttpURLConnection;

public class BasicAuthentication implements Authentication { public BasicAuthentication() { }

public void addAuth(HttpURLConnection urlConnection, String body) {
    String userInfo = urlConnection.getURL().getUserInfo();
    if (userInfo != null) {
        String basicAuth = "Basic " + Base64.encode(UriEncoder.decode(userInfo).getBytes());
        urlConnection.setRequestProperty("Authorization", basicAuth);
    }
}

}


There you go ~