【问题标题】:How to configure security handler in embedded jetty after jetty start码头启动后如何在嵌入式码头中配置安全处理程序
【发布时间】:2018-11-14 04:20:18
【问题描述】:

我正在尝试在 Jetty 启动后在 Jetty 中的 ServletContext 上配置一个安全处理程序。

像这样:

Handler[] contextHandlers = contexts.getHandlers();
for(Handler context : contextHandlers) {
    if(context instanceof ServletContextHandler && ((ServletContextHandler) context).getContextPath().equals("/api")) {
        context.setSecurityHandler(securityHandler);
        break;
}

但我得到以下异常:

java.lang.IllegalStateException:已启动

在 org.eclipse.jetty.servlet.ServletContextHandler.setSecurityHandler(ServletContextHandler.java:483)

为什么这不可能?

截图:

编辑:

我查看了源代码并在那里检查 isStarted 标志。码头启动后添加安全处理程序是否存在安全漏洞?:

public void setSecurityHandler(SecurityHandler securityHandler)
    {
        if (isStarted())
            throw new IllegalStateException("STARTED");

        if (_securityHandler!=null)
            _securityHandler.setHandler(null);
        _securityHandler = securityHandler;
        relinkHandlers();
    }

(原因,我必须这样做有点复杂,但我会尝试解释一下:我在代理后面运行 keycloak 服务器,该代理可以通过我的 Jetty 服务器访问。假设 Jetty 在 host1 上运行,并且keycloak 在 host2 上运行。但是在设置 keycloak 安全处理程序时,无论配置了哪个主机,keycloak 只允许对从该域生成的令牌进行身份验证。因此我想在安全处理程序中配置 Jetty 主机,直到 Jetty 启动才可用)

【问题讨论】:

    标签: java jetty keycloak embedded-jetty


    【解决方案1】:

    您不能在正在运行(已启动)的 web 应用上修改 SecurityHandler

    这主要是由于 Servlet 初始化生命周期的性质,以及需要访问安全层及其配置的无数组件。

    您不能在事后拉出该层并对其进行更改。

    你必须打电话:

    myWebAppContext.stop();
    myWebAppContext.setSecurityHandler(mySuperDooperSecurityHandler);
    myWebAppContext.start();
    

    【讨论】:

      猜你喜欢
      • 2014-03-15
      • 1970-01-01
      • 2011-03-09
      • 2012-02-14
      • 2014-01-14
      • 1970-01-01
      • 1970-01-01
      • 2011-05-08
      • 1970-01-01
      相关资源
      最近更新 更多