SpringMVC+Shiro权限管理

SpringMVC+Shiro权限管理

 

 什么是权限呢?举个简单的例子:

我有一个论坛,注册的用户分为normal用户,manager用户。
对论坛的帖子的操作有这些:
添加,删除,更新,查看,回复
我们规定:
normal用户只能:添加,查看,回复
manager用户可以:删除,更新

normal,manager对应的是角色(role)
添加,删除,更新等对应的是权限(permission)

我们采用下面的逻辑创建权限表结构(不是绝对的,根据需要修改)

一个用户可以有多种角色(normal,manager,admin等等)
一个角色可以有多个用户(user1,user2,user3等等)
一个角色可以有多个权限(save,update,delete,query等等)
一个权限只属于一个角色(delete只属于manager角色)



 

 我们创建四张表:
t_user用户表:设置了3个用户
-------------------------------
id + username   + password
---+----------------+----------
1  +   tom           +  000000
2  +   jack           +  000000
3  +   rose          +  000000
---------------------------------
t_role角色表:设置3个角色
--------------
id + rolename 
---+----------
1  + admin
2  + manager
3  + normal
--------------
t_user_role用户角色表:tom是admin和normal角色,jack是manager和normal角色,rose是normal角色
---------------------
user_id  +  role_id
-----------+-----------
1            +     1
1            +     3
2            +     2
2            +     3
3            +     3
---------------------
t_permission权限表:admin角色可以删除,manager角色可以添加和更新,normal角色可以查看
-----------------------------------
id  +  permissionname  +  role_id
----+------------------------+-----------
1   +   add                     +     2
2   +   del                       +    1
3   +   update                +     2
4   +   query                   +    3
-----------------------------------

 

 建立对应的POJO:

Java代码  
  1. package com.cn.pojo;  
  2.   
  3. import java.util.HashSet;  
  4. import java.util.List;  
  5. import java.util.Set;  
  6.   
  7. import javax.persistence.Entity;  
  8. import javax.persistence.GeneratedValue;  
  9. import javax.persistence.GenerationType;  
  10. import javax.persistence.Id;  
  11. import javax.persistence.JoinColumn;  
  12. import javax.persistence.JoinTable;  
  13. import javax.persistence.ManyToMany;  
  14. import javax.persistence.Table;  
  15. import javax.persistence.Transient;  
  16.   
  17. import org.hibernate.validator.constraints.NotEmpty;  
  18.   
  19. @Entity  
  20. @Table(name="t_user")  
  21. public class User {  
  22.   
  23.     private Integer id;  
  24.     @NotEmpty(message="用户名不能为空")  
  25.     private String username;  
  26.     @NotEmpty(message="密码不能为空")  
  27.     private String password;  
  28.     private List roleList;//一个用户具有多个角色  
  29.       
  30.     @Id  
  31.     @GeneratedValue(strategy=GenerationType.IDENTITY)  
  32.     public Integer getId() {  
  33.         return id;  
  34.     }  
  35.     public void setId(Integer id) {  
  36.         this.id = id;  
  37.     }  
  38.     public String getUsername() {  
  39.         return username;  
  40.     }  
  41.     public void setUsername(String username) {  
  42.         this.username = username;  
  43.     }  
  44.     public String getPassword() {  
  45.         return password;  
  46.     }  
  47.     public void setPassword(String password) {  
  48.         this.password = password;  
  49.     }  
  50.     @ManyToMany  
  51.     @JoinTable(name="t_user_role",joinColumns={@JoinColumn(name="user_id")},inverseJoinColumns={@JoinColumn(name="role_id")})  
  52.     public List getRoleList() {  
  53.         return roleList;  
  54.     }  
  55.     public void setRoleList(List roleList) {  
  56.         this.roleList = roleList;  
  57.     }  
  58.       
  59.     @Transient  
  60.     public Set getRolesName(){  
  61.         List roles=getRoleList();  
  62.         Set set=new HashSet();  
  63.         for (Role role : roles) {  
  64.             set.add(role.getRolename());  
  65.         }  
  66.         return set;  
  67.     }  
  68.       
  69. }  

 

