SpringMVC+Shiro权限管理

博文目录

  1. 权限的简单描述
  2. 实例表结构及内容及POJO
  3. Shiro-pom.xml
  4. Shiro-web.xml
  5. Shiro-MyShiro-权限认证,登录认证层
  6. Shiro-applicationContext-shiro.xml
  7. HomeController
  8. 三个JSP文件

什么是权限呢?举个简单的例子:
我有一个论坛,注册的用户分为normal用户,manager用户。
对论坛的帖子的操作有这些:
添加,删除,更新,查看,回复
我们规定:
normal用户只能:添加,查看,回复
manager用户可以:删除,更新

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

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

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


SpringMVC+Shiro权限管理

我们创建四张表:
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. packagecom.cn.pojo;
  2. importjava.util.HashSet;
  3. importjava.util.List;
  4. importjava.util.Set;
  5. importjavax.persistence.Entity;
  6. importjavax.persistence.GeneratedValue;
  7. importjavax.persistence.GenerationType;
  8. importjavax.persistence.Id;
  9. importjavax.persistence.JoinColumn;
  10. importjavax.persistence.JoinTable;
  11. importjavax.persistence.ManyToMany;
  12. importjavax.persistence.Table;
  13. importjavax.persistence.Transient;
  14. importorg.hibernate.validator.constraints.NotEmpty;
  15. @Entity
  16. @Table(name="t_user")
  17. publicclassUser{
  18. privateIntegerid;
  19. @NotEmpty(message="用户名不能为空")
  20. privateStringusername;
  21. @NotEmpty(message="密码不能为空")
  22. privateStringpassword;
  23. privateList<Role>roleList;//一个用户具有多个角色
  24. @Id
  25. @GeneratedValue(strategy=GenerationType.IDENTITY)
  26. publicIntegergetId(){
  27. returnid;
  28. }
  29. publicvoidsetId(Integerid){
  30. this.id=id;
  31. }
  32. publicStringgetUsername(){
  33. returnusername;
  34. }
  35. publicvoidsetUsername(Stringusername){
  36. this.username=username;
  37. }
  38. publicStringgetPassword(){
  39. returnpassword;
  40. }
  41. publicvoidsetPassword(Stringpassword){
  42. this.password=password;
  43. }
  44. @ManyToMany
  45. @JoinTable(name="t_user_role",joinColumns={@JoinColumn(name="user_id")},inverseJoinColumns={@JoinColumn(name="role_id")})
  46. publicList<Role>getRoleList(){
  47. returnroleList;
  48. }
  49. publicvoidsetRoleList(List<Role>roleList){
  50. this.roleList=roleList;
  51. }
  52. @Transient
  53. publicSet<String>getRolesName(){
  54. List<Role>roles=getRoleList();
  55. Set<String>set=newHashSet<String>();
  56. for(Rolerole:roles){
  57. set.add(role.getRolename());
  58. }
  59. returnset;
  60. }
  61. }

Java代码 收藏代码
  1. packagecom.cn.pojo;
  2. importjava.util.ArrayList;
  3. importjava.util.List;
  4. importjavax.persistence.Entity;
  5. importjavax.persistence.GeneratedValue;
  6. importjavax.persistence.GenerationType;
  7. importjavax.persistence.Id;
  8. importjavax.persistence.JoinColumn;
  9. importjavax.persistence.JoinTable;
  10. importjavax.persistence.ManyToMany;
  11. importjavax.persistence.OneToMany;
  12. importjavax.persistence.Table;
  13. importjavax.persistence.Transient;
  14. @Entity
  15. @Table(name="t_role")
  16. publicclassRole{
  17. privateIntegerid;
  18. privateStringrolename;
  19. privateList<Permission>permissionList;//一个角色对应多个权限
  20. privateList<User>userList;//一个角色对应多个用户
  21. @Id
  22. @GeneratedValue(strategy=GenerationType.IDENTITY)
  23. publicIntegergetId(){
  24. returnid;
  25. }
  26. publicvoidsetId(Integerid){
  27. this.id=id;
  28. }
  29. publicStringgetRolename(){
  30. returnrolename;
  31. }
  32. publicvoidsetRolename(Stringrolename){
  33. this.rolename=rolename;
  34. }
  35. @OneToMany(mappedBy="role")
  36. publicList<Permission>getPermissionList(){
  37. returnpermissionList;
  38. }
  39. publicvoidsetPermissionList(List<Permission>permissionList){
  40. this.permissionList=permissionList;
  41. }
  42. @ManyToMany
  43. @JoinTable(name="t_user_role",joinColumns={@JoinColumn(name="role_id")},inverseJoinColumns={@JoinColumn(name="user_id")})
  44. publicList<User>getUserList(){
  45. returnuserList;
  46. }
  47. publicvoidsetUserList(List<User>userList){
  48. this.userList=userList;
  49. }
  50. @Transient
  51. publicList<String>getPermissionsName(){
  52. List<String>list=newArrayList<String>();
  53. List<Permission>perlist=getPermissionList();
  54. for(Permissionper:perlist){
  55. list.add(per.getPermissionname());
  56. }
  57. returnlist;
  58. }
  59. }

Java代码 收藏代码
  1. packagecom.cn.pojo;
  2. importjavax.persistence.Entity;
  3. importjavax.persistence.GeneratedValue;
  4. importjavax.persistence.GenerationType;
  5. importjavax.persistence.Id;
  6. importjavax.persistence.JoinColumn;
  7. importjavax.persistence.ManyToOne;
  8. importjavax.persistence.Table;
  9. @Entity
  10. @Table(name="t_permission")
  11. publicclassPermission{
  12. privateIntegerid;
  13. privateStringpermissionname;
  14. privateRolerole;//一个权限对应一个角色
  15. @Id
  16. @GeneratedValue(strategy=GenerationType.IDENTITY)
  17. publicIntegergetId(){
  18. returnid;
  19. }
  20. publicvoidsetId(Integerid){
  21. this.id=id;
  22. }
  23. publicStringgetPermissionname(){
  24. returnpermissionname;
  25. }
  26. publicvoidsetPermissionname(Stringpermissionname){
  27. this.permissionname=permissionname;
  28. }
  29. @ManyToOne
  30. @JoinColumn(name="role_id")
  31. publicRolegetRole(){
  32. returnrole;
  33. }
  34. publicvoidsetRole(Rolerole){
  35. this.role=role;
  36. }
  37. }

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

pom.xml中配置如下:

