Shiro框架简单介绍

Dear 丶 2022-07-14 02:07 386阅读 0赞

版权声明:本文为博主原创文章,转载注明出处http://blog.csdn.net/u013142781

目录(?)[+]

一、Shiro框架简单介绍

Apache Shiro是Java的一个安全框架,旨在简化身份验证和授权。Shiro在JavaSE和JavaEE项目中都可以使用。它主要用来处理身份认证,授权,企业会话管理和加密等。Shiro的具体功能点如下:

(1)身份认证/登录,验证用户是不是拥有相应的身份;
(2)授权,即权限验证,验证某个已认证的用户是否拥有某个权限;即判断用户是否能做事情,常见的如:验证某个用户是否拥有某个角色。或者细粒度的验证某个用户对某个资源是否具有某个权限;
(3)会话管理,即用户登录后就是一次会话,在没有退出之前,它的所有信息都在会话中;会话可以是普通JavaSE环境的,也可以是如Web环境的;
(4)加密,保护数据的安全性,如密码加密存储到数据库,而不是明文存储;
(5)Web支持,可以非常容易的集成到Web环境;
Caching:缓存,比如用户登录后,其用户信息、拥有的角色/权限不必每次去查,这样可以提高效率;
(6)shiro支持多线程应用的并发验证,即如在一个线程中开启另一个线程,能把权限自动传播过去;
(7)提供测试支持;
(8)允许一个用户假装为另一个用户(如果他们允许)的身份进行访问;
(9)记住我,这个是非常常见的功能,即一次登录后,下次再来的话不用登录了。

文字描述可能并不能让猿友们完全理解具体功能的意思。下面我们以登录验证为例,向猿友们介绍Shiro的使用。至于其他功能点,猿友们用到的时候再去深究其用法也不迟。

二、Shiro实例详细说明

本实例环境:eclipse + maven
本实例采用的主要技术:spring + springmvc + shiro

2.1、依赖的包

假设已经配置好了spring和springmvc的情况下,还需要引入shiro以及shiro集成到spring的包,maven依赖如下:

  1. <!-- Spring 整合Shiro需要的依赖 -->
  2. <dependency>
  3. <groupId>org.apache.shiro</groupId>
  4. <artifactId>shiro-core</artifactId>
  5. <version>1.2.1</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>org.apache.shiro</groupId>
  9. <artifactId>shiro-web</artifactId>
  10. <version>1.2.1</version>
  11. </dependency>
  12. <dependency>
  13. <groupId>org.apache.shiro</groupId>
  14. <artifactId>shiro-ehcache</artifactId>
  15. <version>1.2.1</version>
  16. </dependency>
  17. <dependency>
  18. <groupId>org.apache.shiro</groupId>
  19. <artifactId>shiro-spring</artifactId>
  20. <version>1.2.1</version>
  21. </dependency>
  22. 1
  23. 2
  24. 3
  25. 4
  26. 5
  27. 6
  28. 7
  29. 8
  30. 9
  31. 10
  32. 11
  33. 12
  34. 13
  35. 14
  36. 15
  37. 16
  38. 17
  39. 18
  40. 19
  41. 20
  42. 21
  43. 1
  44. 2
  45. 3
  46. 4
  47. 5
  48. 6
  49. 7
  50. 8
  51. 9
  52. 10
  53. 11
  54. 12
  55. 13
  56. 14
  57. 15
  58. 16
  59. 17
  60. 18
  61. 19
  62. 20
  63. 21

2.2、定义shiro拦截器

对url进行拦截,如果没有验证成功的需要验证,然后额外给用户赋予角色和权限。