Java代码  
  1. package com.cn.pojo;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. import javax.persistence.Entity;  
  7. import javax.persistence.GeneratedValue;  
  8. import javax.persistence.GenerationType;  
  9. import javax.persistence.Id;  
  10. import javax.persistence.JoinColumn;  
  11. import javax.persistence.JoinTable;  
  12. import javax.persistence.ManyToMany;  
  13. import javax.persistence.OneToMany;  
  14. import javax.persistence.Table;  
  15. import javax.persistence.Transient;  
  16.   
  17. @Entity  
  18. @Table(name="t_role")  
  19. public class Role {  
  20.   
  21.     private Integer id;  
  22.     private String rolename;  
  23.     private List permissionList;//一个角色对应多个权限  
  24.     private List userList;//一个角色对应多个用户  
  25.       
  26.     @Id  
  27.     @GeneratedValue(strategy=GenerationType.IDENTITY)  
  28.     public Integer getId() {  
  29.         return id;  
  30.     }  
  31.     public void setId(Integer id) {  
  32.         this.id = id;  
  33.     }  
  34.     public String getRolename() {  
  35.         return rolename;  
  36.     }  
  37.     public void setRolename(String rolename) {  
  38.         this.rolename = rolename;  
  39.     }  
  40.     @OneToMany(mappedBy="role")  
  41.     public List getPermissionList() {  
  42.         return permissionList;  
  43.     }  
  44.     public void setPermissionList(List permissionList) {  
  45.         this.permissionList = permissionList;  
  46.     }  
  47.     @ManyToMany  
  48.     @JoinTable(name="t_user_role",joinColumns={@JoinColumn(name="role_id")},inverseJoinColumns={@JoinColumn(name="user_id")})  
  49.     public List getUserList() {  
  50.         return userList;  
  51.     }  
  52.     public void setUserList(List userList) {  
  53.         this.userList = userList;  
  54.     }  
  55.       
  56.     @Transient  
  57.     public List getPermissionsName(){  
  58.         List list=new ArrayList();  
  59.         List perlist=getPermissionList();  
  60.         for (Permission per : perlist) {  
  61.             list.add(per.getPermissionname());  
  62.         }  
  63.         return list;  
  64.     }  
  65. }  

 

Java代码  
  1. package com.cn.pojo;  
  2.   
  3. import javax.persistence.Entity;  
  4. import javax.persistence.GeneratedValue;  
  5. import javax.persistence.GenerationType;  
  6. import javax.persistence.Id;  
  7. import javax.persistence.JoinColumn;  
  8. import javax.persistence.ManyToOne;  
  9. import javax.persistence.Table;  
  10.   
  11. @Entity  
  12. @Table(name="t_permission")  
  13. public class Permission {  
  14.   
  15.     private Integer id;  
  16.     private String permissionname;  
  17.     private Role role;//一个权限对应一个角色  
  18.       
  19.     @Id  
  20.     @GeneratedValue(strategy=GenerationType.IDENTITY)  
  21.     public Integer getId() {  
  22.         return id;  
  23.     }  
  24.     public void setId(Integer id) {  
  25.         this.id = id;  
  26.     }  
  27.     public String getPermissionname() {  
  28.         return permissionname;  
  29.     }  
  30.     public void setPermissionname(String permissionname) {  
  31.         this.permissionname = permissionname;  
  32.     }  
  33.     @ManyToOne  
  34.     @JoinColumn(name="role_id")  
  35.     public Role getRole() {  
  36.         return role;  
  37.     }  
  38.     public void setRole(Role role) {  
  39.         this.role = role;  
  40.     }  
  41.       
  42. }  

 

 使用SHIRO的步骤:
1,导入jar
2,配置web.xml
3,建立dbRelm
4,在Spring中配置

pom.xml中配置如下:

Xml代码  
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">  
  3.   <modelVersion>4.0.0modelVersion>  
  4.   <groupId>com.hyxgroupId>  
  5.   <artifactId>springmvcartifactId>  
  6.   <packaging>warpackaging>  
  7.   <version>0.0.1-SNAPSHOTversion>  
  8.   <name>springmvc Maven Webappname>  
  9.   <url>http://maven.apache.orgurl>  
  10.   <dependencies>  
  11.     <dependency>  
  12.       <groupId>junitgroupId>  
  13.       <artifactId>junitartifactId>  
  14.       <version>3.8.1version>  
  15.       <scope>testscope>  
  16.     dependency>  
  17.       
  18.     <dependency>  
  19.         <groupId>org.springframeworkgroupId>  
  20.         <artifactId>spring-webmvcartifactId>  
  21.         <version>3.2.4.RELEASEversion>  
  22.     dependency>  
  23.       
  24.     <dependency>  
  25.         <groupId>org.springframeworkgroupId>  
  26.         <artifactId>spring-jdbcartifactId>  
  27.         <version>3.2.4.RELEASEversion>  
  28.     dependency>  
  29.     <dependency>  
  30.         <groupId>org.springframeworkgroupId>  
  31.         <artifactId>spring-ormartifactId>  
  32.         <version>3.2.4.RELEASEversion>  
  33.     dependency>  
  34.       
  35.       
  36.     <dependency>  
  37.         <groupId>org.hibernategroupId>  
  38.         <artifactId>hibernate-coreartifactId>  
  39.         <version>4.2.5.Finalversion>  
  40.     dependency>  
  41.     <dependency>  
  42.         <groupId>org.hibernategroupId>  
  43.         <artifactId>hibernate-ehcacheartifactId>  
  44.         <version>4.2.5.Finalversion>  
  45.     dependency>  
  46.     <dependency>  
  47.         <groupId>net.sf.ehcachegroupId>  
  48.         <artifactId>ehcacheartifactId>  
  49.         <version>2.7.2version>  
  50.     dependency>  
  51.     <dependency>  
  52.         <groupId>commons-dbcpgroupId>  
  53.         <artifactId>commons-dbcpartifactId>  
  54.         <version>1.4version>  
  55.     dependency>  
  56.     <dependency>  
  57.         <groupId>mysqlgroupId>  
  58.         <artifactId>mysql-connector-javaartifactId>  
  59.         <version>5.1.26version>  
  60.     dependency>  
  61.       
  62.     <dependency>  
  63.         <groupId>javax.injectgroupId>  
  64.         <artifactId>javax.injectartifactId>  
  65.         <version>1version>  
  66.     dependency>          
  67.       
  68.       
  69.       
  70.     <dependency>  
  71.         <groupId>org.hibernategroupId>  
  72.         <artifactId>hibernate-validatorartifactId>  
  73.         <version>5.0.1.Finalversion>  
  74.     dependency>  
  75.       
  76.     <dependency>  
  77.         <groupId>org.codehaus.jacksongroupId>  
  78.         <artifactId>jackson-mapper-aslartifactId>  
  79.         <version>1.9.13version>  
  80.     dependency>          
  81.       
  82.     <dependency>  
  83.         <groupId>javax.servletgroupId>  
  84.         <artifactId>jstlartifactId>  
  85.         <version>1.2version>  
  86.     dependency>  
  87.       
  88.     <dependency>  
  89.       <groupId>javax.servletgroupId>  
  90.       <artifactId>servlet-apiartifactId>  
  91.       <version>2.5version>  
  92.     dependency>  
  93.       
  94.         
  95.     <dependency>    
  96.       <groupId>org.apache.shirogroupId>    
  97.       <artifactId>shiro-coreartifactId>    
  98.       <version>1.2.2version>    
  99.     dependency>    
  100.     <dependency>    
  101.       <groupId>org.apache.shirogroupId>    
  102.       <artifactId>shiro-webartifactId>    
  103.       <version>1.2.2version>    
  104.     dependency>    
  105.     <dependency>    
  106.       <groupId>org.apache.shirogroupId>    
  107.       <artifactId>shiro-springartifactId>    
  108.       <version>1.2.2version>    
  109.     dependency>   
  110.   dependencies>  
  111.     
  112.   <build>  
  113.     <finalName>springmvcfinalName>  
  114.       
  115.     <plugins>  
  116.         <plugin>  
  117.           <groupId>org.mortbay.jettygroupId>  
  118.           <artifactId>jetty-maven-pluginartifactId>  
  119.           <configuration>  
  120.             <scanIntervalSeconds>10scanIntervalSeconds>  
  121.             <webApp>  
  122.               <contextPath>/contextPath>  
  123.             webApp>  
  124.               
  125.             <connectors>  
  126.                <connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">  
  127.                   <port>80port>  
  128.                   <maxIdleTime>60000maxIdleTime>  
  129.                connector>  
  130.             connectors>  
  131.           configuration>  
  132.         plugin>  
  133.     plugins>  
  134.   build>  
  135. project>  

 

 web.xml中的配置:

