简体   繁体   English

启动码头后如何在嵌入式码头配置安全处理程序

[英]How to configure security handler in embedded jetty after jetty start

I am trying to configure a security handler on ServletContext in Jetty after Jetty start. 我尝试在Jetty启动后在Jetty中的ServletContext上配置安全处理程序。

Like this: 像这样:

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

But I get following exception: 但是我得到以下异常:

java.lang.IllegalStateException: STARTED java.lang.IllegalStateException:已开始

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

Why is this not possible? 为什么这不可能呢?

Screenshot: 截图:

在此处输入图片说明

EDIT: 编辑:

I looked at the source code & there it checks for isStarted flag. 我查看了源代码,并在其中检查isStarted标志。 Is it a security flaw to add security handler after jetty start?: 在码头开始后添加安全处理程序是否存在安全缺陷?:

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

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

(Reason, I have to do this is a bit complicated but I will try to explain: I am running a keycloak server behind a proxy which is reachable though my Jetty server. Let's say Jetty s running on host1 and keycloak is running on host2. But at time of setting keycloak security hanlder, whichever host is configured , keycloak allows authentication on tokens generated from that domain only. Therefore I want to configure Jetty host in security handler, which is not available until Jetty start) (原因是,我必须这样做有点复杂,但是我会尝试解释一下:我正在代理服务器后面运行一个密钥库服务器,该代理服务器可以通过我的Jetty服务器访问。假设Jetty在host1上运行,而keycloak在host2上运行。但是在设置keycloak安全处理程序(无论配置了哪个主机)时,keycloak只允许对从该域生成的令牌进行身份验证。因此,我想在安全处理程序中配置Jetty主机,直到Jetty启动后才可用)

You cannot modify the SecurityHandler on a running (started) webapp. 您无法在正在运行的(启动的)Web应用程序上修改SecurityHandler

This is mostly due to the nature of the Servlet initialization lifecycle, and the myriad of components that need access to the Security layer and its configuration. 这主要是由于Servlet初始化生命周期的性质以及需要访问安全性层及其配置的众多组件所致。

You cannot yank that layer out and change it after the fact. 您不能拉出该层并在事后更改它。

You'll have to call: 您必须致电:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM