我还没有在这里问过问题,但经过大约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
答案 0 :(得分:1)
我意识到这是一个老问题,但我最近遇到了类似的问题。在处理代码之后,我最终改变了从以下方式创建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));
希望这有帮助!