Xml代码  
  1. xml version="1.0" encoding="UTF-8" ?>  
  2. <web-app version="2.5"   
  3.     xmlns="http://java.sun.com/xml/ns/javaee"   
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  5.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
  6.     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
  7.   <display-name>Archetype Created Web Applicationdisplay-name>  
  8.     
  9.     
  10.   <filter>  
  11.     <filter-name>opensessioninviewfilter-name>  
  12.     <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilterfilter-class>  
  13.   filter>  
  14.   <filter-mapping>  
  15.     <filter-name>opensessioninviewfilter-name>  
  16.     <url-pattern>/*url-pattern>  
  17.   filter-mapping>  
  18.     
  19.     
  20.   <servlet>  
  21.     <servlet-name>springmvcservlet-name>  
  22.     <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>  
  23.     <load-on-startup>1load-on-startup>  
  24.   servlet>  
  25.   <servlet-mapping>  
  26.     <servlet-name>springmvcservlet-name>  
  27.       
  28.     <url-pattern>/url-pattern>  
  29.   servlet-mapping>  
  30.     
  31.     
  32.   <context-param>  
  33.     <param-name>contextConfigLocationparam-name>  
  34.     <param-value>classpath:applicationContext*.xmlparam-value>  
  35.   context-param>  
  36.   <listener>  
  37.     <listener-class>  
  38.         org.springframework.web.context.ContextLoaderListener  
  39.     listener-class>  
  40.   listener>  
  41.     
  42.       
  43.   <filter>    
  44.     <filter-name>shiroFilterfilter-name>    
  45.     <filter-class>org.springframework.web.filter.DelegatingFilterProxyfilter-class>    
  46.   filter>    
  47.   <filter-mapping>    
  48.     <filter-name>shiroFilterfilter-name>    
  49.     <url-pattern>/*url-pattern>    
  50.   filter-mapping>  
  51.     
  52. web-app>  

 

 

Java代码  
  1. package com.cn.service;  
  2.   
  3. import java.util.List;  
  4.   
  5. import javax.inject.Inject;  
  6.   
  7. import org.apache.shiro.authc.AuthenticationException;  
  8. import org.apache.shiro.authc.AuthenticationInfo;  
  9. import org.apache.shiro.authc.AuthenticationToken;  
  10. import org.apache.shiro.authc.SimpleAuthenticationInfo;  
  11. import org.apache.shiro.authc.UsernamePasswordToken;  
  12. import org.apache.shiro.authz.AuthorizationInfo;  
  13. import org.apache.shiro.authz.SimpleAuthorizationInfo;  
  14. import org.apache.shiro.realm.AuthorizingRealm;  
  15. import org.apache.shiro.subject.PrincipalCollection;  
  16. import org.springframework.stereotype.Service;  
  17. import org.springframework.transaction.annotation.Transactional;  
  18.   
  19. import com.cn.pojo.Role;  
  20. import com.cn.pojo.User;  
  21.   
  22. @Service  
  23. @Transactional  
  24. public class MyShiro extends AuthorizingRealm{  
  25.   
  26.     @Inject  
  27.     private UserService userService;  
  28.     /** 
  29.      * 权限认证 
  30.      */  
  31.     @Override  
  32.     protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {  
  33.         //获取登录时输入的用户名  
  34.         String loginName=(String) principalCollection.fromRealm(getName()).iterator().next();  
  35.         //到数据库查是否有此对象  
  36.         User user=userService.findByName(loginName);  
  37.         if(user!=null){  
  38.             //权限信息对象info,用来存放查出的用户的所有的角色(role)及权限(permission)  
  39.             SimpleAuthorizationInfo info=new SimpleAuthorizationInfo();  
  40.             //用户的角色集合  
  41.             info.setRoles(user.getRolesName());  
  42.             //用户的角色对应的所有权限,如果只使用角色定义访问权限,下面的四行可以不要  
  43.             List roleList=user.getRoleList();  
  44.             for (Role role : roleList) {  
  45.                 info.addStringPermissions(role.getPermissionsName());  
  46.             }  
  47.             return info;  
  48.         }  
  49.         return null;  
  50.     }  
  51.   
  52.     /** 
  53.      * 登录认证; 
  54.      */  
  55.     @Override  
  56.     protected AuthenticationInfo doGetAuthenticationInfo(  
  57.             AuthenticationToken authenticationToken) throws AuthenticationException {  
  58.         //UsernamePasswordToken对象用来存放提交的登录信息  
  59.         UsernamePasswordToken token=(UsernamePasswordToken) authenticationToken;  
  60.         //查出是否有此用户  
  61.         User user=userService.findByName(token.getUsername());  
  62.         if(user!=null){  
  63.             //若存在,将此用户存放到登录认证info中  
  64.             return new SimpleAuthenticationInfo(user.getUsername(), user.getPassword(), getName());  
  65.         }  
  66.         return null;  
  67.     }  
  68.   
  69. }  

 

 在spring的配置文件中配置,为了区别spring原配置和shiro我们将shiro的配置独立出来。