Xml代码 收藏代码
  1. <projectxmlns="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.0http://maven.apache.org/maven-v4_0_0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>com.hyx</groupId>
  5. <artifactId>springmvc</artifactId>
  6. <packaging>war</packaging>
  7. <version>0.0.1-SNAPSHOT</version>
  8. <name>springmvcMavenWebapp</name>
  9. <url>http://maven.apache.org</url>
  10. <dependencies>
  11. <dependency>
  12. <groupId>junit</groupId>
  13. <artifactId>junit</artifactId>
  14. <version>3.8.1</version>
  15. <scope>test</scope>
  16. </dependency>
  17. <!--SpringMVC核心jar-->
  18. <dependency>
  19. <groupId>org.springframework</groupId>
  20. <artifactId>spring-webmvc</artifactId>
  21. <version>3.2.4.RELEASE</version>
  22. </dependency>
  23. <!--springmvc连接数据库需要的jar-->
  24. <dependency>
  25. <groupId>org.springframework</groupId>
  26. <artifactId>spring-jdbc</artifactId>
  27. <version>3.2.4.RELEASE</version>
  28. </dependency>
  29. <dependency>
  30. <groupId>org.springframework</groupId>
  31. <artifactId>spring-orm</artifactId>
  32. <version>3.2.4.RELEASE</version>
  33. </dependency>
  34. <!--************************************-->
  35. <!--Hibernate相关jar-->
  36. <dependency>
  37. <groupId>org.hibernate</groupId>
  38. <artifactId>hibernate-core</artifactId>
  39. <version>4.2.5.Final</version>
  40. </dependency>
  41. <dependency>
  42. <groupId>org.hibernate</groupId>
  43. <artifactId>hibernate-ehcache</artifactId>
  44. <version>4.2.5.Final</version>
  45. </dependency>
  46. <dependency>
  47. <groupId>net.sf.ehcache</groupId>
  48. <artifactId>ehcache</artifactId>
  49. <version>2.7.2</version>
  50. </dependency>
  51. <dependency>
  52. <groupId>commons-dbcp</groupId>
  53. <artifactId>commons-dbcp</artifactId>
  54. <version>1.4</version>
  55. </dependency>
  56. <dependency>
  57. <groupId>mysql</groupId>
  58. <artifactId>mysql-connector-java</artifactId>
  59. <version>5.1.26</version>
  60. </dependency>
  61. <!--javax提供的annotation-->
  62. <dependency>
  63. <groupId>javax.inject</groupId>
  64. <artifactId>javax.inject</artifactId>
  65. <version>1</version>
  66. </dependency>
  67. <!--****************************-->
  68. <!--hibernate验证-->
  69. <dependency>
  70. <groupId>org.hibernate</groupId>
  71. <artifactId>hibernate-validator</artifactId>
  72. <version>5.0.1.Final</version>
  73. </dependency>
  74. <!--用于对@ResponseBody注解的支持-->
  75. <dependency>
  76. <groupId>org.codehaus.jackson</groupId>
  77. <artifactId>jackson-mapper-asl</artifactId>
  78. <version>1.9.13</version>
  79. </dependency>
  80. <!--提供对c标签的支持-->
  81. <dependency>
  82. <groupId>javax.servlet</groupId>
  83. <artifactId>jstl</artifactId>
  84. <version>1.2</version>
  85. </dependency>
  86. <!--servletapi-->
  87. <dependency>
  88. <groupId>javax.servlet</groupId>
  89. <artifactId>servlet-api</artifactId>
  90. <version>2.5</version>
  91. </dependency>
  92. <!--ApacheShiro所需的jar包-->
  93. <dependency>
  94. <groupId>org.apache.shiro</groupId>
  95. <artifactId>shiro-core</artifactId>
  96. <version>1.2.2</version>
  97. </dependency>
  98. <dependency>
  99. <groupId>org.apache.shiro</groupId>
  100. <artifactId>shiro-web</artifactId>
  101. <version>1.2.2</version>
  102. </dependency>
  103. <dependency>
  104. <groupId>org.apache.shiro</groupId>
  105. <artifactId>shiro-spring</artifactId>
  106. <version>1.2.2</version>
  107. </dependency>
  108. </dependencies>
  109. <build>
  110. <finalName>springmvc</finalName>
  111. <!--maven的jetty服务器插件-->
  112. <plugins>
  113. <plugin>
  114. <groupId>org.mortbay.jetty</groupId>
  115. <artifactId>jetty-maven-plugin</artifactId>
  116. <configuration>
  117. <scanIntervalSeconds>10</scanIntervalSeconds>
  118. <webApp>
  119. <contextPath>/</contextPath>
  120. </webApp>
  121. <!--修改jetty的默认端口-->
  122. <connectors>
  123. <connectorimplementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
  124. <port>80</port>
  125. <maxIdleTime>60000</maxIdleTime>
  126. </connector>
  127. </connectors>
  128. </configuration>
  129. </plugin>
  130. </plugins>
  131. </build>
  132. </project>

web.xml中的配置:

