Im frustraded. I've got a Basic Http Authorization. For this I set a HttpGetHeader.
This Header needs to be Base64 encoded. I do it like so:
String acc = uname + ":" + pword;
byte[] a = acc.getBytes();
String header = "Basic " + new String(Base64.encode(a, Base64.DEFAULT));
But this encoded String doesn't work. When I log the header, it prints out the same as I need.
It looks the same as String h = "Basic c2NodWsZXI6aGVpbmNA=="; Which is the working one.
But when I compare header.equals(h); or header==h theire both false.
In the end when I set the header to headerit doesn't work, but when I'm using h it works. I guess its sometehing about String encoding but I tried different ways of .getBytes("UTF-8") and similiar (ASCII, UTF-16) but they worked neither.
The username and password are normal chars and numbers.
Can anyone see the mistake? Thanks
Grevius
header.equals(h) returning false indicates that the strings are not identical. header==h shall return false since they are not the same reference.
Empty spaces perhaps? try header.trim().equals(h.trim())
try using base64.encodestring(s)
Related
I want to replace particular string values with "XXXX". The issue is the pattern is very dynamic and it won't have a fixed pattern in input data.
My input data
https://internetbanking.abc.co.uk/personal/logon/login/?userId=Templ3108&password=Got1&reme
I need to replace the values of userId and password with "XXXX".
My output should be -
https://internetbanking.abc.co.uk/personal/logon/login/?userId=XXXX&password=XXXX&reme
This is an one off example. There are other cases where only userId and password is present -
userId=12345678&password=stackoverflow&rememberID=
I am using Regex in java to achieve the above, but have not been successful yet. Appreciate any guidance.
[&]([^\\/?&;]{0,})(userId=|password=)=[^&;]+|((?<=\\/)|(?<=\\?)|(?<=;))([^\\/?&;]{0,})(userId=|password=)=[^&]+|(?<=\\?)(userId=|password=)=[^&]+|(userId=|password=)=[^&]+
PS : I am not an expert in Regex. Also, please do let me know if there are any other alternatives to achieve this apart from Regex.
This may cover given both cases.
String maskUserNameAndPassword(String input) {
return input.replaceAll("(userId|password)=[^&]+", "$1=XXXXX");
}
String inputUrl1 =
"https://internetbanking.abc.co.uk/personal/logon/login/?userId=Templ3108&password=Got1&reme";
String inputUrl2 =
"userId=12345678&password=stackoverflow&rememberID=";
String input = "https://internetbanking.abc.co.uk/personal/logon/login/?userId=Templ3108&password=Got1&reme";
String maskedUrl1 = maskUserNameAndPassword(inputUrl1);
System.out.println("Mask url1: " + maskUserNameAndPassword(inputUrl1));
String maskedUrl2 = maskUserNameAndPassword(inputUrl1);
System.out.println("Mask url2: " + maskUserNameAndPassword(inputUrl2));
Above will result:
Mask url1: https://internetbanking.abc.co.uk/personal/logon/login/?userId=XXXXX&password=XXXXX&reme
Mask url2: userId=XXXXX&password=XXXXX&rememberID=
String url = "https://internetbanking.abc.co.uk/personal/logon/login/?userId=Templ3108&password=Got1&reme";
String masked = url.replaceAll("(userId|password)=[^&]+", "$1=XXXX");
See online demo and regex explanation.
Please note, that sending sensitive data via the query string is a big security issue.
I would rather use a URL parser than regex. The below example uses the standard URL class available in java but third party libraries can do it much better.
Function<Map.Entry<String, String>, Map.Entry<String, String>> maskUserPasswordEntries = e ->
(e.getKey().equals("userId") || e.getKey().equals("password")) ? Map.entry(e.getKey(), "XXXX") : e;
Function<List<String>, Map.Entry<String, String>> transformParamsToMap = p ->
Map.entry(p.get(0), p.size() == 1 ? "" : p.get(p.size() - 1));
URL url = new URL("https://internetbanking.abc.co.uk/personal/logon/login/?userId=Templ3108&password=Got1&reme");
String maskedQuery = Stream.of(url.getQuery().split("&"))
.map(s -> List.of(s.split("=")))
.map(transformParamsToMap)
.map(maskUserPasswordEntries).map(e -> e.getKey() + "=" + e.getValue())
.collect(Collectors.joining("&"));
System.out.println(url.getProtocol() + "://" + url.getAuthority() + url.getPath() + "?" + maskedQuery);
Output:
https://internetbanking.abc.co.uk/personal/logon/login/?userId=XXXX&password=XXXX&reme=
Just use the methods replace/replaceAll from the String class, they support Charset aswell as regex.
String url = "https://internetbanking.abc.co.uk/personal/logon/login/?userId=Templ3108&password=Got1&reme";
url = url.replaceAll("(userId=.+?&)", "userId=XXXX&");
url = url.replaceAll("(password=.+?&)", "password=XXXX&");
System.out.println(url);
I'm not a regex expert either, but if you find it useful, I usually use this website to test my expressions and as a online Cheatsheet:
https://regexr.com
Use:
(?<=(\?|&))(userId|password)=(.*?)(?=(&|$))
(?<=(\?|&)) makes sure it’s preceded by ? or & (but not part of the match)
(userId|password)= matches either userId or password, then =
(.*?) matches any char as long as the next instruction cannot be executed
(?=(&|$)) makes sure the next char is either & or end of the string, (but not part of the match)
Then, replace with $2=xxxxx (to keep userId or password) and choose replaceAll.
I tried searching for something similar, and couldn't find anything. I'm having difficulty trying to replace a few characters after a specific part in a URL.
Here is the URL: https://scontent-b.xx.fbcdn.net/hphotos-xpf1/v/t1.0-9/s130x130/10390064_10152552351881633_355852593677844144_n.jpg?oh=479fa99a88adea07f6660e1c23724e42&oe=5519DE4B
I want to remove the /v/ part, leave the t1.0-9, and also remove the /s130x130/.I cannot just replace s130x130, because those may be different variables. How do I go about doing that?
I have a previous URL where I am using this code:
if (pictureUri.indexOf("&url=") != -1)
{
String replacement = "";
String url = pictureUri.replaceAll("&", "/");
String result = url.replaceAll("().*?(/url=)",
"$1" + replacement + "$2");
String pictureUrl = null;
if (result.startsWith("/url="))
{
pictureUrl = result.replace("/url=", "");
}
}
Can I do something similar with the above URL?
With the regex
/v/|/s\d+x\d+/
replaced with
/
It turns the string from
https://scontent-b.xx.fbcdn.net/hphotos-xpf1/v/t1.0-9/s130x130/10390064_10152552351881633_355852593677844144_n.jpg?oh=479fa99a88adea07f6660e1c23724e42&oe=5519DE4B
to
https://scontent-b.xx.fbcdn.net/hphotos-xpf1/t1.0-9/10390064_10152552351881633_355852593677844144_n.jpg?oh=479fa99a88adea07f6660e1c23724e42&oe=5519DE4B
as seen here. Is this what you're trying to do?
I'm writing a library in Java which creates the URL from a list of filenames in this way:
final String domain = "http://www.example.com/";
String filenames[] = {"Normal text","Ich weiß nicht", "L'ho inserito tra i princìpi"};
System.out.println(domain+normalize(filenames[0]);
//Prints "http://www.example.com/Normal_text"
System.out.println(domain+normalize(filenames[1]);
//Prints "http://www.example.com/Ich_weib_nicht"
System.out.println(domain+normalize(filenames[2]);
//Prints "http://www.example.com/L_ho_inserito_tra_i_principi"
Exists somewhere a Java library that exposes the method normalize that I'm using in the code above?
Literature:
Which special characters are safe to use in url?
Safe characters for friendly url
Taking the content from my previous answer here, you can use java.text.Normalizer which comes close to normalizing Strings in Java. An example of normalization would be;
Accent removal:
String accented = "árvíztűrő tükörfúrógép";
String normalized = Normalizer.normalize(accented, Normalizer.Form.NFD);
normalized = normalized.replaceAll("[^\\p{ASCII}]", "");
System.out.println(normalized);
Gives;
arvizturo tukorfurogep
Assuming you mean you want to encode the strings to make them safe for the url. In which case use URLEncoder:
final String domain = "http://www.example.com/";
String filenames[] = {"Normal text","Ich weiß nicht", "L'ho inserito tra i princìpi"};
System.out.println(domain + URLEncoder.encode(filenames[0], "UTF-8"));
System.out.println(domain + URLEncoder.encode(filenames[1], "UTF-8"));
System.out.println(domain + URLEncoder.encode(filenames[2], "UTF-8"));
I need to show data from database that supposed to be shown:
in the html: Behälter
in the browser: Behälter
but instead, I got data like this:
in the html: Behälter
in the browser: Behälter
So I need to change the & back to &. I use replaceAll and replace method from the Java's String class. But it didn't work. I even check whether the String has & or not using indexOf method, but it didn't even seems to catch or even see the & sign.
My code:
// supposed the value returned by the getObject function is "Behälter"
String text = (String)getObject("value");
if (text.indexOf("&") >= 0) text = "abc" + text;
text = text.replace("&", "&");
text = text.replaceAll("&", "&");
if (text.indexOf("&") >= 0) text = text + "def";
text = text + "xyz";
The result is
in the html: Behälterxyz
in the browser: Behälterxyz
Is there anything wrong with how I type and use replace / replaceAll? Thank you for the answer.
I would recommend using Apache Commons Lang for the StringEscapeUtils.unescapeHtml().
Feed it your string with the encoded characters, and it should spit it out decoded.
I have a bunch of strings like this:
Some text, bla-bla http://www.easypolls.net/poll.html?p=51e5a300e4b084575d8568bb#.UeWjBcCzaaA.twitter
And I need to parse this String to two:
Some text, bla-bla
http://www.easypolls.net/poll.html?p=51e5a300e4b084575d8568bb#.UeWjBcCzaaA.twitter
I need separate them, but, of course, it's enough to parse only URL.
Can you help me, how can I parse url from string like this.
By using split :
String str = "Some text, bla-bla http://www.easypolls.net/poll.html?p=51e5a300e4b084575d8568bb#.UeWjBcCzaaA.twitter";
String [] ar = str.split("http\\.*");
System.out.println(ar[0]);
System.out.println("http"+ar[1]);
This depends on how robust you want your parser to be. If you can reasonably expect every url to start with http://, then you can use
string.indexOf("http://");
This returns the index of the first character of the string you pass in (and -1 if the string does not appear).
Full code to return a substring with just the URL:
string.substring(string.indexOf("http://"));
Here's the documentation for Java's String class. Let this become your friend in programming! http://docs.oracle.com/javase/7/docs/api/java/lang/String.html
Try something like this:
String string = "sometext http://www.something.com";
String url = string.substring(string.indexOf("http"), string.length());
System.out.println(url);
or use split.
I know in PHP you'd be able to run the explode() (http://www.php.net/manual/en/function.explode.php) function. You'd choose which character you want to explode at. For instance, you could explode at "http://"
So running the code via PHP would look like:
$string = "Some text, bla-bla http://www.easypolls.net/poll.html?p=51e5a300e4b084575d8568bb#.UeWjBcCzaaA.twitter";
$pieces = explode("http://", $string);
echo $pieces[0]; // Would print "Some text, bla-bla"
echo $pieces[1]; // Would print "www.easypolls.net/poll.html?p=51e5a300e4b084575d8568bb#.UeWjBcCzaaA.twitter"