自定义的拦截器需要继承AuthorizingRealm并实现登录验证和赋予角色权限的两个方法,具体代码如下:

  1. package com.luo.shiro.realm;
  2. import java.util.HashSet;
  3. import java.util.Set;
  4. import org.apache.shiro.authc.AuthenticationException;
  5. import org.apache.shiro.authc.AuthenticationInfo;
  6. import org.apache.shiro.authc.AuthenticationToken;
  7. import org.apache.shiro.authc.SimpleAuthenticationInfo;
  8. import org.apache.shiro.authc.UsernamePasswordToken;
  9. import org.apache.shiro.authz.AuthorizationInfo;
  10. import org.apache.shiro.authz.SimpleAuthorizationInfo;
  11. import org.apache.shiro.realm.AuthorizingRealm;
  12. import org.apache.shiro.subject.PrincipalCollection;
  13. import com.luo.util.DecriptUtil;
  14. public class MyShiroRealm extends AuthorizingRealm {
  15. //这里因为没有调用后台,直接默认只有一个用户("luoguohui","123456")
  16. private static final String USER_NAME = "luoguohui";
  17. private static final String PASSWORD = "123456";
  18. /* * 授权 */
  19. @Override
  20. protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
  21. Set<String> roleNames = new HashSet<String>();
  22. Set<String> permissions = new HashSet<String>();
  23. roleNames.add("administrator");//添加角色
  24. permissions.add("newPage.jhtml"); //添加权限
  25. SimpleAuthorizationInfo info = new SimpleAuthorizationInfo(roleNames);
  26. info.setStringPermissions(permissions);
  27. return info;
  28. }
  29. /* * 登录验证 */
  30. @Override
  31. protected AuthenticationInfo doGetAuthenticationInfo(
  32. AuthenticationToken authcToken) throws AuthenticationException {
  33. UsernamePasswordToken token = (UsernamePasswordToken) authcToken;
  34. if(token.getUsername().equals(USER_NAME)){
  35. return new SimpleAuthenticationInfo(USER_NAME, DecriptUtil.MD5(PASSWORD), getName());
  36. }else{
  37. throw new AuthenticationException();
  38. }
  39. }
  40. }
  41. 1
  42. 2
  43. 3
  44. 4
  45. 5
  46. 6
  47. 7
  48. 8
  49. 9
  50. 10
  51. 11
  52. 12
  53. 13
  54. 14
  55. 15
  56. 16
  57. 17
  58. 18
  59. 19
  60. 20
  61. 21
  62. 22
  63. 23
  64. 24
  65. 25
  66. 26
  67. 27
  68. 28
  69. 29
  70. 30
  71. 31
  72. 32
  73. 33
  74. 34
  75. 35
  76. 36
  77. 37
  78. 38
  79. 39
  80. 40
  81. 41
  82. 42
  83. 43
  84. 44
  85. 45
  86. 46
  87. 47
  88. 48
  89. 49
  90. 50
  91. 51
  92. 1
  93. 2
  94. 3
  95. 4
  96. 5
  97. 6
  98. 7
  99. 8
  100. 9
  101. 10
  102. 11
  103. 12
  104. 13
  105. 14
  106. 15
  107. 16
  108. 17
  109. 18
  110. 19
  111. 20
  112. 21
  113. 22
  114. 23
  115. 24
  116. 25
  117. 26
  118. 27
  119. 28
  120. 29
  121. 30
  122. 31
  123. 32
  124. 33
  125. 34
  126. 35
  127. 36
  128. 37
  129. 38
  130. 39
  131. 40
  132. 41
  133. 42
  134. 43
  135. 44
  136. 45
  137. 46
  138. 47
  139. 48
  140. 49
  141. 50
  142. 51

2.3、shiro配置文件

