使用 JNDI 查找访问实现 class
Access Implementation class using JNDI lookup
我已经使用 REST 和 EJB 创建了一个 J2EE 项目。
我的结束文件是webservice.war
我无法从 WebLoginModule.java
的 JNDI 上下文查找中找到 EmployeeEJBImpl
这是我的文件结构
我正在尝试实施 Jboss JASS 安全性,我需要在其中检查数据库中的用户名密码。这是我的文件
public class WebLoginModule extends UsernamePasswordLoginModule{
private SecurityPrincipal sp = null;
Subject sub ;
public boolean logout() throws LoginException
{
sub.getPrincipals().remove(sp);
System.out.println("Logging out");
return super.logout();
}
@Override
protected boolean validatePassword(String username, String password)
{
Principal p = this.getIdentity();
sub = new Subject();
System.out.println("password(as param) : "+password);
if(password==null){
System.out.println("password empty");
password = username;
username = p.getName();
}else{
//Do nothing
}
if(p instanceof SecurityPrincipal) {
try {
sp = (SecurityPrincipal)p;
sp.setUsername(p.getName());
sp.setPassword(password);
sp.setSubj(sub);
sp.setColRole(null);; // TODO: fix this.
System.out.println("username: "+username);
return isValidUser(username, password);
}
catch(Exception e) {
e.printStackTrace();
}
}
return false;
}
@Override
protected String getUsersPassword() throws LoginException {
// TODO Auto-generated method stub
return null;
}
public boolean isValidUser(String username, String password) {
boolean result=false;
try{
final Hashtable jndiProperties = new Hashtable();
jndiProperties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
final Context context = new InitialContext(jndiProperties);
System.out.println("getting value");
EmployeeEJBIf lif = (EmployeeEJBIf) context.lookup("java:global/!EmployeeEJBIf");
System.out.println("loading data");
password=hashPassword(password);
}catch(Exception e){
e.printStackTrace();
return false;
}
return result;
}
@Override
protected Group[] getRoleSets() throws LoginException {
try {
Group callerPrincipal = new SimpleGroup("CallerPrincipal");
Group roles = new SimpleGroup("Roles");
Group[] groups = {roles,callerPrincipal};
roles.addMember(new SecurityPrincipal("SecurityAdmin"));
callerPrincipal.addMember(sp);
return groups;
}
catch(Exception e) {
e.printStackTrace();
throw new LoginException(e.getMessage());
}
}
private String hashPassword(String password) {
String hashword = null;
try {
MessageDigest md5 = MessageDigest.getInstance("MD5");
md5.update(password.getBytes());
BigInteger hash = new BigInteger(1, md5.digest());
hashword = hash.toString(16);
}catch (Exception e) {
e.printStackTrace();
}
return hashword;
}
异常:
获得价值
11:15:13,295 ERROR [stderr] (http--192.168.1.153-8080-5) javax.naming.NameNotFoundException: !EmployeeEJBIf -- service jboss.naming.context.java.global.!EmployeeEJBIf
11:15:13,295 ERROR [stderr] (http--192.168.1.153-8080-5) at org.jboss.as.naming.ServiceBasedNamingStore.lookup(ServiceBasedNamingStore.java:97)
11:15:13,295 ERROR [stderr] (http--192.168.1.153-8080-5) at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:178)
11:15:13,295 ERROR [stderr] (http--192.168.1.153-8080-5) at org.jboss.as.naming.InitialContext.lookup(InitialContext.java:123)
11:15:13,295 ERROR [stderr] (http--192.168.1.153-8080-5) at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:214)
11:15:13,295 ERROR [stderr] (http--192.168.1.153-8080-5) at javax.naming.InitialContext.lookup(Unknown Source)
11:15:13,295 ERROR [stderr] (http--192.168.1.153-8080-5) at com.webservice.security.WebLoginModule.isValidUser(WebLoginModule.java:88)
11:15:13,295 ERROR [stderr] (http--192.168.1.153-8080-5) at com.webservice.security.WebLoginModule.validatePassword(WebLoginModule.java:64)
11:15:13,295 ERROR [stderr] (http--192.168.1.153-8080-5) at org.jboss.security.auth.spi.UsernamePasswordLoginModule.login(UsernamePasswordLoginModule.java:267)
11:15:13,295 ERROR [stderr] (http--192.168.1.153-8080-5) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
11:15:13,295 ERROR [stderr] (http--192.168.1.153-8080-5) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
11:15:13,295 ERROR [stderr] (http--192.168.1.153-8080-5) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
11:15:13,295 ERROR [stderr] (http--192.168.1.153-8080-5) at java.lang.reflect.Method.invoke(Unknown Source)
11:15:13,295 ERROR [stderr] (http--192.168.1.153-8080-5) at javax.security.auth.login.LoginContext.invoke(Unknown Source)
11:15:13,311 ERROR [stderr] (http--192.168.1.153-8080-5) at javax.security.auth.login.LoginContext.access[=12=]0(Unknown Source)
11:15:13,312 ERROR [stderr] (http--192.168.1.153-8080-5) at javax.security.auth.login.LoginContext.run(Unknown Source)
11:15:13,313 ERROR [stderr] (http--192.168.1.153-8080-5) at javax.security.auth.login.LoginContext.run(Unknown Source)
11:15:13,313 ERROR [stderr] (http--192.168.1.153-8080-5) at java.security.AccessController.doPrivileged(Native Method)
11:15:13,314 ERROR [stderr] (http--192.168.1.153-8080-5) at javax.security.auth.login.LoginContext.invokePriv(Unknown Source)
11:15:13,315 ERROR [stderr] (http--192.168.1.153-8080-5) at javax.security.auth.login.LoginContext.login(
您应该能够通过 Web 中的注释字段注入 EJB class:
public class WebLoginModule extends UsernamePasswordLoginModule {
@EJB
private EmployeeEJBIf employeeService;
...
因为您的实现是唯一实现接口的实现,注入框架会自动使用您的实现。
好吧,我在登录身份验证时需要这个 EJB。所以我不能作为 EJB 注入。
我通过为 url 提供正确的路径解决了问题,即
context.lookup("java:global/webservice/EmployeeEJBImpl!com.webservice.service.EmployeeEJBIf");
我已经使用 REST 和 EJB 创建了一个 J2EE 项目。
我的结束文件是webservice.war
我无法从 WebLoginModule.java
的 JNDI 上下文查找中找到 EmployeeEJBImpl这是我的文件结构
我正在尝试实施 Jboss JASS 安全性,我需要在其中检查数据库中的用户名密码。这是我的文件
public class WebLoginModule extends UsernamePasswordLoginModule{
private SecurityPrincipal sp = null;
Subject sub ;
public boolean logout() throws LoginException
{
sub.getPrincipals().remove(sp);
System.out.println("Logging out");
return super.logout();
}
@Override
protected boolean validatePassword(String username, String password)
{
Principal p = this.getIdentity();
sub = new Subject();
System.out.println("password(as param) : "+password);
if(password==null){
System.out.println("password empty");
password = username;
username = p.getName();
}else{
//Do nothing
}
if(p instanceof SecurityPrincipal) {
try {
sp = (SecurityPrincipal)p;
sp.setUsername(p.getName());
sp.setPassword(password);
sp.setSubj(sub);
sp.setColRole(null);; // TODO: fix this.
System.out.println("username: "+username);
return isValidUser(username, password);
}
catch(Exception e) {
e.printStackTrace();
}
}
return false;
}
@Override
protected String getUsersPassword() throws LoginException {
// TODO Auto-generated method stub
return null;
}
public boolean isValidUser(String username, String password) {
boolean result=false;
try{
final Hashtable jndiProperties = new Hashtable();
jndiProperties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
final Context context = new InitialContext(jndiProperties);
System.out.println("getting value");
EmployeeEJBIf lif = (EmployeeEJBIf) context.lookup("java:global/!EmployeeEJBIf");
System.out.println("loading data");
password=hashPassword(password);
}catch(Exception e){
e.printStackTrace();
return false;
}
return result;
}
@Override
protected Group[] getRoleSets() throws LoginException {
try {
Group callerPrincipal = new SimpleGroup("CallerPrincipal");
Group roles = new SimpleGroup("Roles");
Group[] groups = {roles,callerPrincipal};
roles.addMember(new SecurityPrincipal("SecurityAdmin"));
callerPrincipal.addMember(sp);
return groups;
}
catch(Exception e) {
e.printStackTrace();
throw new LoginException(e.getMessage());
}
}
private String hashPassword(String password) {
String hashword = null;
try {
MessageDigest md5 = MessageDigest.getInstance("MD5");
md5.update(password.getBytes());
BigInteger hash = new BigInteger(1, md5.digest());
hashword = hash.toString(16);
}catch (Exception e) {
e.printStackTrace();
}
return hashword;
}
异常:
获得价值
11:15:13,295 ERROR [stderr] (http--192.168.1.153-8080-5) javax.naming.NameNotFoundException: !EmployeeEJBIf -- service jboss.naming.context.java.global.!EmployeeEJBIf
11:15:13,295 ERROR [stderr] (http--192.168.1.153-8080-5) at org.jboss.as.naming.ServiceBasedNamingStore.lookup(ServiceBasedNamingStore.java:97)
11:15:13,295 ERROR [stderr] (http--192.168.1.153-8080-5) at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:178)
11:15:13,295 ERROR [stderr] (http--192.168.1.153-8080-5) at org.jboss.as.naming.InitialContext.lookup(InitialContext.java:123)
11:15:13,295 ERROR [stderr] (http--192.168.1.153-8080-5) at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:214)
11:15:13,295 ERROR [stderr] (http--192.168.1.153-8080-5) at javax.naming.InitialContext.lookup(Unknown Source)
11:15:13,295 ERROR [stderr] (http--192.168.1.153-8080-5) at com.webservice.security.WebLoginModule.isValidUser(WebLoginModule.java:88)
11:15:13,295 ERROR [stderr] (http--192.168.1.153-8080-5) at com.webservice.security.WebLoginModule.validatePassword(WebLoginModule.java:64)
11:15:13,295 ERROR [stderr] (http--192.168.1.153-8080-5) at org.jboss.security.auth.spi.UsernamePasswordLoginModule.login(UsernamePasswordLoginModule.java:267)
11:15:13,295 ERROR [stderr] (http--192.168.1.153-8080-5) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
11:15:13,295 ERROR [stderr] (http--192.168.1.153-8080-5) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
11:15:13,295 ERROR [stderr] (http--192.168.1.153-8080-5) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
11:15:13,295 ERROR [stderr] (http--192.168.1.153-8080-5) at java.lang.reflect.Method.invoke(Unknown Source)
11:15:13,295 ERROR [stderr] (http--192.168.1.153-8080-5) at javax.security.auth.login.LoginContext.invoke(Unknown Source)
11:15:13,311 ERROR [stderr] (http--192.168.1.153-8080-5) at javax.security.auth.login.LoginContext.access[=12=]0(Unknown Source)
11:15:13,312 ERROR [stderr] (http--192.168.1.153-8080-5) at javax.security.auth.login.LoginContext.run(Unknown Source)
11:15:13,313 ERROR [stderr] (http--192.168.1.153-8080-5) at javax.security.auth.login.LoginContext.run(Unknown Source)
11:15:13,313 ERROR [stderr] (http--192.168.1.153-8080-5) at java.security.AccessController.doPrivileged(Native Method)
11:15:13,314 ERROR [stderr] (http--192.168.1.153-8080-5) at javax.security.auth.login.LoginContext.invokePriv(Unknown Source)
11:15:13,315 ERROR [stderr] (http--192.168.1.153-8080-5) at javax.security.auth.login.LoginContext.login(
您应该能够通过 Web 中的注释字段注入 EJB class:
public class WebLoginModule extends UsernamePasswordLoginModule {
@EJB
private EmployeeEJBIf employeeService;
...
因为您的实现是唯一实现接口的实现,注入框架会自动使用您的实现。
好吧,我在登录身份验证时需要这个 EJB。所以我不能作为 EJB 注入。 我通过为 url 提供正确的路径解决了问题,即
context.lookup("java:global/webservice/EmployeeEJBImpl!com.webservice.service.EmployeeEJBIf");