jsp访问EJB 问题!
问题:我是用jsp访问本地网络的一个session这个session需要认证才能访问:
我现在遇到的问题是;如果我加上这个认证的程序块,就不能通过,
但是如果我把这个访问session的程序作为一个单独的Client类的时候是能通过的
我不知道什么原因:(
下面是我的client类
是正常的
/*
* Created on 2005-4-16
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package junit;
import javax.naming.Context;
import javax.naming.InitialContext;
import java.rmi.RemoteException;
import java.util.Hashtable;
import java.util.Properties;
import javax.ejb.CreateException;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import net.lancetech.ioffice.pub.PublicBasic;
import net.lancetech.ioffice.pub.PublicBasicHome;
import net.ioffice.lanceAuthentication.*;
import javax.security.auth.login.LoginContext;
import com.jivesoftware.base.Log;
import com.jivesoftware.base.UnauthorizedException;
import com.jivesoftware.base.AuthToken;
import com.jivesoftware.base.AuthFactory;
import com.jivesoftware.base.UnauthorizedException;
import com.jivesoftware.base.Log;
/**
* @author kuanda
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class TestEJB {
private net.lancetech.ioffice.pub.PublicBasicHome getHome()
throws NamingException {
return (net.lancetech.ioffice.pub.PublicBasicHome) getContext().lookup(
"iOffice/PublicBasic");
}
private InitialContext getContext() throws NamingException {
Hashtable props = new Hashtable();
props.put(InitialContext.INITIAL_CONTEXT_FACTORY,
"org.jnp.interfaces.NamingContextFactory");
props.put(InitialContext.PROVIDER_URL, "jnp://192.168.0.131:1099");
props.put(InitialContext.URL_PKG_PREFIXES,"org.jboss.naming:org.jnp.interfaces");
InitialContext initialContext = new InitialContext(props);
return initialContext;
}
public PublicBasic testBean(String username, String password)
throws UnauthorizedException
{
if (username == null || password == null) {
throw new UnauthorizedException();
}
char[] charPass;
charPass = new char[password.length()];
for (int i = 0;i< password.length();i++){
charPass[i] = password.charAt(i);
}
try {
iOfficeCallbackHandler handler = new iOfficeCallbackHandler(username, charPass);
LoginContext lc = new LoginContext("iOffice", handler);
lc.login();
net.lancetech.ioffice.pub.PublicBasic myBean = getHome().create();
return myBean;
} catch (RemoteException e) {
e.printStackTrace();
return null;
} catch (CreateException e) {
e.printStackTrace();
return null;
} catch (NamingException e) {
e.printStackTrace();
return null;
} catch (javax.security.auth.login.LoginException e){
e.printStackTrace();
throw new UnauthorizedException();
}
}
public static void main(String[] args) {
TestEJB test = new TestEJB();
PublicBasic myb;
try{
myb = test.testBean("wangyukui@lance-tech.net","wangyukui");
System.out.println( myb.getMyself().firstName);
}catch(UnauthorizedException e){
e.printStackTrace();
}
catch (RemoteException e) {
e.printStackTrace();
}
}
}