applicationContext-shiro.xml

Xml代码  
  1. xml version="1.0" encoding="UTF-8" ?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.        xmlns:aop="http://www.springframework.org/schema/aop"  
  5.        xmlns:tx="http://www.springframework.org/schema/tx"  
  6.        xmlns:context="http://www.springframework.org/schema/context"  
  7.        xsi:schemaLocation="  
  8. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
  9. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd  
  10. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd  
  11. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">  
  12.   
  13.       
  14.     <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">    
  15.           
  16.         <property name="realm" ref="myShiro"/>    
  17.           
  18.         <property name="cacheManager" ref="cacheManager"/>    
  19.     bean>  
  20.       
  21.       
  22.     <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">   
  23.            
  24.         <property name="securityManager" ref="securityManager"/>   
  25.            
  26.         <property name="loginUrl" value="/login"/>    
  27.           
  28.         <property name="successUrl" value="/user"/>    
  29.           
  30.         <property name="unauthorizedUrl" value="/403"/>    
  31.           
  32.         <property name="filterChainDefinitions">    
  33.             <value>    
  34.                   
  35.                 /static/**=anon  
  36.                   
  37.                 /user=perms[user:query]  
  38.                   
  39.                 /user/add=roles[manager]  
  40.                 /user/del/**=roles[admin]  
  41.                 /user/edit/**=roles[manager]  
  42.                     
  43.                 /** = authc  
  44.             value>    
  45.         property>    
  46.     bean>  
  47.       
  48.       
  49.     <bean id="cacheManager" class="org.apache.shiro.cache.MemoryConstrainedCacheManager" />    
  50.     <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor" />   
  51.       
  52. beans>  

 

 用于登录,登出,权限跳转的控制:

Java代码  
  1. package com.cn.controller;  
  2.   
  3. import javax.validation.Valid;  
  4.   
  5. import org.apache.shiro.SecurityUtils;  
  6. import org.apache.shiro.authc.AuthenticationException;  
  7. import org.apache.shiro.authc.UsernamePasswordToken;  
  8. import org.springframework.stereotype.Controller;  
  9. import org.springframework.ui.Model;  
  10. import org.springframework.validation.BindingResult;  
  11. import org.springframework.web.bind.annotation.RequestMapping;  
  12. import org.springframework.web.bind.annotation.RequestMethod;  
  13. import org.springframework.web.servlet.mvc.support.RedirectAttributes;  
  14.   
  15. import com.cn.pojo.User;  
  16.   
  17. @Controller  
  18. public class HomeController {  
  19.   
  20.     @RequestMapping(value="/login",method=RequestMethod.GET)  
  21.     public String loginForm(Model model){  
  22.         model.addAttribute("user"new User());  
  23.         return "/login";  
  24.     }  
  25.       
  26.     @RequestMapping(value="/login",method=RequestMethod.POST)  
  27.     public String login(@Valid User user,BindingResult bindingResult,RedirectAttributes redirectAttributes){  
  28.         try {  
  29.             if(bindingResult.hasErrors()){  
  30.                 return "/login";  
  31.             }  
  32.             //使用权限工具进行用户登录,登录成功后跳到shiro配置的successUrl中,与下面的return没什么关系!  
  33.             SecurityUtils.getSubject().login(new UsernamePasswordToken(user.getUsername(), user.getPassword()));  
  34.             return "redirect:/user";  
  35.         } catch (AuthenticationException e) {  
  36.             redirectAttributes.addFlashAttribute("message","用户名或密码错误");  
  37.             return "redirect:/login";  
  38.         }  
  39.     }  
  40.       
  41.     @RequestMapping(value="/logout",method=RequestMethod.GET)    
  42.     public String logout(RedirectAttributes redirectAttributes ){   
  43.         //使用权限管理工具进行用户的退出,跳出登录,给出提示信息  
  44.         SecurityUtils.getSubject().logout();    
  45.         redirectAttributes.addFlashAttribute("message""您已安全退出");    
  46.         return "redirect:/login";  
  47.     }   
  48.       
  49.     @RequestMapping("/403")  
  50.     public String unauthorizedRole(){  
  51.         return "/403";  
  52.     }  
  53. }  

 

 三个主要的JSP:
login.jsp:

Html代码  
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>  
  3. >  
  4. <html>  
  5.   <head>  
  6.     <title>My JSP 'MyJsp.jsp' starting pagetitle>  
  7.   head>  
  8.     
  9.   <body>  
  10.     <h1>登录页面----${message }h1>  
  11.     <img alt="" src="/static/img/1.jpg">  
  12.     <form:form action="/login" commandName="user" method="post">  
  13.         用户名:<form:input path="username"/> <form:errors path="username" cssClass="error"/> <br/>  
  14.         密   码:<form:password path="password"/> <form:errors path="password" cssClass="error" /> <br/>  
  15.         <form:button name="button">submitform:button>  
  16.     form:form>  
  17.   body>  
  18. html>  

 

 user.jsp:

Html代码  
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>  
  3. <%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>  
  4. >  
  5. <html>  
  6.   <head>  
  7.     <title>用户列表title>  
  8.   head>  
  9.   <body>  
  10.     <h1>${message }h1>  
  11.     <h1>用户列表--<a href="/user/add">添加用户a>---<a href="/logout">退出登录a>    h1> 
  12.     <h2>权限列表h2>  
  13.     <shiro:authenticated>用户已经登录显示此内容shiro:authenticated>  
  14.     <shiro:hasRole name="manager">manager角色登录显示此内容shiro:hasRole>  
  15.     <shiro:hasRole name="admin">admin角色登录显示此内容shiro:hasRole>  
  16.     <shiro:hasRole name="normal">normal角色登录显示此内容shiro:hasRole>  
  17.       
  18.     <shiro:hasAnyRoles name="manager,admin">**manager or admin 角色用户登录显示此内容**shiro:hasAnyRoles>  
  19.     <shiro:principal/>-显示当前登录用户名  
  20.     <shiro:hasPermission name="add">add权限用户显示此内容shiro:hasPermission>  
  21.     <shiro:hasPermission name="user:query">query权限用户显示此内容<shiro:principal/>shiro:hasPermission>  
  22.     <shiro:lacksPermission name="user:del"> 不具有user:del权限的用户显示此内容 shiro:lacksPermission>  
  23.     <ul>  
  24.         <c:forEach items="${userList }" var="user">  
  25.             <li>用户名:${user.username }----密码:${user.password }----<a href="/user/edit/${user.id}">修改用户a>----<a href="javascript:;" class="del" ref="${user.id }">删除用户a>li>  
  26.         c:forEach>  
  27.     ul>  
  28.     <img alt="" src="/static/img/1.jpg">  
  29.     <script type="text/javascript" src="http://cdn.staticfile.org/jquery/1.9.1/jquery.min.js">script>  
  30.     <script>  
  31.         $(function(){  
  32.             $(".del").click(function(){  
  33.                 var id=$(this).attr("ref");  
  34.                 $.ajax({  
  35.                     type:"delete",  
  36.                     url:"/user/del/"+id,  
  37.                     success:function(e){  
  38.                           
  39.                     }  
  40.                 });  
  41.             });  
  42.         });  
  43.     script>  
  44.   body>  
  45. html>  

 

 

 403.jsp:

Html代码  
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>  
  3. >  
  4. <html>  
  5.   <head>  
  6.     <title>权限错误title>  
  7.   head>  
  8.     
  9.   <body>  
  10.     <h1>对不起,您没有权限请求此连接!h1>  
  11.     <img alt="" src="/static/img/1.jpg">  
  12.       
  13.   body>  
  14. html>  

 

你可能感兴趣的:(java)