Bypassing built-in browser authentication when mak

2019-05-25 13:45发布

问题:

  • I have a simple web page with an embedded Java applet.
  • The applet makes HTTP calls to different Axis Cameras who all share the same authentication (e.g. username, password).
  • I am passing the user name and password to the Java code upon launch of the applet - no problem.
  • When I run from within NetBeans with the applet viewer, I get full access to the cameras and see streaming video - exactly as advertised.
  • The problem begins when I open the HTML page in a web browser (Firefox).
  • Even though my code handles authentication:

    URL u = new URL(useMJPGStream ? mjpgURL : jpgURL);
    huc = (HttpURLConnection) u.openConnection();
    
    
    String base64authorization = 
        securityMan.getAlias(this.securityAlias).getBase64authorization();
    // if authorization is required set up the connection with the encoded 
    // authorization-information
    if(base64authorization != null)
    {
        huc.setDoInput(true);
        huc.setRequestProperty("Authorization",base64authorization);
        huc.connect();
    }
    
    InputStream is = huc.getInputStream();
    connected = true;
    BufferedInputStream bis = new BufferedInputStream(is);
    dis= new DataInputStream(bis);
    
  • The browser still brings up an authentication pop-up and requests the username and password for each camera separately!
  • To make things worse, the images displayed from the camera are frozen and old (from last night).
  • How can I bypass the browser's authentication?

回答1:

Fixed

I added the following lines:

huc.setDoOuput(true);
huc.setUseCaches(false);

after the

huc.setDoInput(true);

line.



回答2:

When running in the browser base64authorization not null, correct?

I'm not really sure what getBase64authorization is supposed to return, but I'm fairly certain when you call huc.setRequestProperty("Authorization", **autorization value**) it's looking for a HTTP Basic authentication value. Meaning **authorization value** needs to be in the format Basic **base 64 encoding of username:password** as described here.

Maybe you just need to add the Basic (note the trailing space) string to your property.