2013-11-04 71 views
0

我發現了一個代碼,它應該達到的登錄用戶:如何使用WEBLOGIC在JAVA中獲取登錄的用戶?

InitialContext ic = new InitialContext(); 
SessionContext sessionContext = (SessionContext)ic.lookup("java:comp/EJBContext"); 
System.out.println("look up injected sctx: " + sessionContext); 
Principal p = sessionContext.getCallerPrincipal(); 
System.out.println(p.getName()); 

但它仍然給我<anonymus>。這是爲什麼?我怎樣才能達到記錄的用戶名?

回答

1

您的bean需要被標記爲安全(使用規範中提供的任何安全相關攔截器發揮作用的方式)。

作爲第一步,你可以這樣做:

@Override 
@PermitAll 
public String whoAmI() { 
    return context.getCallerPrincipal().getName(); 
} 

這只是在那裏你允許所有角色訪問方法的例子。該@PermitAll安全註釋的存在將指示EJB容器將EJB安全攔截器引入其中。看看這些文件,以進一步的細節.Giving你的JBoss例如 https://docs.jboss.org/author/display/AS72/Securing+EJBs

+0

它不適合我:/不是他們 – victorio

1

可以使用的MBean API也得到它

import javax.naming.*; 
import javax.management.MBeanInfo; 
import weblogic.jndi.Environment; 
import weblogic.management.runtime.ServerRuntimeMBean; 
import weblogic.security.providers.authentication.DefaultAuthenticatorMBean; 
import weblogic.management.security.authentication.UserReaderMBean; 
import weblogic.management.security.authentication.GroupReaderMBean; 
import weblogic.management.MBeanHome; 
import weblogic.management.WebLogicMBean; 
import weblogic.management.tools.Info; 
import weblogic.management.Helper; 
import weblogic.management.security.authentication.*; 

public class ListUsersAndGroups 
{ 
public static void main(String[] args) 
{ 

MBeanHome home = null; 
try 
{ 

Environment env = new Environment(); 
env.setProviderUrl(「t3://localhost:7001?); 
env.setSecurityPrincipal(「weblogic」); 
env.setSecurityCredentials(「weblogic」); 
Context ctx = env.getInitialContext(); 

home = (MBeanHome)ctx.lookup(「weblogic.management.adminhome」); 

weblogic.management.security.RealmMBean rmBean = home.getActiveDomain().getSecurityConfiguration().getDefaultRealm(); 

AuthenticationProviderMBean[] authenticationBeans = rmBean.getAuthenticationProviders(); 
DefaultAuthenticatorMBean defaultAuthenticationMBean = (DefaultAuthenticatorMBean)authenticationBeans[0]; 
UserReaderMBean userReaderMBean = (UserReaderMBean)defaultAuthenticationMBean; 
GroupReaderMBean groupReaderMBean = (GroupReaderMBean)defaultAuthenticationMBean; 

String userCurName = userReaderMBean.listUsers(「*」, 100); 

while (userReaderMBean.haveCurrent(userCurName)) 
{ 
String user = userReaderMBean.getCurrentName(userCurName); 
System.out.println(「\n User: 」 + user); 
userReaderMBean.advance(userCurName); 
} 

String cursorName = groupReaderMBean.listGroups(「*」, 100); 
while (groupReaderMBean.haveCurrent(cursorName)) 
{ 

String group = groupReaderMBean.getCurrentName(cursorName); 
System.out.println(「\n Group: 」 + group); 
groupReaderMBean.advance(cursorName); 
} 

} 
catch (Exception e) 
{ 
e.printStackTrace(); 
} 
} 
} 
1

你可以試試這個。

Subject subject = Subject.getSubject(AccessController.getContext()); 
Set<java.security.Principal> principals = s.getPrincipals(); 
for (java.security.Principal principal : principals) { 
    if (principal.getClass() == WLSUserImpl.class) { 
    return principal.getName(); 
    } 
} 

凡WLSUserImpl從包weblogic.security.principal.This來只爲WebLogic可行的。