Xml代码 收藏代码
  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <web-appversion="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>ArchetypeCreatedWebApplication</display-name>
  8. <!--spring-orm-hibernate4的OpenSessionInViewFilter-->
  9. <filter>
  10. <filter-name>opensessioninview</filter-name>
  11. <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
  12. </filter>
  13. <filter-mapping>
  14. <filter-name>opensessioninview</filter-name>
  15. <url-pattern>/*</url-pattern>
  16. </filter-mapping>
  17. <!--配置springmvcservlet-->
  18. <servlet>
  19. <servlet-name>springmvc</servlet-name>
  20. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  21. <load-on-startup>1</load-on-startup>
  22. </servlet>
  23. <servlet-mapping>
  24. <servlet-name>springmvc</servlet-name>
  25. <!--/表示所有的请求都要经过此serlvet-->
  26. <url-pattern>/</url-pattern>
  27. </servlet-mapping>
  28. <!--spring的监听器-->
  29. <context-param>
  30. <param-name>contextConfigLocation</param-name>
  31. <param-value>classpath:applicationContext*.xml</param-value>
  32. </context-param>
  33. <listener>
  34. <listener-class>
  35. org.springframework.web.context.ContextLoaderListener
  36. </listener-class>
  37. </listener>
  38. <!--Shiro配置-->
  39. <filter>
  40. <filter-name>shiroFilter</filter-name>
  41. <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
  42. </filter>
  43. <filter-mapping>
  44. <filter-name>shiroFilter</filter-name>
  45. <url-pattern>/*</url-pattern>
  46. </filter-mapping>
  47. </web-app>

Java代码 收藏代码
  1. packagecom.cn.service;
  2. importjava.util.List;
  3. importjavax.inject.Inject;
  4. importorg.apache.shiro.authc.AuthenticationException;
  5. importorg.apache.shiro.authc.AuthenticationInfo;
  6. importorg.apache.shiro.authc.AuthenticationToken;
  7. importorg.apache.shiro.authc.SimpleAuthenticationInfo;
  8. importorg.apache.shiro.authc.UsernamePasswordToken;
  9. importorg.apache.shiro.authz.AuthorizationInfo;
  10. importorg.apache.shiro.authz.SimpleAuthorizationInfo;
  11. importorg.apache.shiro.realm.AuthorizingRealm;
  12. importorg.apache.shiro.subject.PrincipalCollection;
  13. importorg.springframework.stereotype.Service;
  14. importorg.springframework.transaction.annotation.Transactional;
  15. importcom.cn.pojo.Role;
  16. importcom.cn.pojo.User;
  17. @Service
  18. @Transactional
  19. publicclassMyShiroextendsAuthorizingRealm{
  20. @Inject
  21. privateUserServiceuserService;
  22. /**
  23. *权限认证
  24. */
  25. @Override
  26. protectedAuthorizationInfodoGetAuthorizationInfo(PrincipalCollectionprincipalCollection){
  27. //获取登录时输入的用户名
  28. StringloginName=(String)principalCollection.fromRealm(getName()).iterator().next();
  29. //到数据库查是否有此对象
  30. Useruser=userService.findByName(loginName);
  31. if(user!=null){
  32. //权限信息对象info,用来存放查出的用户的所有的角色(role)及权限(permission)
  33. SimpleAuthorizationInfoinfo=newSimpleAuthorizationInfo();
  34. //用户的角色集合
  35. info.setRoles(user.getRolesName());
  36. //用户的角色对应的所有权限,如果只使用角色定义访问权限,下面的四行可以不要
  37. List<Role>roleList=user.getRoleList();
  38. for(Rolerole:roleList){
  39. info.addStringPermissions(role.getPermissionsName());
  40. }
  41. returninfo;
  42. }
  43. returnnull;
  44. }
  45. /**
  46. *登录认证;
  47. */
  48. @Override
  49. protectedAuthenticationInfodoGetAuthenticationInfo(
  50. AuthenticationTokenauthenticationToken)throwsAuthenticationException{
  51. //UsernamePasswordToken对象用来存放提交的登录信息
  52. UsernamePasswordTokentoken=(UsernamePasswordToken)authenticationToken;
  53. //查出是否有此用户
  54. Useruser=userService.findByName(token.getUsername());
  55. if(user!=null){
  56. //若存在,将此用户存放到登录认证info中
  57. returnnewSimpleAuthenticationInfo(user.getUsername(),user.getPassword(),getName());
  58. }
  59. returnnull;
  60. }
  61. }

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