spring-shiro.xml文件内容如下:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd" default-lazy-init="true">
  3. <description>Shiro Configuration</description>
  4. <!-- Shiro's main business-tier object for web-enabled applications -->
  5. <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
  6. <property name="realm" ref="myShiroRealm" />
  7. <property name="cacheManager" ref="cacheManager" />
  8. </bean>
  9. <!-- 項目自定义的Realm -->
  10. <bean id="myShiroRealm" class="com.luo.shiro.realm.MyShiroRealm">
  11. <property name="cacheManager" ref="cacheManager" />
  12. </bean>
  13. <!-- Shiro Filter -->
  14. <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
  15. <property name="securityManager" ref="securityManager" />
  16. <property name="loginUrl" value="/login.jhtml" />
  17. <property name="successUrl" value="/loginsuccess.jhtml" />
  18. <property name="unauthorizedUrl" value="/error.jhtml" />
  19. <property name="filterChainDefinitions">
  20. <value>
  21. /index.jhtml = authc
  22. /login.jhtml = anon
  23. /checkLogin.json = anon
  24. /loginsuccess.jhtml = anon
  25. /logout.json = anon
  26. /** = authc
  27. </value>
  28. </property>
  29. </bean>
  30. <!-- 用户授权信息Cache -->
  31. <bean id="cacheManager" class="org.apache.shiro.cache.MemoryConstrainedCacheManager" />
  32. <!-- 保证实现了Shiro内部lifecycle函数的bean执行 -->
  33. <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor" />
  34. <!-- AOP式方法级权限检查 -->
  35. <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" depends-on="lifecycleBeanPostProcessor">
  36. <property name="proxyTargetClass" value="true" />
  37. </bean>
  38. <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
  39. <property name="securityManager" ref="securityManager" />
  40. </bean>
  41. </beans>
  42. 1
  43. 2
  44. 3
  45. 4
  46. 5
  47. 6
  48. 7
  49. 8
  50. 9
  51. 10
  52. 11
  53. 12
  54. 13
  55. 14
  56. 15
  57. 16
  58. 17
  59. 18
  60. 19
  61. 20
  62. 21
  63. 22
  64. 23
  65. 24
  66. 25
  67. 26
  68. 27
  69. 28
  70. 29
  71. 30
  72. 31
  73. 32
  74. 33
  75. 34
  76. 35
  77. 36
  78. 37
  79. 38
  80. 39
  81. 40
  82. 41
  83. 42
  84. 43
  85. 44
  86. 45
  87. 46
  88. 47
  89. 48
  90. 49
  91. 50
  92. 51
  93. 52
  94. 53
  95. 54
  96. 55
  97. 1
  98. 2
  99. 3
  100. 4
  101. 5
  102. 6
  103. 7
  104. 8
  105. 9
  106. 10
  107. 11
  108. 12
  109. 13
  110. 14
  111. 15
  112. 16
  113. 17
  114. 18
  115. 19
  116. 20
  117. 21
  118. 22
  119. 23
  120. 24
  121. 25
  122. 26
  123. 27
  124. 28
  125. 29
  126. 30
  127. 31
  128. 32
  129. 33
  130. 34
  131. 35
  132. 36
  133. 37
  134. 38
  135. 39
  136. 40
  137. 41
  138. 42
  139. 43
  140. 44
  141. 45
  142. 46
  143. 47
  144. 48
  145. 49
  146. 50
  147. 51
  148. 52
  149. 53
  150. 54
  151. 55

这里有必要说清楚”shiroFilter” 这个bean里面的各个属性property的含义:

(1)securityManager:这个属性是必须的,没什么好说的,就这样配置就好。
(2)loginUrl:没有登录的用户请求需要登录的页面时自动跳转到登录页面,可配置也可不配置。
(3)successUrl:登录成功默认跳转页面,不配置则跳转至”/”,一般可以不配置,直接通过代码进行处理。
(4)unauthorizedUrl:没有权限默认跳转的页面。
(5)filterChainDefinitions,对于过滤器就有必要详细说明一下:

1)Shiro验证URL时,URL匹配成功便不再继续匹配查找(所以要注意配置文件中的URL顺序,尤其在使用通配符时),故filterChainDefinitions的配置顺序为自上而下,以最上面的为准

2)当运行一个Web应用程序时,Shiro将会创建一些有用的默认Filter实例,并自动地在[main]项中将它们置为可用自动地可用的默认的Filter实例是被DefaultFilter枚举类定义的,枚举的名称字段就是可供配置的名称

3)通常可将这些过滤器分为两组:

anon,authc,authcBasic,user是第一组认证过滤器

perms,port,rest,roles,ssl是第二组授权过滤器

注意user和authc不同:当应用开启了rememberMe时,用户下次访问时可以是一个user,但绝不会是authc,因为authc是需要重新认证的
user表示用户不一定已通过认证,只要曾被Shiro记住过登录状态的用户就可以正常发起请求,比如rememberMe

说白了,以前的一个用户登录时开启了rememberMe,然后他关闭浏览器,下次再访问时他就是一个user,而不会authc

4)举几个例子
/admin=authc,roles[admin] 表示用户必需已通过认证,并拥有admin角色才可以正常发起’/admin’请求
/edit=authc,perms[admin:edit] 表示用户必需已通过认证,并拥有admin:edit权限才可以正常发起’/edit’请求
/home=user 表示用户不一定需要已经通过认证,只需要曾经被Shiro记住过登录状态就可以正常发起’/home’请求

5)各默认过滤器常用如下(注意URL Pattern里用到的是两颗星,这样才能实现任意层次的全匹配)
/admins/**=anon 无参,表示可匿名使用,可以理解为匿名用户或游客
/admins/user/**=authc 无参,表示需认证才能使用
/admins/user/**=authcBasic 无参,表示httpBasic认证
/admins/user/**=user 无参,表示必须存在用户,当登入操作时不做检查
/admins/user/**=ssl 无参,表示安全的URL请求,协议为https
/admins/user/*=perms[user:add:]
参数可写多个,多参时必须加上引号,且参数之间用逗号分割,如/admins/user/*=perms[“user:add:,user:modify:*”]
当有多个参数时必须每个参数都通过才算通过,相当于isPermitedAll()方法
/admins/user/**=port[8081]
当请求的URL端口不是8081时,跳转到schemal://serverName:8081?queryString
其中schmal是协议http或https等,serverName是你访问的Host,8081是Port端口,queryString是你访问的URL里的?后面的参数
/admins/user/**=rest[user]
根据请求的方法,相当于/admins/user/**=perms[user:method],其中method为post,get,delete等
/admins/user/**=roles[admin]
参数可写多个,多个时必须加上引号,且参数之间用逗号分割,如/admins/user/**=roles[“admin,guest”]
当有多个参数时必须每个参数都通过才算通过,相当于hasAllRoles()方法

上文参考了http://www.cppblog.com/guojingjia2006/archive/2014/05/14/206956.html,更多详细说明请访问该链接。

2.4、web.xml配置引入对应的配置文件和过滤器

  1. <!-- 读取spring和shiro配置文件 -->
  2. <context-param>
  3. <param-name>contextConfigLocation</param-name>
  4. <param-value>classpath:application.xml,classpath:shiro/spring-shiro.xml</param-value>
  5. </context-param>
  6. <!-- shiro过滤器 -->
  7. <filter>
  8. <filter-name>shiroFilter</filter-name>
  9. <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
  10. <init-param>
  11. <param-name>targetFilterLifecycle</param-name>
  12. <param-value>true</param-value>
  13. </init-param>
  14. </filter>
  15. <filter-mapping>
  16. <filter-name>shiroFilter</filter-name>
  17. <url-pattern>*.jhtml</url-pattern>
  18. <url-pattern>*.json</url-pattern>
  19. </filter-mapping>
  20. 1
  21. 2
  22. 3
  23. 4
  24. 5
  25. 6
  26. 7
  27. 8
  28. 9
  29. 10
  30. 11
  31. 12
  32. 13
  33. 14
  34. 15
  35. 16
  36. 17
  37. 18
  38. 19
  39. 20
  40. 1
  41. 2
  42. 3
  43. 4
  44. 5
  45. 6
  46. 7
  47. 8
  48. 9
  49. 10
  50. 11
  51. 12
  52. 13
  53. 14
  54. 15
  55. 16
  56. 17
  57. 18
  58. 19
  59. 20

2.5、controller代码

  1. package com.luo.controller;
  2. import java.util.HashMap;
  3. import java.util.Map;
  4. import javax.servlet.http.HttpServletRequest;
  5. import org.apache.shiro.SecurityUtils;
  6. import org.apache.shiro.authc.UsernamePasswordToken;
  7. import org.apache.shiro.subject.Subject;
  8. import org.springframework.stereotype.Controller;
  9. import org.springframework.web.bind.annotation.RequestMapping;
  10. import org.springframework.web.bind.annotation.RequestMethod;
  11. import org.springframework.web.bind.annotation.ResponseBody;
  12. import org.springframework.web.servlet.ModelAndView;
  13. import com.alibaba.druid.support.json.JSONUtils;
  14. import com.luo.errorcode.LuoErrorCode;
  15. import com.luo.exception.BusinessException;
  16. import com.luo.util.DecriptUtil;
  17. @Controller
  18. public class UserController {
  19. @RequestMapping("/index.jhtml")
  20. public ModelAndView getIndex(HttpServletRequest request) throws Exception {
  21. ModelAndView mav = new ModelAndView("index");
  22. return mav;
  23. }
  24. @RequestMapping("/exceptionForPageJumps.jhtml")
  25. public ModelAndView exceptionForPageJumps(HttpServletRequest request) throws Exception {
  26. throw new BusinessException(LuoErrorCode.NULL_OBJ);
  27. }
  28. @RequestMapping(value="/businessException.json", method=RequestMethod.POST)
  29. @ResponseBody
  30. public String businessException(HttpServletRequest request) {
  31. throw new BusinessException(LuoErrorCode.NULL_OBJ);
  32. }
  33. @RequestMapping(value="/otherException.json", method=RequestMethod.POST)
  34. @ResponseBody
  35. public String otherException(HttpServletRequest request) throws Exception {
  36. throw new Exception();
  37. }
  38. //跳转到登录页面
  39. @RequestMapping("/login.jhtml")
  40. public ModelAndView login() throws Exception {
  41. ModelAndView mav = new ModelAndView("login");
  42. return mav;
  43. }
  44. //跳转到登录成功页面
  45. @RequestMapping("/loginsuccess.jhtml")
  46. public ModelAndView loginsuccess() throws Exception {
  47. ModelAndView mav = new ModelAndView("loginsuccess");
  48. return mav;
  49. }
  50. @RequestMapping("/newPage.jhtml")
  51. public ModelAndView newPage() throws Exception {
  52. ModelAndView mav = new ModelAndView("newPage");
  53. return mav;
  54. }
  55. @RequestMapping("/newPageNotAdd.jhtml")
  56. public ModelAndView newPageNotAdd() throws Exception {
  57. ModelAndView mav = new ModelAndView("newPageNotAdd");
  58. return mav;
  59. }
  60. /** * 验证用户名和密码 * @param String username,String password * @return */
  61. @RequestMapping(value="/checkLogin.json",method=RequestMethod.POST)
  62. @ResponseBody
  63. public String checkLogin(String username,String password) {
  64. Map<String, Object> result = new HashMap<String, Object>();
  65. try{
  66. UsernamePasswordToken token = new UsernamePasswordToken(username, DecriptUtil.MD5(password));
  67. Subject currentUser = SecurityUtils.getSubject();
  68. if (!currentUser.isAuthenticated()){
  69. //使用shiro来验证
  70. token.setRememberMe(true);
  71. currentUser.login(token);//验证角色和权限
  72. }
  73. }catch(Exception ex){
  74. throw new BusinessException(LuoErrorCode.LOGIN_VERIFY_FAILURE);
  75. }
  76. result.put("success", true);
  77. return JSONUtils.toJSONString(result);
  78. }
  79. /** * 退出登录 */
  80. @RequestMapping(value="/logout.json",method=RequestMethod.POST)
  81. @ResponseBody
  82. public String logout() {
  83. Map<String, Object> result = new HashMap<String, Object>();
  84. result.put("success", true);
  85. Subject currentUser = SecurityUtils.getSubject();
  86. currentUser.logout();
  87. return JSONUtils.toJSONString(result);
  88. }
  89. }
  90. 1
  91. 2
  92. 3
  93. 4
  94. 5
  95. 6
  96. 7
  97. 8
  98. 9
  99. 10
  100. 11
  101. 12
  102. 13
  103. 14
  104. 15
  105. 16
  106. 17
  107. 18
  108. 19
  109. 20
  110. 21
  111. 22
  112. 23
  113. 24
  114. 25
  115. 26
  116. 27
  117. 28
  118. 29
  119. 30
  120. 31
  121. 32
  122. 33
  123. 34
  124. 35
  125. 36
  126. 37
  127. 38
  128. 39
  129. 40
  130. 41
  131. 42
  132. 43
  133. 44
  134. 45
  135. 46
  136. 47
  137. 48
  138. 49
  139. 50
  140. 51
  141. 52
  142. 53
  143. 54
  144. 55
  145. 56
  146. 57
  147. 58
  148. 59
  149. 60
  150. 61
  151. 62
  152. 63
  153. 64
  154. 65
  155. 66
  156. 67
  157. 68
  158. 69
  159. 70
  160. 71
  161. 72
  162. 73
  163. 74
  164. 75
  165. 76
  166. 77
  167. 78
  168. 79
  169. 80
  170. 81
  171. 82
  172. 83
  173. 84
  174. 85
  175. 86
  176. 87
  177. 88
  178. 89
  179. 90
  180. 91
  181. 92
  182. 93
  183. 94
  184. 95
  185. 96
  186. 97
  187. 98
  188. 99
  189. 100
  190. 101
  191. 102
  192. 103
  193. 104
  194. 105
  195. 106
  196. 107
  197. 108
  198. 109
  199. 110
  200. 111
  201. 1
  202. 2
  203. 3
  204. 4
  205. 5
  206. 6
  207. 7
  208. 8
  209. 9
  210. 10
  211. 11
  212. 12
  213. 13
  214. 14
  215. 15
  216. 16
  217. 17
  218. 18
  219. 19
  220. 20
  221. 21
  222. 22
  223. 23
  224. 24
  225. 25
  226. 26
  227. 27
  228. 28
  229. 29
  230. 30
  231. 31
  232. 32
  233. 33
  234. 34
  235. 35
  236. 36
  237. 37
  238. 38
  239. 39
  240. 40
  241. 41
  242. 42
  243. 43
  244. 44
  245. 45
  246. 46
  247. 47
  248. 48
  249. 49
  250. 50
  251. 51
  252. 52
  253. 53
  254. 54
  255. 55
  256. 56
  257. 57
  258. 58
  259. 59
  260. 60
  261. 61
  262. 62
  263. 63
  264. 64
  265. 65
  266. 66
  267. 67
  268. 68
  269. 69
  270. 70
  271. 71
  272. 72
  273. 73
  274. 74
  275. 75
  276. 76
  277. 77
  278. 78
  279. 79
  280. 80
  281. 81
  282. 82
  283. 83
  284. 84
  285. 85
  286. 86
  287. 87
  288. 88
  289. 89
  290. 90
  291. 91
  292. 92
  293. 93
  294. 94
  295. 95
  296. 96
  297. 97
  298. 98
  299. 99
  300. 100
  301. 101
  302. 102
  303. 103
  304. 104
  305. 105
  306. 106
  307. 107
  308. 108
  309. 109
  310. 110
  311. 111

