Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在JSF上下文中替换经过身份验证的javax.security.Principal?_Java_Jsf_Jsf 2_Jaas - Fatal编程技术网

如何在JSF上下文中替换经过身份验证的javax.security.Principal?

如何在JSF上下文中替换经过身份验证的javax.security.Principal?,java,jsf,jsf-2,jaas,Java,Jsf,Jsf 2,Jaas,我想在我的JSF应用程序中创建一个“模拟”特性。此功能将使管理员能够访问通过低级用户身份验证的应用程序,而无需知道密码 我认为这将是一个简单的setUserPrincipal,类似于我用来获取当前登录用户的方法 FacesContext.getCurrentInstance().getExternalContext().getUserPrincipal(),但在javax.faces.context.ExternalContext中找不到任何“setUserPrincipal”方法 简而言之,我

我想在我的JSF应用程序中创建一个“模拟”特性。此功能将使管理员能够访问通过低级用户身份验证的应用程序,而无需知道密码

我认为这将是一个简单的
setUserPrincipal
,类似于我用来获取当前登录用户的方法
FacesContext.getCurrentInstance().getExternalContext().getUserPrincipal()
,但在
javax.faces.context.ExternalContext
中找不到任何“setUserPrincipal”方法

简而言之,我想要的是以编程方式更改当前登录的用户,这样管理员就可以模拟任何其他用户,而无需通知凭据。可能吗


谢谢

我强烈建议不要玩身份验证/授权,除非你真的没有其他选择

不管怎样,撇开JSF不谈,它在游戏中出现太晚了

最简单的方法是提供一个定制的请求,并提供一个过滤器:

@WebFilter(filterName = "impersonateFilter", urlPatterns = "/*", asyncSupported = true)
public class ImpersonateFilter implements Filter
{
    @Override
    public void init(FilterConfig filterConfig) throws ServletException
    {
        // do nothing
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException
    {
        HttpServletRequest httpRequest = (HttpServletRequest) request;
        ImpersonateRequest impersonateRequest = new ImpersonateRequest(httpRequest);
        chain.doFilter(impersonateRequest, response);
    }

    @Override
    public void destroy()
    {
        // do nothing
    }

    public static class ImpersonateRequest extends HttpServletRequestWrapper
    {
        protected Principal principal;

        public ImpersonateRequest(HttpServletRequest request)
        {
            super(request);

            HttpSession session = request.getSession(false);
            if(session != null)
            {
                principal = (Principal) session.getAttribute(ImpersonateRequest.class.getName());
            }
        }

        @Override
        public Principal getUserPrincipal()
        {
            if(principal == null)
            {
                principal = super.getUserPrincipal();
            }

            return principal;
        }

        public void setUserPrincipal(Principal principal)
        {
            this.principal = principal;
            getSession().setAttribute(ImpersonateRequest.class.getName(), principal);
        }

        @Override
        public String getRemoteUser()
        {
            return principal == null ? super.getRemoteUser() : principal.getName();
        }
    }
}

这样就足够了。

让校长一个人呆着,换个角色就行了。确保您可以将其更改回:-我总是在身份验证机制周围创建一个(小)包装器,在其中放置“模拟”内容。因此,我永远不需要接触身份验证机制。