applicationContext-shiro.xml

Xml代码 收藏代码
  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <beansxmlns="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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd
  9. http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsd
  10. http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd
  11. http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd">
  12. <!--配置权限管理器-->
  13. <beanid="securityManager"class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
  14. <!--ref对应我们写的realmMyShiro-->
  15. <propertyname="realm"ref="myShiro"/>
  16. <!--使用下面配置的缓存管理器-->
  17. <propertyname="cacheManager"ref="cacheManager"/>
  18. </bean>
  19. <!--配置shiro的过滤器工厂类,id-shiroFilter要和我们在web.xml中配置的过滤器一致-->
  20. <beanid="shiroFilter"class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
  21. <!--调用我们配置的权限管理器-->
  22. <propertyname="securityManager"ref="securityManager"/>
  23. <!--配置我们的登录请求地址-->
  24. <propertyname="loginUrl"value="/login"/>
  25. <!--配置我们在登录页登录成功后的跳转地址,如果你访问的是非/login地址,则跳到您访问的地址-->
  26. <propertyname="successUrl"value="/user"/>
  27. <!--如果您请求的资源不再您的权限范围,则跳转到/403请求地址-->
  28. <propertyname="unauthorizedUrl"value="/403"/>
  29. <!--权限配置-->
  30. <propertyname="filterChainDefinitions">
  31. <value>
  32. <!--anon表示此地址不需要任何权限即可访问-->
  33. /static/**=anon
  34. <!--perms[user:query]表示访问此连接需要权限为user:query的用户-->
  35. /user=perms[user:query]
  36. <!--roles[manager]表示访问此连接需要用户的角色为manager-->
  37. /user/add=roles[manager]
  38. /user/del/**=roles[admin]
  39. /user/edit/**=roles[manager]
  40. <!--所有的请求(除去配置的静态资源请求或请求地址为anon的请求)都要通过登录验证,如果未登录则跳到/login-->
  41. /**=authc
  42. </value>
  43. </property>
  44. </bean>
  45. <beanid="cacheManager"class="org.apache.shiro.cache.MemoryConstrainedCacheManager"/>
  46. <beanid="lifecycleBeanPostProcessor"class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>
  47. </beans>

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

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

三个主要的JSP:
login.jsp:

Html代码 收藏代码
  1. <%@pagelanguage="java"import="java.util.*"pageEncoding="UTF-8"%>
  2. <%@taglibprefix="form"uri="http://www.springframework.org/tags/form"%>
  3. <!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN">
  4. <html>
  5. <head>
  6. <title>MyJSP'MyJsp.jsp'startingpage</title>
  7. </head>
  8. <body>
  9. <h1>登录页面----${message}</h1>
  10. <imgalt=""src="/static/img/1.jpg">
  11. <form:formaction="/login"commandName="user"method="post">
  12. 用户名:<form:inputpath="username"/><form:errorspath="username"cssClass="error"/><br/>
  13. 密&nbsp;&nbsp;码:<form:passwordpath="password"/><form:errorspath="password"cssClass="error"/><br/>
  14. <form:buttonname="button">submit</form:button>
  15. </form:form>
  16. </body>
  17. </html>

user.jsp:

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

403.jsp:

Html代码 收藏代码
  1. <%@pagelanguage="java"import="java.util.*"pageEncoding="UTF-8"%>
  2. <%@taglibprefix="form"uri="http://www.springframework.org/tags/form"%>
  3. <!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN">
  4. <html>
  5. <head>
  6. <title>权限错误</title>
  7. </head>
  8. <body>
  9. <h1>对不起,您没有权限请求此连接!</h1>
  10. <imgalt=""src="/static/img/1.jpg">
  11. </body>
  12. </html>

你可能感兴趣的:(springMVC)