0

我找到了一个代码,它应该到达登录的用户:

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>。这是为什么?我怎样才能找到登录用户的名字?

4

3 回答 3

2

你可以试试这个。

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。这仅适用于 weblogic。

于 2014-04-09T01:55:11.183 回答
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();
}
}
}
于 2013-11-04T16:27:05.063 回答
1

您的 bean 需要被标记为安全的(使用任何规范提供的安全相关拦截器发挥作用的方式)。

作为第一步,您可以执行以下操作:

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

这只是一个示例,您允许所有角色访问该方法。@PermitAll 安全注释的存在将指示 EJB 容器显示 EJB 安全拦截器。查看此文档以获取更多详细信息。为您提供 jboss 示例 https://docs.jboss.org/author/display/AS72/Securing+EJBs

于 2013-11-04T15:53:26.497 回答