Embedded Jetty + ShiroFilter issues
I haven't asked any questions yet, but after two months of fighting over it, I finally decided to ask for help.
I'm new to Java, this is actually my first project (just for fun) in which I am trying to create a web server with Jetty (no web.xml descriptor) and Shiro authentication (with shiro.ini config file), but by what for some reason I can't get it to work. I tried almost everything I found on the internet, tried to start everything, read all the FM radio ... But nothing ...
The end situation is that I get errors that the EnvironmentListener is not registered:
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)
So after a lot of reading, I ended up with this addition to the code:
EnvironmentLoaderListener listener = new EnvironmentLoaderListener();
secured.callContextInitialized(listener, new ServletContextEvent(secured.getServletContext()));
I also had another error before related to SecurityManager not installed (or whatever), but for some reason I am not getting this error now?
To fix it I used this code (it is commented now as apparently I no longer need it, but anyway, it doesn't matter if it is commented or not at this point ...)
//Factory<SecurityManager> factory=new IniSecurityManagerFactory("src/main/resources/shiro.ini");
//SecurityManager securityManager=factory.getInstance();
//SecurityUtils.setSecurityManager(securityManager);
So in the end I ended up with this error:
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)
This is my code, I think it has a lot of room for optimization, but since I'm new to Java I'm open to any suggestions ...
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!");
}
}
}
}
I've pretty much tried everything I could find, so please help me fix this as it drives me crazy now ...
If any further information is needed please let me know and I will provide it :)
edit: I tried to go through all the listed classes in the stack trace, but after a few hours spent reading various java classes and functions, I was stumped ...
Jetty jar version: jetty-all-9.3.0-M2
source to share
I realize this is an old question, but I recently ran into a similar issue. Working with your code, I ended up changing the way the EnvironmentLoaderListener was created:
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));
in
//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));
Hope this helps!
source to share