Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

cannot access protected url by java

2 views
Skip to first unread message

shai

unread,
Apr 30, 2003, 6:03:20 AM4/30/03
to
I try to access url like http://username:pass...@www.host.com.
by browser i can access it,but by servlet i get :
java.security.AccessControlException: access denied
(java.net.NetPermission
setDefaultAuthenticator)
at java.security.AccessControlContext.checkPermission(AccessControlContext.java:270)
at java.security.AccessController.checkPermission(AccessController.java:401)
at java.lang.SecurityManager.checkPermission(SecurityManager.java:542)
at java.net.Authenticator.setDefault(Authenticator.java:83)
...
I use a following code:
public class MyServlet extends HttpServlet {

public void service(HttpServletRequest req, HttpServletResponse res)
throws IOException{
String userName = "username";
String password = "password";
URL url=new URL("http://www.host.com");
Authenticator.setDefault (new MyAuthenticator
(userName,password));

URLConnection uc=url.openConnection();
uc.setAllowUserInteraction(true);

InputStream fi = uc.getInputStream();

}

}

public class MyAuthenticator extends Authenticator {
private java.security.AccessControlContext js=null;
private String userName = null;
private String password = null;

public MyAuthenticator(String userName,String password){
super();
this.userName = userName;
this.password = password;
}

protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication
(userName,password.toCharArray());
}
}

0 new messages