Commits

conanca committed ae5baf1

增加shiro jsp标签的支持

  • Participants
  • Parent commits cf0f55a

Comments (0)

Files changed (15)

File src/main/java/com/dolplay/nutzshiro/filter/AuthenticationFilter.java

 
 import javax.servlet.ServletRequest;
 import javax.servlet.ServletResponse;
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
 
 import org.apache.shiro.authc.AuthenticationException;
 import org.apache.shiro.authc.AuthenticationToken;
 import org.apache.shiro.authc.UsernamePasswordToken;
 import org.apache.shiro.mgt.SecurityManager;
 import org.apache.shiro.subject.Subject;
+import org.apache.shiro.util.ThreadContext;
 import org.apache.shiro.web.util.WebUtils;
 import org.nutz.lang.Strings;
 import org.nutz.mvc.ActionContext;
 		}
 		try {
 			Subject subject = MvcUtils.getSubject(securityManager, request, response);
+			ThreadContext.bind(subject);
 			subject.login(token);
 			return onLoginSuccess(token, subject, request, response);
 		} catch (AuthenticationException e) {

File src/main/java/com/dolplay/nutzshiro/filter/AuthorizationFilter.java

 
 import java.util.List;
 
-import org.apache.shiro.mgt.SecurityManager;
+import org.apache.shiro.SecurityUtils;
 import org.apache.shiro.subject.PrincipalCollection;
 import org.apache.shiro.subject.Subject;
 import org.nutz.lang.Lang;
 import org.slf4j.LoggerFactory;
 
 import com.dolplay.nutzshiro.annotation.Authorization;
-import com.dolplay.nutzshiro.util.MvcUtils;
 
 public class AuthorizationFilter implements ActionFilter {
 	final static Logger logger = LoggerFactory.getLogger(AuthorizationFilter.class);
 
-	private SecurityManager securityManager;
-
 	private static final View UNAUTH = new HttpStatusView(403);
 
 	@Override
 			return null;
 		}
 
-		Subject currentUser = MvcUtils.getSubject(securityManager, actionContext.getRequest(),
-				actionContext.getResponse());
+		Subject currentUser = SecurityUtils.getSubject();
 
 		if (a.requiresUser()) {
 			if (!currentUser.isAuthenticated()) {
 			}
 		}
 		String requiresPermissionsStr = a.requiresPermissions();
-		if(!Strings.isEmpty(requiresPermissionsStr)){
+		if (!Strings.isEmpty(requiresPermissionsStr)) {
 			String[] requiresPermissions = requiresPermissionsStr.split(",");
 			if (!(currentUser.isPermittedAll(requiresPermissions))) {
 				logger.info("用户未拥有该操作所需的所有权限,无法执行操作!");

File src/main/java/com/dolplay/nutzshiro/filter/LogoutFilter.java

 package com.dolplay.nutzshiro.filter;
 
-import javax.servlet.ServletRequest;
-import javax.servlet.ServletResponse;
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-
-import org.apache.shiro.mgt.SecurityManager;
+import org.apache.shiro.SecurityUtils;
 import org.apache.shiro.session.SessionException;
 import org.apache.shiro.subject.Subject;
-import org.apache.shiro.web.util.WebUtils;
 import org.nutz.lang.Strings;
 import org.nutz.mvc.ActionContext;
 import org.nutz.mvc.ActionFilter;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import com.dolplay.nutzshiro.util.MvcUtils;
-
 public class LogoutFilter implements ActionFilter {
 	private static Logger logger = LoggerFactory.getLogger(LogoutFilter.class);
 
 	public static final String DEFAULT_LOGOUT_URL = "/logout";
 	private String logoutUrl = DEFAULT_LOGOUT_URL;
-	private SecurityManager securityManager;
 
 	@Override
 	public View match(ActionContext actionContext) {
 		String path = actionContext.getPath();
 		if (!Strings.isEmpty(path) && path.equals(logoutUrl)) {
 
-			Subject currentUser = MvcUtils.getSubject(securityManager, actionContext.getRequest(),
-					actionContext.getResponse());
+			Subject currentUser = SecurityUtils.getSubject();
 			try {
 				currentUser.logout();
 			} catch (SessionException ise) {

File src/main/java/com/dolplay/nutzshiro/module/SystemModule.java

 public class SystemModule {
 
 	@At("/login")
-	@Ok("redirect:/user/all")
+	@Ok(">>:/")
 	@Filters({ @By(type = AuthenticationFilter.class, args = { "ioc:authenticationFilter" }) })
 	public void login() {
 
 	}
 	
 	@At("/logout")
-	@Ok("redirect:/")
+	@Ok(">>:/")
 	@Filters({ @By(type = AuthenticationFilter.class, args = { "ioc:logoutFilter" }) })
 	public void logout() {
 

File src/main/resources/shiro.js

 
 	authorizationFilter : {
 		type : "com.dolplay.nutzshiro.filter.AuthorizationFilter",
-		fields : {
-			securityManager : {
-				refer : "securityManager"
-			}
-		}
 	},
 
 	logoutFilter : {
 		type : "com.dolplay.nutzshiro.filter.LogoutFilter",
-		fields : {
-			securityManager : {
-				refer : "securityManager"
-			}
-		}
 	}
 };

File src/main/webapp/WEB-INF/jsp/permission_add.jsp

 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
 <%@ page isELIgnored="false" %>
 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
+<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
 <!DOCTYPE html>
 <html lang="en">
 <head>
             <li class="active"><a href="permission/all">权限管理</a></li>
           </ul>
           <ul class="nav pull-right">
+            <li><a>欢迎, <shiro:principal/></a></li>
             <li><a href="logout">登出</a></li>
           </ul>
         </div><!--/.nav-collapse -->

File src/main/webapp/WEB-INF/jsp/permission_list.jsp

 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
 <%@ page isELIgnored="false" %>
 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
+<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
 <!DOCTYPE html>
 <html lang="en">
 <head>
             <li class="active"><a href="permission/all">权限管理</a></li>
           </ul>
           <ul class="nav pull-right">
+            <li><a>欢迎, <shiro:principal/></a></li>
             <li><a href="logout">登出</a></li>
           </ul>
         </div><!--/.nav-collapse -->

File src/main/webapp/WEB-INF/jsp/permission_view.jsp

 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
 <%@ page isELIgnored="false" %>
 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
+<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
 <!DOCTYPE html>
 <html lang="en">
 <head>
             <li class="active"><a href="permission/all">权限管理</a></li>
           </ul>
           <ul class="nav pull-right">
+            <li><a>欢迎, <shiro:principal/></a></li>
             <li><a href="logout">登出</a></li>
           </ul>
         </div><!--/.nav-collapse -->

File src/main/webapp/WEB-INF/jsp/role_add.jsp

 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
 <%@ page isELIgnored="false" %>
 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
+<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
 <!DOCTYPE html>
 <html lang="en">
 <head>
             <li><a href="permission/all">权限管理</a></li>
           </ul>
           <ul class="nav pull-right">
+            <li><a>欢迎, <shiro:principal/></a></li>
             <li><a href="logout">登出</a></li>
           </ul>
         </div><!--/.nav-collapse -->

File src/main/webapp/WEB-INF/jsp/role_list.jsp

 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
 <%@ page isELIgnored="false" %>
 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
+<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
 <!DOCTYPE html>
 <html lang="en">
 <head>
             <li><a href="permission/all">权限管理</a></li>
           </ul>
           <ul class="nav pull-right">
+            <li><a>欢迎, <shiro:principal/></a></li>
             <li><a href="logout">登出</a></li>
           </ul>
         </div><!--/.nav-collapse -->

File src/main/webapp/WEB-INF/jsp/role_view.jsp

 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
 <%@ page isELIgnored="false" %>
 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
+<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
 <!DOCTYPE html>
 <html lang="en">
 <head>
             <li><a href="permission/all">权限管理</a></li>
           </ul>
           <ul class="nav pull-right">
+            <li><a>欢迎, <shiro:principal/></a></li>
             <li><a href="logout">登出</a></li>
           </ul>
         </div><!--/.nav-collapse -->

File src/main/webapp/WEB-INF/jsp/user_add.jsp

 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
 <%@ page isELIgnored="false" %>
 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
+<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
 <!DOCTYPE html>
 <html lang="en">
 <head>
             <li><a href="permission/all">权限管理</a></li>
           </ul>
           <ul class="nav pull-right">
+            <li><a>欢迎, <shiro:principal/></a></li>
             <li><a href="logout">登出</a></li>
           </ul>
         </div><!--/.nav-collapse -->

File src/main/webapp/WEB-INF/jsp/user_list.jsp

 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
 <%@ page isELIgnored="false" %>
 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
+<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
 <!DOCTYPE html>
 <html lang="en">
 <head>
             <li><a href="permission/all">权限管理</a></li>
           </ul>
           <ul class="nav pull-right">
+            <li><a>欢迎, <shiro:principal/></a></li>
             <li><a href="logout">登出</a></li>
           </ul>
         </div><!--/.nav-collapse -->

File src/main/webapp/WEB-INF/jsp/user_view.jsp

 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
 <%@ page isELIgnored="false" %>
 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
+<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
 <!DOCTYPE html>
 <html lang="en">
 <head>
             <li><a href="permission/all">权限管理</a></li>
           </ul>
           <ul class="nav pull-right">
+            <li><a>欢迎, <shiro:principal/></a></li>
             <li><a href="logout">登出</a></li>
           </ul>
         </div><!--/.nav-collapse -->

File src/main/webapp/index.jsp

 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
 <%@ page isELIgnored="false" %>
+<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
 <!DOCTYPE html>
 <html lang="en">
 <head>
 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
-<title>登录</title>
+<title>Nutzdemo - Shiro</title>
 <link href="css/bootstrap.min.css" rel="stylesheet">
 <style>
 body {
         </a>
         <a class="brand" href="#">Nutzdemo-shiro</a>
         <div class="nav-collapse">
+          <shiro:user>
+          <ul class="nav">
+            <li><a href="user/all">用户管理</a></li>
+            <li><a href="role/all">角色管理</a></li>
+            <li><a href="permission/all">权限管理</a></li>
+          </ul>
           <ul class="nav pull-right">
-            <li class="active"><a href="#">登录</a></li>
+            <li><a>欢迎, <shiro:principal/></a></li>
+            <li><a href="logout">登出</a></li>
           </ul>
+          </shiro:user>
         </div><!--/.nav-collapse -->
       </div>
     </div>
   </div>
   <div class="container">
+  
+    <div class="hero-unit">
+      <p>这是一个结合 Nutz 使用 Shiro 进行登录验证和鉴权的 Demo</p>
+      <shiro:user>
+        <p>欢迎, <shiro:principal/></p>
+        <p>您可以通过点击顶部导航,进行不同的操作</p>
+      </shiro:user>
+      <shiro:guest>
+        <p>请您先登录</p>
+      </shiro:guest>
+    </div>
+  
+  <shiro:guest>
   <form action="login" class="form-vertical">
     <fieldset>
       <legend>请登录</legend>
       </div>
     </fieldset>
   </form>
+  </shiro:guest>
   </div>
   <script src="js/jquery-1.7.2.min.js"></script>
   <script src="js/bootstrap.min.js"></script>