在 Tomcat 中从 HttpServletRequest.getRemoteUser() 获取值而不修改应用程序

发布于 2024-12-06 09:35:52 字数 339 浏览 1 评论 0原文

(使用Java 6和Tomcat 6。)

有没有办法让我在我的开发环境(即本地主机)中让HttpServletRequest.getRemoteUser()返回一个值,而无需修改我的应用程序的web.xml文件?

我问的原因是应用程序部署到远程环境时的身份验证实现是由 Web 服务器和插件工具处理的。在本地运行我显然没有插件工具或单独的网络服务器;我只有 Tomcat 6。我试图避免仅仅为了支持本地主机上的开发而向我的应用程序添加代码。

我希望可以对 context.xml 或 server.xml 文件进行修改,让我设置远程用户 ID,或者尝试从 HTTP 标头或其他内容中提取它。

(Using Java 6 and Tomcat 6.)

Is there a way for me to get HttpServletRequest.getRemoteUser() to return a value in my development environment (i.e. localhost) without needing to modify my application's web.xml file?

The reason I ask is that the authentication implementation when the app is deployed to a remote environment is handled by a web server and plugged-in tool. Running locally I obviously do not have the plugged-in tool or a separate web server; I just have Tomcat 6. I am trying to avoid adding code to my application merely to support development on my localhost.

I am hoping there is a modification I can make to the context.xml or server.xml files that will let me set the remote user ID or that will try to pull it from a HTTP header or something.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烏雲後面有陽光 2024-12-13 09:35:52

下面是一个概念验证 Valve 实现:(

import java.io.IOException;
import java.security.Principal;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;

import org.apache.catalina.connector.Request;
import org.apache.catalina.connector.Response;
import org.apache.catalina.realm.GenericPrincipal;
import org.apache.catalina.valves.ValveBase;

public class RemoteUserValve extends ValveBase {

    public RemoteUserValve() {
    }

    @Override
    public void invoke(final Request request, final Response response)
            throws IOException, ServletException {
        final String username = "myUser";
        final String credentials = "credentials";
        final List<String> roles = new ArrayList<String>();

            // Tomcat 7 version
        final Principal principal = new GenericPrincipal(username, 
                            credentials, roles);
            // Tomcat 6 version:
            // final Principal principal = new GenericPrincipal(null, 
            //              username, credentials, roles);


        request.setUserPrincipal(principal);

        getNext().invoke(request, response);
    }

}

使用 Tomcat 7.0.21 进行测试。)

编译它,将其放入 jar 中并将该 jar 复制到 apache-tomcat-7.0 .21/lib 文件夹。您需要修改 server.xml

<Host name="localhost"  appBase="webapps"
    unpackWARs="true" autoDeploy="true">

    <Valve className="remoteuservalve.RemoteUserValve" />
...

我想它也可以在 EngineContext 容器内工作。

更多信息:

Here is a proof of concept Valve implementation which does it:

import java.io.IOException;
import java.security.Principal;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;

import org.apache.catalina.connector.Request;
import org.apache.catalina.connector.Response;
import org.apache.catalina.realm.GenericPrincipal;
import org.apache.catalina.valves.ValveBase;

public class RemoteUserValve extends ValveBase {

    public RemoteUserValve() {
    }

    @Override
    public void invoke(final Request request, final Response response)
            throws IOException, ServletException {
        final String username = "myUser";
        final String credentials = "credentials";
        final List<String> roles = new ArrayList<String>();

            // Tomcat 7 version
        final Principal principal = new GenericPrincipal(username, 
                            credentials, roles);
            // Tomcat 6 version:
            // final Principal principal = new GenericPrincipal(null, 
            //              username, credentials, roles);


        request.setUserPrincipal(principal);

        getNext().invoke(request, response);
    }

}

(Tested with Tomcat 7.0.21.)

Compile it, put it inside a jar and copy the jar to the apache-tomcat-7.0.21/lib folder. You need to modify the server.xml:

<Host name="localhost"  appBase="webapps"
    unpackWARs="true" autoDeploy="true">

    <Valve className="remoteuservalve.RemoteUserValve" />
...

I suppose it works inside the Engine and Context containers too.

More information:

生活了然无味 2024-12-13 09:35:52

使用本地、基于文件的领域进行测试。检查您的 conf/tomcat-users.xml 并为您的应用程序创建角色和用户,并在 web.xml 中启用安全约束。 tomcat-users.xml 中有很好的示例。

Use a local, file-based realm for testing. Check your conf/tomcat-users.xml and create roles and users for your application and enable the security constraints in your web.xml. There are good examples in the tomcat-users.xml.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文