【问题标题】:Is it possible to programmatically set Principal in JSF+EJB是否可以在 JSF+EJB 中以编程方式设置 Principal
【发布时间】:2012-12-10 22:16:07
【问题描述】:

我想使用 EJBContext.getCallerPrincipal() 检索用户名

public class GlobalInterceptor {

    @EJB
    private  AuditLogsEJB auditLogsEJB;

    @Resource
    private EJBContext context;

    @AroundInvoke
    public Object auditLog(InvocationContext invocationContext) throws Exception
    {  
          System.out.println(context.getCallerPrincipal());  //it outputs "ANONYMOUS"
    }
}

但是我没有任何安全领域,我没有定义任何 ,因为用户名/密码被发送到远程应用程序进行验证。但为了方便起见,我仍然想使用 Principal 类来获取用户名和角色。

有没有办法在 JSF 托管 bean 或任何 EJB 中以编程方式设置 Principal(用户名和角色),以便拦截器可以检索它?例如:

String role = authenticationWSPort.validate(username, password);
if(role != null) 
{
      Principal p = new Principal(username);
      p.getRoles.add(role);
      FacesContext.getCurrentInstance().blah-blah.blah-blah.addPrincipal(p);
      //I am looking for something similar to the 3 lines above
}
else
     // invalid username and password

【问题讨论】:

    标签: jsf ejb-3.0 ejb-3.1 jaas principal


    【解决方案1】:

    显然,正确的方法是设置一个领域并使用它。

    也许您可以创建和配置自己的 Realm 实现,让您在 servlet 中调用 request.login(username, password) 之前即时创建用户。

    或者。

    如果你 - 真的 - 想要使用底层请求,这是可能的,但它是特定于应用程序服务器的,并且会像这样,假设你在一个基于 tomcat (catalina) 的容器中:

    if (request instanceof RequestFacade) {
            Request unwrapped = null;
            RequestFacade rf = RequestFacade.class.cast(request);
            try {
                Field requestField = rf.getClass().getDeclaredField("request");
                requestField.setAccessible(true);
                unwrapped = Request.class.cast(requestField.get(rf));
            } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException ex) {
                unwrapped = null;
            }
    
            if (unwrapped != null) {
                unwrapped.setUserPrincipal(... your principal construction here ...);
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-15
      • 1970-01-01
      • 2023-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多