gpt4 book ai didi

java - 嵌入式Jetty + ShiroFilter问题

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:59:30 28 4
gpt4 key购买 nike

我还没有在这里提出问题,但经过大约 2 个月的努力,我决定最终寻求帮助。

我是 Java 的新手,这实际上是我的第一个项目(只是为了好玩),我在其中尝试使用 Jetty(没有 web.xml 描述符)和 Shiro 身份验证(使用 shiro.ini 配置)创建一个 Web 服务器文件),但由于某种原因我无法使其工作。我尝试了几乎所有我在互联网上找到的东西,尝试重新开始,阅读所有 FM……但什么都没有……

最后的情况是我收到关于没有注册 EnvironmentListener 的错误:

javax.servlet.ServletException: java.lang.IllegalStateException: No WebEnvironment found: no EnvironmentLoaderListener registered?
at org.apache.shiro.web.servlet.AbstractFilter.init(AbstractFilter.java:105)
at org.eclipse.jetty.servlet.FilterHolder.initialize(FilterHolder.java:138)
at org.eclipse.jetty.servlet.ServletHandler.initialize(ServletHandler.java:852)
at org.eclipse.jetty.servlet.ServletContextHandler.startContext(ServletContextHandler.java:341)
at org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:742)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:132)
at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:114)
at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:61)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:132)
at org.eclipse.jetty.server.Server.start(Server.java:399)
at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:114)
at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:61)
at org.eclipse.jetty.server.Server.doStart(Server.java:366)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
at MyPackage.Main.main(Main.java:89)

所以在大量阅读之后,我最终添加了以下代码:

EnvironmentLoaderListener listener = new EnvironmentLoaderListener();
secured.callContextInitialized(listener, new ServletContextEvent(secured.getServletContext()));

我之前也有一个不同的错误,与未设置 SecurityManager(或其他)有关,但出于某种原因,我现在没有收到该错误?

为了修复它,我使用了那个代码(它现在被注释了,显然我不再需要它了,但无论如何,在这一点上它是否被注释都没有区别......)

    //Factory<SecurityManager> factory=new IniSecurityManagerFactory("src/main/resources/shiro.ini");
//SecurityManager securityManager=factory.getInstance();
//SecurityUtils.setSecurityManager(securityManager);

所以毕竟,我最终遇到了这个错误:

Exception in thread "main" java.lang.NullPointerException
at org.eclipse.jetty.server.handler.ContextHandler$Context.log(ContextHandler.java:2059)
at org.apache.shiro.web.env.EnvironmentLoader.initEnvironment(EnvironmentLoader.java:127)
at org.apache.shiro.web.env.EnvironmentLoaderListener.contextInitialized(EnvironmentLoaderListener.java:58)
at org.eclipse.jetty.server.handler.ContextHandler.callContextInitialized(ContextHandler.java:801)
at org.eclipse.jetty.servlet.ServletContextHandler.callContextInitialized(ServletContextHandler.java:499)
at MyPackage.Main.main(Main.java:72)

这是我的代码,我想它有很多需要优化的地方,但由于我是 Java 的新手,我愿意接受任何建议...

import java.util.EnumSet;
import javax.servlet.DispatcherType;
import javax.servlet.ServletContextEvent;
import org.apache.jasper.servlet.JspServlet;
import org.apache.log4j.PropertyConfigurator;
import org.apache.shiro.web.env.EnvironmentLoaderListener;
import org.apache.shiro.web.servlet.ShiroFilter;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.HandlerCollection;
import org.eclipse.jetty.servlet.DefaultServlet;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;

public class Main {

public static void main(String[] args) {
System.out.println("Initializing server...");
PropertyConfigurator.configure("config//log4j.properties");

final ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);

context.setContextPath("/");
context.setResourceBase("src/main/webapp/main");
context.setClassLoader(Thread.currentThread().getContextClassLoader());

context.addServlet(DefaultServlet.class, "/*");

context.addServlet("MyPackage.CheckLogin", "/checklogin");

final ServletHolder jsp = context.addServlet(JspServlet.class, "*.jsp");
jsp.setInitParameter("classpath", context.getClassPath());

final ServletContextHandler secured = new ServletContextHandler(ServletContextHandler.SESSIONS);

secured.setContextPath("/protected");
secured.setResourceBase("src/main/webapp/protected");
secured.setClassLoader(Thread.currentThread().getContextClassLoader());

secured.addServlet(DefaultServlet.class, "/*");

final ServletHolder jsp2 = secured.addServlet(JspServlet.class, "*.jsp");
jsp2.setInitParameter("classpath", context.getClassPath());

EnvironmentLoaderListener listener = new EnvironmentLoaderListener();
secured.callContextInitialized(listener, new ServletContextEvent(secured.getServletContext()));

//Factory<SecurityManager> factory=new IniSecurityManagerFactory("src/main/resources/shiro.ini");
//SecurityManager securityManager=factory.getInstance();
//SecurityUtils.setSecurityManager(securityManager);

secured.addFilter(ShiroFilter.class,"/protected/*",EnumSet.of(DispatcherType.REQUEST,DispatcherType.FORWARD,DispatcherType.INCLUDE,DispatcherType.ERROR));

HandlerCollection hc = new HandlerCollection();
hc.addHandler(secured);
hc.addHandler(context);

final Server server = new Server(8080);
server.setHandler(hc);

System.out.println("Starting server...");
try {
server.start();
} catch(Exception e) {
System.out.println("Failed to start server!");
e.printStackTrace();
return;
}

System.out.println("Server running...");
while(true) {
try {
server.join();
} catch(InterruptedException e) {
System.out.println("Server interrupted!");
}
}
}
}

我几乎尝试了所有我能想到的方法,所以请帮我修复它,因为它现在快把我逼疯了......

如果需要任何其他信息,请告诉我,我会提供:)

编辑:我试图遍历堆栈跟踪中列出的所有类,但在花了几个小时阅读其中的不同 Java 类和函数后,我陷入了死胡同...

Jetty jar 版本:jetty-all-9.3.0-M2

最佳答案

我意识到这是一个老问题,但我最近遇到了类似的问题。根据您的代码,我最终改变了您创建 EnvironmentLoaderListener 的方式:

EnvironmentLoaderListener listener = new EnvironmentLoaderListener();
secured.callContextInitialized(listener, new ServletContextEvent(secured.getServletContext()));

secured.addFilter(ShiroFilter.class,"/protected/*",EnumSet.of(DispatcherType.REQUEST,DispatcherType.FORWARD,DispatcherType.INCLUDE,DispatcherType.ERROR));

到:

//Setup the environment loader listener to initialize Shiro and indicate where our ini file is
servletContextHandler.setInitParameter("shiroConfigLocations", "classpath:shiro.ini");
secured.addEventListener(new EnvironmentLoaderListener());

//Create and init the Shiro filter that will lookup and use environment we just created
secured.addFilter(
"org.apache.shiro.web.servlet.IniShiroFilter", "/*", EnumSet.of(
DispatcherType.INCLUDE, DispatcherType.REQUEST,
DispatcherType.FORWARD, DispatcherType.ERROR));

希望这对您有所帮助!

关于java - 嵌入式Jetty + ShiroFilter问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30368965/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com