התקן של HTTP כולל קביעה של שם משתמש וסיסמא ישירות בURL, למשל:

http://user:password@site.com/file.txt

מסיבה לא ברורה, ג’אווה לא מסתדר עם URLים כאלו ונראה שהוא לא מעביר את שם המשתמש והסיסמא לאתר.
הנה פתרון לעניין (עקום אך גנרי) :

Authenticator.setDefault(new Authenticator()
{
    @Override
    protected PasswordAuthentication getPasswordAuthentication()
    {
        URL url = getRequestingURL();
        String userInfo = url.getUserInfo();
        String user;
        String password;
        int i = userInfo.indexOf(‘:’);
        if (i == -1)
        {
            user = userInfo.substring(0);
            password = “”;
        }
        else
        {
            user = userInfo.substring(0, i);
            password = userInfo.substring(i+1);
        }
        PasswordAuthentication ps = new PasswordAuthentication(user, password.toCharArray());
        return ps;
    }
});

עוגיה למי שסביר למה המימוש הסטנדרטי של ג’אווה לא תומך בדבר הטריויאלי הזה.

4 תגובות ל “Java with authenticated urls”

  1. שחר ISRAEL Windows Vista Mozilla Firefox 3.5.5 כותב/ת

    לפי מה שאני יודע, השימוש ב-username:password כחלק מה-URL הוא לא בתקן של HTTP, למרות שבחלק מהמקרים זה עובד. אני לא יודע אם זה עובד בגלל שהדפדפנים דואגים לשים את שם המשתמש והסיסמה ב-header או שהשרת מקבל את ה-URL מהצורה הזאת.

  2. עמרי ISRAEL Debian GNU/Linux Mozilla Firefox 3.0.6 כותב/ת

    http://www.ietf.org/rfc/rfc1738.txt

    3.1. Common Internet Scheme Syntax
    While the syntax for the rest of the URL may vary depending on the
    particular scheme selected, URL schemes that involve the direct use
    of an IP-based protocol to a specified host on the Internet use a
    common syntax for the scheme-specific data:
    user:password@host:port/url-path
  3. שחר ISRAEL Windows Vista Mozilla Firefox 3.5.5 כותב/ת

    באותו מסמך, בסעיף 3.3 שמתייחס ספציפית ל-HTTP כתוב:

    3.3. HTTP
    …..
    An HTTP URL takes the form:

    http://:/?

    where and are as described in Section 3.1. If :
    is omitted, the port defaults to 80. No user name or password is
    allowed.

  4. עמרי ISRAEL Debian GNU/Linux Mozilla Firefox 3.0.6 כותב/ת

    כנראה הדפדפנים דואגים להכניס את המידע בHEADER של הבקשה (בכל מקרה השרת לא מקבל את הURL עצמו).
    נראה לי שמגיעה לך עוגיה.
    קבל:
    cookie

השאר תגובה

FireStats icon ‏מריץ FireStats‏