(2013 年 1 月 2 日更新)我现在在https://github.com/AndyWi/GuiceJerseyJettyShiroExample将所有代码和 pom.xml 添加到 github (更新结束)
我正在尝试使用 Shiro 将基于表单的身份验证添加到我的简单应用程序中。该应用程序使用 Guice 3.0、Jersey 1.16 和 Shiro 1.2.1,在嵌入式 Jetty 9.0.0.M4 上运行。
我的问题是(据我所知)Shiro 需要通过 Guice 提供 login.jsp,然后添加到 Shiro 的过滤器链中。但是,当我这样做时,Jetty 找不到 login.jsp。当我从 Guice 过滤器中排除 login.jsp 时,Jetty 可以找到该 jsp,但是 Shiro 无法使用它,因此身份验证不起作用。
因此,在我的引导代码中,我使用这一行将 login.jsp 添加到 Guice 过滤器:
webAppContext.addFilter(GuiceFilter.class, "/login.jsp", null);
在我的 ShiroWebModule 中,我像这样添加 login.jsp:
addFilterChain("/login.jsp", AUTHC);
我花了很长时间寻找答案,却没有任何暗示其他人也有同样的问题——这意味着我显然做错了一些非常简单的事情!但我无法弄清楚它是什么。如果有人能帮助我克服这个问题,我将不胜感激。
我已经将我的项目简化为一个小例子来演示这个问题。这一切应该做的就是接受 /api/uuid 的 rest url,将用户重定向到 login.jsp,接受任何用户名/密码组合进行身份验证,然后从 /api/uuid 服务返回一个新的 UUID;用户还应保持登录状态以备将来请求。这是完整的代码,希望它能帮助那里的人发现问题:
引导程序:
package eg.guicejerseyjettyshiro;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.DefaultServlet;
import org.eclipse.jetty.webapp.WebAppContext;
import com.google.inject.servlet.GuiceFilter;
import eg.guicejerseyjettyshiro.modules.EgGuiceServletContextListener;
public class Bootstrap {
public static void main(String[] args) throws Exception {
Server server = new Server(8081);
WebAppContext webAppContext = new WebAppContext();
webAppContext.setContextPath("/");
webAppContext.setResourceBase("src/main/webapp/");
webAppContext.setParentLoaderPriority(true);
webAppContext.addEventListener(new EgGuiceServletContextListener());
webAppContext.addFilter(GuiceFilter.class, "/api/*", null);
// **** Shiro needs login.jsp to go through the GuiceFilter,
// but Jetty can't find the jsp when this happens. Commenting
// out this line lets Jetty find the jsp, but Shiro can't see it:
webAppContext.addFilter(GuiceFilter.class, "/login.jsp", null);
webAppContext.addServlet(DefaultServlet.class, "/");
server.setHandler(webAppContext);
server.start();
server.join();
}
}
例如GuiceServletContextListener:
package eg.guicejerseyjettyshiro.modules;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.servlet.GuiceServletContextListener;
public class EgGuiceServletContextListener extends GuiceServletContextListener {
private ServletContext servletContext;
@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
this.servletContext = servletContextEvent.getServletContext();
super.contextInitialized(servletContextEvent);
}
@Override
protected Injector getInjector() {
return Guice.createInjector(
new EgJerseyServletModule(),
new EgShiroWebModule(this.servletContext));
}
}
例如JerseyServletModule:
package eg.guicejerseyjettyshiro.modules;
import org.apache.shiro.guice.web.GuiceShiroFilter;
import com.sun.jersey.guice.JerseyServletModule;
import com.sun.jersey.guice.spi.container.servlet.GuiceContainer;
import eg.guicejerseyjettyshiro.dao.UuidDao;
import eg.guicejerseyjettyshiro.services.UuidService;
public class EgJerseyServletModule extends JerseyServletModule {
@Override
protected void configureServlets() {
bindings();
filters();
}
private void bindings() {
bind(UuidDao.class);
bind(UuidService.class);
}
private void filters() {
filter("/*").through(GuiceShiroFilter.class);
filter("/*").through(GuiceContainer.class);
}
}
例如ShiroWebModule:
package eg.guicejerseyjettyshiro.modules;
import javax.servlet.ServletContext;
import org.apache.shiro.guice.web.ShiroWebModule;
import com.google.inject.name.Names;
import eg.guicejerseyjettyshiro.realms.EgAuthorizingRealm;
public class EgShiroWebModule extends ShiroWebModule {
public EgShiroWebModule(ServletContext servletContext) {
super(servletContext);
}
@Override
protected void configureShiroWeb() {
bindConstant().annotatedWith(Names.named("shiro.globalSessionTimeout")).to(30000L);
bindRealm().to(EgAuthorizingRealm.class).asEagerSingleton();
addFilterChain("/login.jsp", AUTHC);
addFilterChain("/api/*", AUTHC);
}
}
例如授权领域:
package eg.guicejerseyjettyshiro.realms;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
public class EgAuthorizingRealm extends AuthorizingRealm {
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token)
throws AuthenticationException {
UsernamePasswordToken upToken = (UsernamePasswordToken) token;
System.out.println("In EgAuthorizingRealm.doGetAuthenticationInfo for: " + upToken.getUsername() + "/" + new String(upToken.getPassword()) + " - remember=" + upToken.isRememberMe());
return new SimpleAuthenticationInfo(upToken.getUsername(), upToken.getPassword(), getName());
}
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection pc) {
System.out.println("In EgAuthorizingRealm.doGetAuthorizationInfo");
// Doing nothing just now
return null;
}
}
Uuid服务:
package eg.guicejerseyjettyshiro.services;
import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import eg.guicejerseyjettyshiro.dao.UuidDao;
@Path("/api/uuid")
@Produces({MediaType.APPLICATION_XML})
public class UuidService {
private final UuidDao uuidDao;
@Inject
public UuidService(UuidDao uuidDao) {
this.uuidDao = uuidDao;
}
@GET
public String get() {
Subject currentUser = SecurityUtils.getSubject();
System.out.println("UuidService current user: " + currentUser.getPrincipal().toString());
return "<uuid>" + this.uuidDao.generateUuid().toString() + "</uuid>";
}
}
UuidDao:
package eg.guicejerseyjettyshiro.dao;
import java.util.UUID;
public class UuidDao {
public UUID generateUuid() {
return UUID.randomUUID();
}
}
登录.jsp:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Please Login</title>
</head>
<body>
<form name="loginForm" action="" method="post">
<table align="left" border="0" cellspacing="0" cellpadding="3">
<tr>
<td>Username:</td>
<td><input type="text" name="username" maxlength="30"></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="password" name="password" maxlength="30"></td>
</tr>
<tr>
<td colspan="2" align="left"><input type="checkbox" name="rememberMe"><font size="2">Remember Me</font></td>
</tr>
<tr>
<td colspan="2" align="right"><input type="submit" name="submit" value="Login"></td>
</tr>
</table>
</form>
</body>
</html>
好的,我想就是这样。我意识到这很长,所以非常感谢你读到这里。这一直让我发疯,所以你能给我的任何帮助将不胜感激!
谢谢,安迪。