上面代码,我们只需要更多地关注登录验证和退出登录的代码。
其中DecriptUtil.MD5(password),对密码进行md5加密解密是我自己写的工具类DecriptUtil,对应MyShiroRealm里面的登录验证里面也有对应对应的方法。
另外,BusinessException是我自己封装的异常类。
最后会提供整个工程源码供猿友下载,里面包含了所有的代码。

2.6、login.jsp代码

  1. <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
  2. <html>
  3. <head>
  4. <script src="<%=request.getContextPath()%>/static/bui/js/jquery-1.8.1.min.js"></script>
  5. </head>
  6. <body>
  7. username: <input type="text" id="username"><br><br>
  8. password: <input type="password" id="password"><br><br>
  9. <button id="loginbtn">登录</button>
  10. </body>
  11. <script type="text/javascript"> $('#loginbtn').click(function() { var param = { username : $("#username").val(), password : $("#password").val() }; $.ajax({ type: "post", url: "<%=request.getContextPath()%>" + "/checkLogin.json", data: param, dataType: "json", success: function(data) { if(data.success == false){ alert(data.errorMsg); }else{ //登录成功 window.location.href = "<%=request.getContextPath()%>" + "/loginsuccess.jhtml"; } }, error: function(data) { alert("调用失败...."); } }); }); </script>
  12. </html>
  13. 1
  14. 2
  15. 3
  16. 4
  17. 5
  18. 6
  19. 7
  20. 8
  21. 9
  22. 10
  23. 11
  24. 12
  25. 13
  26. 14
  27. 15
  28. 16
  29. 17
  30. 18
  31. 19
  32. 20
  33. 21
  34. 22
  35. 23
  36. 24
  37. 25
  38. 26
  39. 27
  40. 28
  41. 29
  42. 30
  43. 31
  44. 32
  45. 33
  46. 34
  47. 35
  48. 36
  49. 1
  50. 2
  51. 3
  52. 4
  53. 5
  54. 6
  55. 7
  56. 8
  57. 9
  58. 10
  59. 11
  60. 12
  61. 13
  62. 14
  63. 15
  64. 16
  65. 17
  66. 18
  67. 19
  68. 20
  69. 21
  70. 22
  71. 23
  72. 24
  73. 25
  74. 26
  75. 27
  76. 28
  77. 29
  78. 30
  79. 31
  80. 32
  81. 33
  82. 34
  83. 35
  84. 36

2.7、效果演示

(1)如果未登录前,输入http://localhost:8080/web_exception_project/index.jhtml会自动跳转到http://localhost:8080/web_exception_project/login.jhtml。

(2)如果登录失败和登录成功:

这里写图片描述

这里写图片描述

(3)如果登录成功,访问http://localhost:8080/web_exception_project/index.jhtml就可以到其对应的页面了。

这里写图片描述

2.8、源码下载

http://download.csdn.net/detail/u013142781/9426670

2.9、我遇到的坑

在本实例的调试里面遇到一个问题,虽然跟shiro没有关系,但是也跟猿友们分享一下。
就是ajax请求设置了“contentType : “application/json””,导致controller获取不到username和password这两个参数。
后面去掉contentType : “application/json”,采用默认的就可以了。

具体原因可以浏览博文:http://blog.csdn.net/mhmyqn/article/details/25561535

发表评论

表情:
评论列表 (有 0 条评论,386人围观)

还没有评论,来说两句吧...

相关阅读