转载

在前后端分离的项目中,后台使用shiro框架时,怎样使用它的会话管理系统(session),从而实现权限控制

在前后端分离的项目中,ajax跨域和保存用户信息是其中的重点和难点。

如果在后台使用shiro框架来进行权限控制,就需要用到cookie+session的模式来保存用户的信息。

在前一篇文章《在前后端分离的项目中,ajax跨域请求怎样附带cookie》中,我具体写了怎样在ajax跨域的情况下携带cookie,使用该方法使跨域请求携带cookie便可以在前后端分离的项目中使用shrio的session(会话管理系统)。

但是由于那种方法近乎与取巧的将Access-Control-Allow-Origin由*改为"null"不是所有的前端ajax框架所公认的,我们需要一种更好的模式来使用session。

在传统的前后端分离模式中,我们通常是在请求头中增加一个请求头Authorization,它的值是一串加密的信息或者密钥,在后台通过对这个请求头值的读取,获取用户的信息。

而在这样的模式中,通常都是开发者自己设计的session或者加密方式来读取和保存用户信息,而在shiro中,集成了权限控制和用户管理在它的session系统中,这就意味着我们只能通过他所规定的session+cookie来保存用户信息,在这种情况下,该以什么方式在前后端分离的项目中使用shiro?

通过资料的查询,和对shiro设计模式的解读,我发现shiro和servlet一样实在cookie中存储一个session会话的id然后在每次请求中读取该session的id并获取session,这样就可以获取指定session中储存的用户信息。

我的想法就是通过重写shiro中获取cookie中的sessionId的方法来获取请求头Authorization中的密钥,而密钥储存的便是登录是返回的sessionId,从而实现在前后端分离的项目中使用shiro框架。

接下来就是代码演示(使用SpringMVC+Shiro),只贴出核心代码:

首先是登录的代码:

[java] view plain copy
<embed id="ZeroClipboardMovie_1" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="17" height="16" name="ZeroClipboardMovie_1" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=1&width=17&height=16" wmode="transparent" style="outline: 0px; word-break: break-all;">
<embed id="ZeroClipboardMovie_5" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="17" height="16" name="ZeroClipboardMovie_5" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=5&width=17&height=16" wmode="transparent" style="outline: 0px; word-break: break-all;">
  1.     @ResponseBody  
  2. <libox-sizing: border-box;="" outline:="" 0px;="" padding:="" 0px="" 3px="" 10px="" !important;="" margin-top:="" 8px;="" margin-right:="" margin-bottom:="" margin-left:="" 40px;="" border-top:="" none;="" border-right:="" border-bottom:="" border-left:="" solid="" rgb(108,="" 226,="" 108);="" border-image:="" initial;="" list-style-type:="" decimal;="" list-style-image:="" background-color:="" rgb(248,="" 248,="" 248);="" color:="" rgb(92,="" 92,="" 92);="" line-height:="" 20px;="" list-style-position:="" outside="" word-break:="" break-all;"="">    @RequestMapping(value = "/login", method = RequestMethod.POST, produces = "application/json;charset=utf-8")  
  3.     public String login(  
  4. <libox-sizing: border-box;="" outline:="" 0px;="" padding:="" 0px="" 3px="" 10px="" !important;="" margin-top:="" 8px;="" margin-right:="" margin-bottom:="" margin-left:="" 40px;="" border-top:="" none;="" border-right:="" border-bottom:="" border-left:="" solid="" rgb(108,="" 226,="" 108);="" border-image:="" initial;="" list-style-type:="" decimal;="" list-style-image:="" background-color:="" rgb(248,="" 248,="" 248);="" color:="" rgb(92,="" 92,="" 92);="" line-height:="" 20px;="" list-style-position:="" outside="" word-break:="" break-all;"="">            @RequestParam(required = false) String username,  
  5.             @RequestParam(required = false) String password  
  6. <libox-sizing: border-box;="" outline:="" 0px;="" padding:="" 0px="" 3px="" 10px="" !important;="" margin-top:="" 8px;="" margin-right:="" margin-bottom:="" margin-left:="" 40px;="" border-top:="" none;="" border-right:="" border-bottom:="" border-left:="" solid="" rgb(108,="" 226,="" 108);="" border-image:="" initial;="" list-style-type:="" decimal;="" list-style-image:="" background-color:="" rgb(248,="" 248,="" 248);="" color:="" rgb(92,="" 92,="" 92);="" line-height:="" 20px;="" list-style-position:="" outside="" word-break:="" break-all;"="">    ) {  
  7.   
  8. <libox-sizing: border-box;="" outline:="" 0px;="" padding:="" 0px="" 3px="" 10px="" !important;="" margin-top:="" 8px;="" margin-right:="" margin-bottom:="" margin-left:="" 40px;="" border-top:="" none;="" border-right:="" border-bottom:="" border-left:="" solid="" rgb(108,="" 226,="" 108);="" border-image:="" initial;="" list-style-type:="" decimal;="" list-style-image:="" background-color:="" rgb(248,="" 248,="" 248);="" color:="" rgb(92,="" 92,="" 92);="" line-height:="" 20px;="" list-style-position:="" outside="" word-break:="" break-all;"="">        JSONObject jsonObject = new JSONObject();  
  9.   
  10. <libox-sizing: border-box;="" outline:="" 0px;="" padding:="" 0px="" 3px="" 10px="" !important;="" margin-top:="" 8px;="" margin-right:="" margin-bottom:="" margin-left:="" 40px;="" border-top:="" none;="" border-right:="" border-bottom:="" border-left:="" solid="" rgb(108,="" 226,="" 108);="" border-image:="" initial;="" list-style-type:="" decimal;="" list-style-image:="" background-color:="" rgb(248,="" 248,="" 248);="" color:="" rgb(92,="" 92,="" 92);="" line-height:="" 20px;="" list-style-position:="" outside="" word-break:="" break-all;"="">        Subject subject = SecurityUtils.getSubject();  
  11.   
  12. <libox-sizing: border-box;="" outline:="" 0px;="" padding:="" 0px="" 3px="" 10px="" !important;="" margin-top:="" 8px;="" margin-right:="" margin-bottom:="" margin-left:="" 40px;="" border-top:="" none;="" border-right:="" border-bottom:="" border-left:="" solid="" rgb(108,="" 226,="" 108);="" border-image:="" initial;="" list-style-type:="" decimal;="" list-style-image:="" background-color:="" rgb(248,="" 248,="" 248);="" color:="" rgb(92,="" 92,="" 92);="" line-height:="" 20px;="" list-style-position:="" outside="" word-break:="" break-all;"="">        password = MD5Tools.MD5(password);  
  13.   
  14. <libox-sizing: border-box;="" outline:="" 0px;="" padding:="" 0px="" 3px="" 10px="" !important;="" margin-top:="" 8px;="" margin-right:="" margin-bottom:="" margin-left:="" 40px;="" border-top:="" none;="" border-right:="" border-bottom:="" border-left:="" solid="" rgb(108,="" 226,="" 108);="" border-image:="" initial;="" list-style-type:="" decimal;="" list-style-image:="" background-color:="" rgb(248,="" 248,="" 248);="" color:="" rgb(92,="" 92,="" 92);="" line-height:="" 20px;="" list-style-position:="" outside="" word-break:="" break-all;"="">        UsernamePasswordToken token = new UsernamePasswordToken(username, password);  
  15.   
  16. <libox-sizing: border-box;="" outline:="" 0px;="" padding:="" 0px="" 3px="" 10px="" !important;="" margin-top:="" 8px;="" margin-right:="" margin-bottom:="" margin-left:="" 40px;="" border-top:="" none;="" border-right:="" border-bottom:="" border-left:="" solid="" rgb(108,="" 226,="" 108);="" border-image:="" initial;="" list-style-type:="" decimal;="" list-style-image:="" background-color:="" rgb(248,="" 248,="" 248);="" color:="" rgb(92,="" 92,="" 92);="" line-height:="" 20px;="" list-style-position:="" outside="" word-break:="" break-all;"="">        try {  
  17.             // 登录,即身份验证  
  18. <libox-sizing: border-box;="" outline:="" 0px;="" padding:="" 0px="" 3px="" 10px="" !important;="" margin-top:="" 8px;="" margin-right:="" margin-bottom:="" margin-left:="" 40px;="" border-top:="" none;="" border-right:="" border-bottom:="" border-left:="" solid="" rgb(108,="" 226,="" 108);="" border-image:="" initial;="" list-style-type:="" decimal;="" list-style-image:="" background-color:="" rgb(248,="" 248,="" 248);="" color:="" rgb(92,="" 92,="" 92);="" line-height:="" 20px;="" list-style-position:="" outside="" word-break:="" break-all;"="">        subject.login(token);  
  19.         onlineSessionManager.addOnlineSession(subject.getSession().getId());  
  20. <libox-sizing: border-box;="" outline:="" 0px;="" padding:="" 0px="" 3px="" 10px="" !important;="" margin-top:="" 8px;="" margin-right:="" margin-bottom:="" margin-left:="" 40px;="" border-top:="" none;="" border-right:="" border-bottom:="" border-left:="" solid="" rgb(108,="" 226,="" 108);="" border-image:="" initial;="" list-style-type:="" decimal;="" list-style-image:="" background-color:="" rgb(248,="" 248,="" 248);="" color:="" rgb(92,="" 92,="" 92);="" line-height:="" 20px;="" list-style-position:="" outside="" word-break:="" break-all;"="">        User user = userService.getUserByLoginName(token.getUsername());  
  21.         // 在session中存放用户信息  
  22. <libox-sizing: border-box;="" outline:="" 0px;="" padding:="" 0px="" 3px="" 10px="" !important;="" margin-top:="" 8px;="" margin-right:="" margin-bottom:="" margin-left:="" 40px;="" border-top:="" none;="" border-right:="" border-bottom:="" border-left:="" solid="" rgb(108,="" 226,="" 108);="" border-image:="" initial;="" list-style-type:="" decimal;="" list-style-image:="" background-color:="" rgb(248,="" 248,="" 248);="" color:="" rgb(92,="" 92,="" 92);="" line-height:="" 20px;="" list-style-position:="" outside="" word-break:="" break-all;"="">        subject.getSession().setAttribute("userLogin", user);  
  23.         jsonObject.put("error"0);  
  24. <libox-sizing: border-box;="" outline:="" 0px;="" padding:="" 0px="" 3px="" 10px="" !important;="" margin-top:="" 8px;="" margin-right:="" margin-bottom:="" margin-left:="" 40px;="" border-top:="" none;="" border-right:="" border-bottom:="" border-left:="" solid="" rgb(108,="" 226,="" 108);="" border-image:="" initial;="" list-style-type:="" decimal;="" list-style-image:="" background-color:="" rgb(248,="" 248,="" 248);="" color:="" rgb(92,="" 92,="" 92);="" line-height:="" 20px;="" list-style-position:="" outside="" word-break:="" break-all;"="">        jsonObject.put("msg""登录成功");  
  25.         // 返回sessionId作为token  
  26. <libox-sizing: border-box;="" outline:="" 0px;="" padding:="" 0px="" 3px="" 10px="" !important;="" margin-top:="" 8px;="" margin-right:="" margin-bottom:="" margin-left:="" 40px;="" border-top:="" none;="" border-right:="" border-bottom:="" border-left:="" solid="" rgb(108,="" 226,="" 108);="" border-image:="" initial;="" list-style-type:="" decimal;="" list-style-image:="" background-color:="" rgb(248,="" 248,="" 248);="" color:="" rgb(92,="" 92,="" 92);="" line-height:="" 20px;="" list-style-position:="" outside="" word-break:="" break-all;"="">        jsonObject.put("token",subject.getSession().getId());  
  27.         } catch (IncorrectCredentialsException e) {  
  28. <libox-sizing: border-box;="" outline:="" 0px;="" padding:="" 0px="" 3px="" 10px="" !important;="" margin-top:="" 8px;="" margin-right:="" margin-bottom:="" margin-left:="" 40px;="" border-top:="" none;="" border-right:="" border-bottom:="" border-left:="" solid="" rgb(108,="" 226,="" 108);="" border-image:="" initial;="" list-style-type:="" decimal;="" list-style-image:="" background-color:="" rgb(248,="" 248,="" 248);="" color:="" rgb(92,="" 92,="" 92);="" line-height:="" 20px;="" list-style-position:="" outside="" word-break:="" break-all;"="">            throw new JsonException("用户名或密码错误"405);  
  29.         } catch (LockedAccountException e) {  
  30. <libox-sizing: border-box;="" outline:="" 0px;="" padding:="" 0px="" 3px="" 10px="" !important;="" margin-top:="" 8px;="" margin-right:="" margin-bottom:="" margin-left:="" 40px;="" border-top:="" none;="" border-right:="" border-bottom:="" border-left:="" solid="" rgb(108,="" 226,="" 108);="" border-image:="" initial;="" list-style-type:="" decimal;="" list-style-image:="" background-color:="" rgb(248,="" 248,="" 248);="" color:="" rgb(92,="" 92,="" 92);="" line-height:="" 20px;="" list-style-position:="" outside="" word-break:="" break-all;"="">            throw new JsonException("登录失败,该用户已被冻结"405);  
  31.         } catch (AuthenticationException e) {  
  32. <libox-sizing: border-box;="" outline:="" 0px;="" padding:="" 0px="" 3px="" 10px="" !important;="" margin-top:="" 8px;="" margin-right:="" margin-bottom:="" margin-left:="" 40px;="" border-top:="" none;="" border-right:="" border-bottom:="" border-left:="" solid="" rgb(108,="" 226,="" 108);="" border-image:="" initial;="" list-style-type:="" decimal;="" list-style-image:="" background-color:="" rgb(248,="" 248,="" 248);="" color:="" rgb(92,="" 92,="" 92);="" line-height:="" 20px;="" list-style-position:="" outside="" word-break:="" break-all;"="">            throw new JsonException("用户名或密码错误"405);  
  33.         }  
  34. <libox-sizing: border-box;="" outline:="" 0px;="" padding:="" 0px="" 3px="" 10px="" !important;="" margin-top:="" 8px;="" margin-right:="" margin-bottom:="" margin-left:="" 40px;="" border-top:="" none;="" border-right:="" border-bottom:="" border-left:="" solid="" rgb(108,="" 226,="" 108);="" border-image:="" initial;="" list-style-type:="" decimal;="" list-style-image:="" background-color:="" rgb(248,="" 248,="" 248);="" color:="" rgb(92,="" 92,="" 92);="" line-height:="" 20px;="" list-style-position:="" outside="" word-break:="" break-all;"="">        return jsonObject.toString();  
  35.     }  
  36. </libox-sizing:></libox-sizing:></libox-sizing:></libox-sizing:></libox-sizing:></libox-sizing:></libox-sizing:></libox-sizing:></libox-sizing:></libox-sizing:></libox-sizing:></libox-sizing:></libox-sizing:></libox-sizing:></libox-sizing:></libox-sizing:></libox-sizing:>

然后重写DefaultWebSessionManager的getSessionId方法

[java] view plain copy
<embed id="ZeroClipboardMovie_2" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="17" height="16" name="ZeroClipboardMovie_2" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=2&width=17&height=16" wmode="transparent" style="outline: 0px; word-break: break-all;">
<embed id="ZeroClipboardMovie_6" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="17" height="16" name="ZeroClipboardMovie_6" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=6&width=17&height=16" wmode="transparent" style="outline: 0px; word-break: break-all;">
  1. package com.cangshi.shiro.ssesion;  
  2. <libox-sizing: border-box;="" outline:="" 0px;="" padding:="" 0px="" 3px="" 10px="" !important;="" margin-top:="" 8px;="" margin-right:="" margin-bottom:="" margin-left:="" 40px;="" border-top:="" none;="" border-right:="" border-bottom:="" border-left:="" solid="" rgb(108,="" 226,="" 108);="" border-image:="" initial;="" list-style-type:="" decimal;="" list-style-image:="" background-color:="" rgb(248,="" 248,="" 248);="" color:="" rgb(92,="" 92,="" 92);="" line-height:="" 20px;="" list-style-position:="" outside="" word-break:="" break-all;"="">  
  3. import org.apache.shiro.web.servlet.ShiroHttpServletRequest;  
  4. <libox-sizing: border-box;="" outline:="" 0px;="" padding:="" 0px="" 3px="" 10px="" !important;="" margin-top:="" 8px;="" margin-right:="" margin-bottom:="" margin-left:="" 40px;="" border-top:="" none;="" border-right:="" border-bottom:="" border-left:="" solid="" rgb(108,="" 226,="" 108);="" border-image:="" initial;="" list-style-type:="" decimal;="" list-style-image:="" background-color:="" rgb(248,="" 248,="" 248);="" color:="" rgb(92,="" 92,="" 92);="" line-height:="" 20px;="" list-style-position:="" outside="" word-break:="" break-all;"="">import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;  
  5. import org.apache.shiro.web.util.WebUtils;  
  6. <libox-sizing: border-box;="" outline:="" 0px;="" padding:="" 0px="" 3px="" 10px="" !important;="" margin-top:="" 8px;="" margin-right:="" margin-bottom:="" margin-left:="" 40px;="" border-top:="" none;="" border-right:="" border-bottom:="" border-left:="" solid="" rgb(108,="" 226,="" 108);="" border-image:="" initial;="" list-style-type:="" decimal;="" list-style-image:="" background-color:="" rgb(248,="" 248,="" 248);="" color:="" rgb(92,="" 92,="" 92);="" line-height:="" 20px;="" list-style-position:="" outside="" word-break:="" break-all;"="">import org.slf4j.Logger;  
  7. import org.slf4j.LoggerFactory;  
  8. <libox-sizing: border-box;="" outline:="" 0px;="" padding:="" 0px="" 3px="" 10px="" !important;="" margin-top:="" 8px;="" margin-right:="" margin-bottom:="" margin-left:="" 40px;="" border-top:="" none;="" border-right:="" border-bottom:="" border-left:="" solid="" rgb(108,="" 226,="" 108);="" border-image:="" initial;="" list-style-type:="" decimal;="" list-style-image:="" background-color:="" rgb(248,="" 248,="" 248);="" color:="" rgb(92,="" 92,="" 92);="" line-height:="" 20px;="" list-style-position:="" outside="" word-break:="" break-all;"="">  
  9. import javax.servlet.ServletRequest;  
  10. <libox-sizing: border-box;="" outline:="" 0px;="" padding:="" 0px="" 3px="" 10px="" !important;="" margin-top:="" 8px;="" margin-right:="" margin-bottom:="" margin-left:="" 40px;="" border-top:="" none;="" border-right:="" border-bottom:="" border-left:="" solid="" rgb(108,="" 226,="" 108);="" border-image:="" initial;="" list-style-type:="" decimal;="" list-style-image:="" background-color:="" rgb(248,="" 248,="" 248);="" color:="" rgb(92,="" 92,="" 92);="" line-height:="" 20px;="" list-style-position:="" outside="" word-break:="" break-all;"="">import javax.servlet.ServletResponse;  
  11. import javax.servlet.http.HttpServletRequest;  
  12. <libox-sizing: border-box;="" outline:="" 0px;="" padding:="" 0px="" 3px="" 10px="" !important;="" margin-top:="" 8px;="" margin-right:="" margin-bottom:="" margin-left:="" 40px;="" border-top:="" none;="" border-right:="" border-bottom:="" border-left:="" solid="" rgb(108,="" 226,="" 108);="" border-image:="" initial;="" list-style-type:="" decimal;="" list-style-image:="" background-color:="" rgb(248,="" 248,="" 248);="" color:="" rgb(92,="" 92,="" 92);="" line-height:="" 20px;="" list-style-position:="" outside="" word-break:="" break-all;"="">import java.io.Serializable;  
  13.   
  14. <libox-sizing: border-box;="" outline:="" 0px;="" padding:="" 0px="" 3px="" 10px="" !important;="" margin-top:="" 8px;="" margin-right:="" margin-bottom:="" margin-left:="" 40px;="" border-top:="" none;="" border-right:="" border-bottom:="" border-left:="" solid="" rgb(108,="" 226,="" 108);="" border-image:="" initial;="" list-style-type:="" decimal;="" list-style-image:="" background-color:="" rgb(248,="" 248,="" 248);="" color:="" rgb(92,="" 92,="" 92);="" line-height:="" 20px;="" list-style-position:="" outside="" word-break:="" break-all;"="">/** 
  15.  * Created by Palerock 
  16. <libox-sizing: border-box;="" outline:="" 0px;="" padding:="" 0px="" 3px="" 10px="" !important;="" margin-top:="" 8px;="" margin-right:="" margin-bottom:="" margin-left:="" 40px;="" border-top:="" none;="" border-right:="" border-bottom:="" border-left:="" solid="" rgb(108,="" 226,="" 108);="" border-image:="" initial;="" list-style-type:="" decimal;="" list-style-image:="" background-color:="" rgb(248,="" 248,="" 248);="" color:="" rgb(92,="" 92,="" 92);="" line-height:="" 20px;="" list-style-position:="" outside="" word-break:="" break-all;"=""> */  
  17. public class SessionManager extends DefaultWebSessionManager {  
  18. <libox-sizing: border-box;="" outline:="" 0px;="" padding:="" 0px="" 3px="" 10px="" !important;="" margin-top:="" 8px;="" margin-right:="" margin-bottom:="" margin-left:="" 40px;="" border-top:="" none;="" border-right:="" border-bottom:="" border-left:="" solid="" rgb(108,="" 226,="" 108);="" border-image:="" initial;="" list-style-type:="" decimal;="" list-style-image:="" background-color:="" rgb(248,="" 248,="" 248);="" color:="" rgb(92,="" 92,="" 92);="" line-height:="" 20px;="" list-style-position:="" outside="" word-break:="" break-all;"="">    private static final Logger log = LoggerFactory.getLogger(DefaultWebSessionManager.class);  
  19.     private String authorization = "Authorization";  
  20. <libox-sizing: border-box;="" outline:="" 0px;="" padding:="" 0px="" 3px="" 10px="" !important;="" margin-top:="" 8px;="" margin-right:="" margin-bottom:="" margin-left:="" 40px;="" border-top:="" none;="" border-right:="" border-bottom:="" border-left:="" solid="" rgb(108,="" 226,="" 108);="" border-image:="" initial;="" list-style-type:="" decimal;="" list-style-image:="" background-color:="" rgb(248,="" 248,="" 248);="" color:="" rgb(92,="" 92,="" 92);="" line-height:="" 20px;="" list-style-position:="" outside="" word-break:="" break-all;"="">  
  21.     /** 
  22. <libox-sizing: border-box;="" outline:="" 0px;="" padding:="" 0px="" 3px="" 10px="" !important;="" margin-top:="" 8px;="" margin-right:="" margin-bottom:="" margin-left:="" 40px;="" border-top:="" none;="" border-right:="" border-bottom:="" border-left:="" solid="" rgb(108,="" 226,="" 108);="" border-image:="" initial;="" list-style-type:="" decimal;="" list-style-image:="" background-color:="" rgb(248,="" 248,="" 248);="" color:="" rgb(92,="" 92,="" 92);="" line-height:="" 20px;="" list-style-position:="" outside="" word-break:="" break-all;"="">     * 重写获取sessionId的方法调用当前Manager的获取方法 
  23.      * 
  24. <libox-sizing: border-box;="" outline:="" 0px;="" padding:="" 0px="" 3px="" 10px="" !important;="" margin-top:="" 8px;="" margin-right:="" margin-bottom:="" margin-left:="" 40px;="" border-top:="" none;="" border-right:="" border-bottom:="" border-left:="" solid="" rgb(108,="" 226,="" 108);="" border-image:="" initial;="" list-style-type:="" decimal;="" list-style-image:="" background-color:="" rgb(248,="" 248,="" 248);="" color:="" rgb(92,="" 92,="" 92);="" line-height:="" 20px;="" list-style-position:="" outside="" word-break:="" break-all;"="">     * @param request 
  25.      * @param response 
  26. <libox-sizing: border-box;="" outline:="" 0px;="" padding:="" 0px="" 3px="" 10px="" !important;="" margin-top:="" 8px;="" margin-right:="" margin-bottom:="" margin-left:="" 40px;="" border-top:="" none;="" border-right:="" border-bottom:="" border-left:="" solid="" rgb(108,="" 226,="" 108);="" border-image:="" initial;="" list-style-type:="" decimal;="" list-style-image:="" background-color:="" rgb(248,="" 248,="" 248);="" color:="" rgb(92,="" 92,="" 92);="" line-height:="" 20px;="" list-style-position:="" outside="" word-break:="" break-all;"="">     * @return 
  27.      */  
  28. <libox-sizing: border-box;="" outline:="" 0px;="" padding:="" 0px="" 3px="" 10px="" !important;="" margin-top:="" 8px;="" margin-right:="" margin-bottom:="" margin-left:="" 40px;="" border-top:="" none;="" border-right:="" border-bottom:="" border-left:="" solid="" rgb(108,="" 226,="" 108);="" border-image:="" initial;="" list-style-type:="" decimal;="" list-style-image:="" background-color:="" rgb(248,="" 248,="" 248);="" color:="" rgb(92,="" 92,="" 92);="" line-height:="" 20px;="" list-style-position:="" outside="" word-break:="" break-all;"="">    @Override  
  29.     protected Serializable getSessionId(ServletRequest request, ServletResponse response) {  
  30. <libox-sizing: border-box;="" outline:="" 0px;="" padding:="" 0px="" 3px="" 10px="" !important;="" margin-top:="" 8px;="" margin-right:="" margin-bottom:="" margin-left:="" 40px;="" border-top:="" none;="" border-right:="" border-bottom:="" border-left:="" solid="" rgb(108,="" 226,="" 108);="" border-image:="" initial;="" list-style-type:="" decimal;="" list-style-image:="" background-color:="" rgb(248,="" 248,="" 248);="" color:="" rgb(92,="" 92,="" 92);="" line-height:="" 20px;="" list-style-position:="" outside="" word-break:="" break-all;"="">        return this.getReferencedSessionId(request, response);  
  31.     }  
  32. <libox-sizing: border-box;="" outline:="" 0px;="" padding:="" 0px="" 3px="" 10px="" !important;="" margin-top:="" 8px;="" margin-right:="" margin-bottom:="" margin-left:="" 40px;="" border-top:="" none;="" border-right:="" border-bottom:="" border-left:="" solid="" rgb(108,="" 226,="" 108);="" border-image:="" initial;="" list-style-type:="" decimal;="" list-style-image:="" background-color:="" rgb(248,="" 248,="" 248);="" color:="" rgb(92,="" 92,="" 92);="" line-height:="" 20px;="" list-style-position:="" outside="" word-break:="" break-all;"="">  
  33.     /** 
  34. <libox-sizing: border-box;="" outline:="" 0px;="" padding:="" 0px="" 3px="" 10px="" !important;="" margin-top:="" 8px;="" margin-right:="" margin-bottom:="" margin-left:="" 40px;="" border-top:="" none;="" border-right:="" border-bottom:="" border-left:="" solid="" rgb(108,="" 226,="" 108);="" border-image:="" initial;="" list-style-type:="" decimal;="" list-style-image:="" background-color:="" rgb(248,="" 248,="" 248);="" color:="" rgb(92,="" 92,="" 92);="" line-height:="" 20px;="" list-style-position:="" outside="" word-break:="" break-all;"="">     * 获取sessionId从请求中 
  35.      * 
  36. <libox-sizing: border-box;="" outline:="" 0px;="" padding:="" 0px="" 3px="" 10px="" !important;="" margin-top:="" 8px;="" margin-right:="" margin-bottom:="" margin-left:="" 40px;="" border-top:="" none;="" border-right:="" border-bottom:="" border-left:="" solid="" rgb(108,="" 226,="" 108);="" border-image:="" initial;="" list-style-type:="" decimal;="" list-style-image:="" background-color:="" rgb(248,="" 248,="" 248);="" color:="" rgb(92,="" 92,="" 92);="" line-height:="" 20px;="" list-style-position:="" outside="" word-break:="" break-all;"="">     * @param request 
  37.      * @param response 
  38. <libox-sizing: border-box;="" outline:="" 0px;="" padding:="" 0px="" 3px="" 10px="" !important;="" margin-top:="" 8px;="" margin-right:="" margin-bottom:="" margin-left:="" 40px;="" border-top:="" none;="" border-right:="" border-bottom:="" border-left:="" solid="" rgb(108,="" 226,="" 108);="" border-image:="" initial;="" list-style-type:="" decimal;="" list-style-image:="" background-color:="" rgb(248,="" 248,="" 248);="" color:="" rgb(92,="" 92,="" 92);="" line-height:="" 20px;="" list-style-position:="" outside="" word-break:="" break-all;"="">     * @return 
  39.      */  
  40. <libox-sizing: border-box;="" outline:="" 0px;="" padding:="" 0px="" 3px="" 10px="" !important;="" margin-top:="" 8px;="" margin-right:="" margin-bottom:="" margin-left:="" 40px;="" border-top:="" none;="" border-right:="" border-bottom:="" border-left:="" solid="" rgb(108,="" 226,="" 108);="" border-image:="" initial;="" list-style-type:="" decimal;="" list-style-image:="" background-color:="" rgb(248,="" 248,="" 248);="" color:="" rgb(92,="" 92,="" 92);="" line-height:="" 20px;="" list-style-position:="" outside="" word-break:="" break-all;"="">    private Serializable getReferencedSessionId(ServletRequest request, ServletResponse response) {  
  41.         String id = this.getSessionIdCookieValue(request, response);  
  42. <libox-sizing: border-box;="" outline:="" 0px;="" padding:="" 0px="" 3px="" 10px="" !important;="" margin-top:="" 8px;="" margin-right:="" margin-bottom:="" margin-left:="" 40px;="" border-top:="" none;="" border-right:="" border-bottom:="" border-left:="" solid="" rgb(108,="" 226,="" 108);="" border-image:="" initial;="" list-style-type:="" decimal;="" list-style-image:="" background-color:="" rgb(248,="" 248,="" 248);="" color:="" rgb(92,="" 92,="" 92);="" line-height:="" 20px;="" list-style-position:="" outside="" word-break:="" break-all;"="">        if (id != null) {  
  43.             request.setAttribute(ShiroHttpServletRequest.REFERENCED_SESSION_ID_SOURCE, "cookie");  
  44. <libox-sizing: border-box;="" outline:="" 0px;="" padding:="" 0px="" 3px="" 10px="" !important;="" margin-top:="" 8px;="" margin-right:="" margin-bottom:="" margin-left:="" 40px;="" border-top:="" none;="" border-right:="" border-bottom:="" border-left:="" solid="" rgb(108,="" 226,="" 108);="" border-image:="" initial;="" list-style-type:="" decimal;="" list-style-image:="" background-color:="" rgb(248,="" 248,="" 248);="" color:="" rgb(92,="" 92,="" 92);="" line-height:="" 20px;="" list-style-position:="" outside="" word-break:="" break-all;"="">        } else {  
  45.             id = this.getUriPathSegmentParamValue(request, "JSESSIONID");  
  46. <libox-sizing: border-box;="" outline:="" 0px;="" padding:="" 0px="" 3px="" 10px="" !important;="" margin-top:="" 8px;="" margin-right:="" margin-bottom:="" margin-left:="" 40px;="" border-top:="" none;="" border-right:="" border-bottom:="" border-left:="" solid="" rgb(108,="" 226,="" 108);="" border-image:="" initial;="" list-style-type:="" decimal;="" list-style-image:="" background-color:="" rgb(248,="" 248,="" 248);="" color:="" rgb(92,="" 92,="" 92);="" line-height:="" 20px;="" list-style-position:="" outside="" word-break:="" break-all;"="">            if (id == null) {  
  47.                 // 获取请求头中的session  
  48. <libox-sizing: border-box;="" outline:="" 0px;="" padding:="" 0px="" 3px="" 10px="" !important;="" margin-top:="" 8px;="" margin-right:="" margin-bottom:="" margin-left:="" 40px;="" border-top:="" none;="" border-right:="" border-bottom:="" border-left:="" solid="" rgb(108,="" 226,="" 108);="" border-image:="" initial;="" list-style-type:="" decimal;="" list-style-image:="" background-color:="" rgb(248,="" 248,="" 248);="" color:="" rgb(92,="" 92,="" 92);="" line-height:="" 20px;="" list-style-position:="" outside="" word-break:="" break-all;"="">                id = WebUtils.toHttp(request).getHeader(this.authorization);  
  49.                 if (id == null) {  
  50. <libox-sizing: border-box;="" outline:="" 0px;="" padding:="" 0px="" 3px="" 10px="" !important;="" margin-top:="" 8px;="" margin-right:="" margin-bottom:="" margin-left:="" 40px;="" border-top:="" none;="" border-right:="" border-bottom:="" border-left:="" solid="" rgb(108,="" 226,="" 108);="" border-image:="" initial;="" list-style-type:="" decimal;="" list-style-image:="" background-color:="" rgb(248,="" 248,="" 248);="" color:="" rgb(92,="" 92,="" 92);="" line-height:="" 20px;="" list-style-position:="" outside="" word-break:="" break-all;"="">                    String name = this.getSessionIdName();  
  51.                     id = request.getParameter(name);  
  52. <libox-sizing: border-box;="" outline:="" 0px;="" padding:="" 0px="" 3px="" 10px="" !important;="" margin-top:="" 8px;="" margin-right:="" margin-bottom:="" margin-left:="" 40px;="" border-top:="" none;="" border-right:="" border-bottom:="" border-left:="" solid="" rgb(108,="" 226,="" 108);="" border-image:="" initial;="" list-style-type:="" decimal;="" list-style-image:="" background-color:="" rgb(248,="" 248,="" 248);="" color:="" rgb(92,="" 92,="" 92);="" line-height:="" 20px;="" list-style-position:="" outside="" word-break:="" break-all;"="">                    if (id == null) {  
  53.                         id = request.getParameter(name.toLowerCase());  
  54. <libox-sizing: border-box;="" outline:="" 0px;="" padding:="" 0px="" 3px="" 10px="" !important;="" margin-top:="" 8px;="" margin-right:="" margin-bottom:="" margin-left:="" 40px;="" border-top:="" none;="" border-right:="" border-bottom:="" border-left:="" solid="" rgb(108,="" 226,="" 108);="" border-image:="" initial;="" list-style-type:="" decimal;="" list-style-image:="" background-color:="" rgb(248,="" 248,="" 248);="" color:="" rgb(92,="" 92,="" 92);="" line-height:="" 20px;="" list-style-position:="" outside="" word-break:="" break-all;"="">                    }  
  55.                 }  
  56. <libox-sizing: border-box;="" outline:="" 0px;="" padding:="" 0px="" 3px="" 10px="" !important;="" margin-top:="" 8px;="" margin-right:="" margin-bottom:="" margin-left:="" 40px;="" border-top:="" none;="" border-right:="" border-bottom:="" border-left:="" solid="" rgb(108,="" 226,="" 108);="" border-image:="" initial;="" list-style-type:="" decimal;="" list-style-image:="" background-color:="" rgb(248,="" 248,="" 248);="" color:="" rgb(92,="" 92,="" 92);="" line-height:="" 20px;="" list-style-position:="" outside="" word-break:="" break-all;"="">            }  
  57.             if (id != null) {  
  58. <libox-sizing: border-box;="" outline:="" 0px;="" padding:="" 0px="" 3px="" 10px="" !important;="" margin-top:="" 8px;="" margin-right:="" margin-bottom:="" margin-left:="" 40px;="" border-top:="" none;="" border-right:="" border-bottom:="" border-left:="" solid="" rgb(108,="" 226,="" 108);="" border-image:="" initial;="" list-style-type:="" decimal;="" list-style-image:="" background-color:="" rgb(248,="" 248,="" 248);="" color:="" rgb(92,="" 92,="" 92);="" line-height:="" 20px;="" list-style-position:="" outside="" word-break:="" break-all;"="">                request.setAttribute(ShiroHttpServletRequest.REFERENCED_SESSION_ID_SOURCE, "url");  
  59.             }  
  60. <libox-sizing: border-box;="" outline:="" 0px;="" padding:="" 0px="" 3px="" 10px="" !important;="" margin-top:="" 8px;="" margin-right:="" margin-bottom:="" margin-left:="" 40px;="" border-top:="" none;="" border-right:="" border-bottom:="" border-left:="" solid="" rgb(108,="" 226,="" 108);="" border-image:="" initial;="" list-style-type:="" decimal;="" list-style-image:="" background-color:="" rgb(248,="" 248,="" 248);="" color:="" rgb(92,="" 92,="" 92);="" line-height:="" 20px;="" list-style-position:="" outside="" word-break:="" break-all;"="">        }  
  61.   
  62. <libox-sizing: border-box;="" outline:="" 0px;="" padding:="" 0px="" 3px="" 10px="" !important;="" margin-top:="" 8px;="" margin-right:="" margin-bottom:="" margin-left:="" 40px;="" border-top:="" none;="" border-right:="" border-bottom:="" border-left:="" solid="" rgb(108,="" 226,="" 108);="" border-image:="" initial;="" list-style-type:="" decimal;="" list-style-image:="" background-color:="" rgb(248,="" 248,="" 248);="" color:="" rgb(92,="" 92,="" 92);="" line-height:="" 20px;="" list-style-position:="" outside="" word-break:="" break-all;"="">        if (id != null) {  
  63.             request.setAttribute(ShiroHttpServletRequest.REFERENCED_SESSION_ID, id);  
  64. <libox-sizing: border-box;="" outline:="" 0px;="" padding:="" 0px="" 3px="" 10px="" !important;="" margin-top:="" 8px;="" margin-right:="" margin-bottom:="" margin-left:="" 40px;="" border-top:="" none;="" border-right:="" border-bottom:="" border-left:="" solid="" rgb(108,="" 226,="" 108);="" border-image:="" initial;="" list-style-type:="" decimal;="" list-style-image:="" background-color:="" rgb(248,="" 248,="" 248);="" color:="" rgb(92,="" 92,="" 92);="" line-height:="" 20px;="" list-style-position:="" outside="" word-break:="" break-all;"="">            request.setAttribute(ShiroHttpServletRequest.REFERENCED_SESSION_ID_IS_VALID, Boolean.TRUE);  
  65.         }  
  66. <libox-sizing: border-box;="" outline:="" 0px;="" padding:="" 0px="" 3px="" 10px="" !important;="" margin-top:="" 8px;="" margin-right:="" margin-bottom:="" margin-left:="" 40px;="" border-top:="" none;="" border-right:="" border-bottom:="" border-left:="" solid="" rgb(108,="" 226,="" 108);="" border-image:="" initial;="" list-style-type:="" decimal;="" list-style-image:="" background-color:="" rgb(248,="" 248,="" 248);="" color:="" rgb(92,="" 92,="" 92);="" line-height:="" 20px;="" list-style-position:="" outside="" word-break:="" break-all;"="">  
  67.         return id;  
  68. <libox-sizing: border-box;="" outline:="" 0px;="" padding:="" 0px="" 3px="" 10px="" !important;="" margin-top:="" 8px;="" margin-right:="" margin-bottom:="" margin-left:="" 40px;="" border-top:="" none;="" border-right:="" border-bottom:="" border-left:="" solid="" rgb(108,="" 226,="" 108);="" border-image:="" initial;="" list-style-type:="" decimal;="" list-style-image:="" background-color:="" rgb(248,="" 248,="" 248);="" color:="" rgb(92,="" 92,="" 92);="" line-height:="" 20px;="" list-style-position:="" outside="" word-break:="" break-all;"="">    }  
  69.   
  70. <libox-sizing: border-box;="" outline:="" 0px;="" padding:="" 0px="" 3px="" 10px="" !important;="" margin-top:="" 8px;="" margin-right:="" margin-bottom:="" margin-left:="" 40px;="" border-top:="" none;="" border-right:="" border-bottom:="" border-left:="" solid="" rgb(108,="" 226,="" 108);="" border-image:="" initial;="" list-style-type:="" decimal;="" list-style-image:="" background-color:="" rgb(248,="" 248,="" 248);="" color:="" rgb(92,="" 92,="" 92);="" line-height:="" 20px;="" list-style-position:="" outside="" word-break:="" break-all;"="">    // copy super  
  71.     private String getSessionIdCookieValue(ServletRequest request, ServletResponse response) {  
  72. <libox-sizing: border-box;="" outline:="" 0px;="" padding:="" 0px="" 3px="" 10px="" !important;="" margin-top:="" 8px;="" margin-right:="" margin-bottom:="" margin-left:="" 40px;="" border-top:="" none;="" border-right:="" border-bottom:="" border-left:="" solid="" rgb(108,="" 226,="" 108);="" border-image:="" initial;="" list-style-type:="" decimal;="" list-style-image:="" background-color:="" rgb(248,="" 248,="" 248);="" color:="" rgb(92,="" 92,="" 92);="" line-height:="" 20px;="" list-style-position:="" outside="" word-break:="" break-all;"="">        if (!this.isSessionIdCookieEnabled()) {  
  73.             log.debug("Session ID cookie is disabled - session id will not be acquired from a request cookie.");  
  74. <libox-sizing: border-box;="" outline:="" 0px;="" padding:="" 0px="" 3px="" 10px="" !important;="" margin-top:="" 8px;="" margin-right:="" margin-bottom:="" margin-left:="" 40px;="" border-top:="" none;="" border-right:="" border-bottom:="" border-left:="" solid="" rgb(108,="" 226,="" 108);="" border-image:="" initial;="" list-style-type:="" decimal;="" list-style-image:="" background-color:="" rgb(248,="" 248,="" 248);="" color:="" rgb(92,="" 92,="" 92);="" line-height:="" 20px;="" list-style-position:="" outside="" word-break:="" break-all;"="">            return null;  
  75.         } else if (!(request instanceof HttpServletRequest)) {  
  76. <libox-sizing: border-box;="" outline:="" 0px;="" padding:="" 0px="" 3px="" 10px="" !important;="" margin-top:="" 8px;="" margin-right:="" margin-bottom:="" margin-left:="" 40px;="" border-top:="" none;="" border-right:="" border-bottom:="" border-left:="" solid="" rgb(108,="" 226,="" 108);="" border-image:="" initial;="" list-style-type:="" decimal;="" list-style-image:="" background-color:="" rgb(248,="" 248,="" 248);="" color:="" rgb(92,="" 92,="" 92);="" line-height:="" 20px;="" list-style-position:="" outside="" word-break:="" break-all;"="">            log.debug("Current request is not an HttpServletRequest - cannot get session ID cookie.  Returning null.");  
  77.             return null;  
  78. <libox-sizing: border-box;="" outline:="" 0px;="" padding:="" 0px="" 3px="" 10px="" !important;="" margin-top:="" 8px;="" margin-right:="" margin-bottom:="" margin-left:="" 40px;="" border-top:="" none;="" border-right:="" border-bottom:="" border-left:="" solid="" rgb(108,="" 226,="" 108);="" border-image:="" initial;="" list-style-type:="" decimal;="" list-style-image:="" background-color:="" rgb(248,="" 248,="" 248);="" color:="" rgb(92,="" 92,="" 92);="" line-height:="" 20px;="" list-style-position:="" outside="" word-break:="" break-all;"="">        } else {  
  79.             HttpServletRequest httpRequest = (HttpServletRequest) request;  
  80. <libox-sizing: border-box;="" outline:="" 0px;="" padding:="" 0px="" 3px="" 10px="" !important;="" margin-top:="" 8px;="" margin-right:="" margin-bottom:="" margin-left:="" 40px;="" border-top:="" none;="" border-right:="" border-bottom:="" border-left:="" solid="" rgb(108,="" 226,="" 108);="" border-image:="" initial;="" list-style-type:="" decimal;="" list-style-image:="" background-color:="" rgb(248,="" 248,="" 248);="" color:="" rgb(92,="" 92,="" 92);="" line-height:="" 20px;="" list-style-position:="" outside="" word-break:="" break-all;"="">            return this.getSessionIdCookie().readValue(httpRequest, WebUtils.toHttp(response));  
  81.         }  
  82. <libox-sizing: border-box;="" outline:="" 0px;="" padding:="" 0px="" 3px="" 10px="" !important;="" margin-top:="" 8px;="" margin-right:="" margin-bottom:="" margin-left:="" 40px;="" border-top:="" none;="" border-right:="" border-bottom:="" border-left:="" solid="" rgb(108,="" 226,="" 108);="" border-image:="" initial;="" list-style-type:="" decimal;="" list-style-image:="" background-color:="" rgb(248,="" 248,="" 248);="" color:="" rgb(92,="" 92,="" 92);="" line-height:="" 20px;="" list-style-position:="" outside="" word-break:="" break-all;"="">    }  
  83.   
  84. <libox-sizing: border-box;="" outline:="" 0px;="" padding:="" 0px="" 3px="" 10px="" !important;="" margin-top:="" 8px;="" margin-right:="" margin-bottom:="" margin-left:="" 40px;="" border-top:="" none;="" border-right:="" border-bottom:="" border-left:="" solid="" rgb(108,="" 226,="" 108);="" border-image:="" initial;="" list-style-type:="" decimal;="" list-style-image:="" background-color:="" rgb(248,="" 248,="" 248);="" color:="" rgb(92,="" 92,="" 92);="" line-height:="" 20px;="" list-style-position:="" outside="" word-break:="" break-all;"="">    // copy super  
  85.     private String getUriPathSegmentParamValue(ServletRequest servletRequest, String paramName) {  
  86. <libox-sizing: border-box;="" outline:="" 0px;="" padding:="" 0px="" 3px="" 10px="" !important;="" margin-top:="" 8px;="" margin-right:="" margin-bottom:="" margin-left:="" 40px;="" border-top:="" none;="" border-right:="" border-bottom:="" border-left:="" solid="" rgb(108,="" 226,="" 108);="" border-image:="" initial;="" list-style-type:="" decimal;="" list-style-image:="" background-color:="" rgb(248,="" 248,="" 248);="" color:="" rgb(92,="" 92,="" 92);="" line-height:="" 20px;="" list-style-position:="" outside="" word-break:="" break-all;"="">        if (!(servletRequest instanceof HttpServletRequest)) {  
  87.             return null;  
  88. <libox-sizing: border-box;="" outline:="" 0px;="" padding:="" 0px="" 3px="" 10px="" !important;="" margin-top:="" 8px;="" margin-right:="" margin-bottom:="" margin-left:="" 40px;="" border-top:="" none;="" border-right:="" border-bottom:="" border-left:="" solid="" rgb(108,="" 226,="" 108);="" border-image:="" initial;="" list-style-type:="" decimal;="" list-style-image:="" background-color:="" rgb(248,="" 248,="" 248);="" color:="" rgb(92,="" 92,="" 92);="" line-height:="" 20px;="" list-style-position:="" outside="" word-break:="" break-all;"="">        } else {  
  89.             HttpServletRequest request = (HttpServletRequest) servletRequest;  
  90. <libox-sizing: border-box;="" outline:="" 0px;="" padding:="" 0px="" 3px="" 10px="" !important;="" margin-top:="" 8px;="" margin-right:="" margin-bottom:="" margin-left:="" 40px;="" border-top:="" none;="" border-right:="" border-bottom:="" border-left:="" solid="" rgb(108,="" 226,="" 108);="" border-image:="" initial;="" list-style-type:="" decimal;="" list-style-image:="" background-color:="" rgb(248,="" 248,="" 248);="" color:="" rgb(92,="" 92,="" 92);="" line-height:="" 20px;="" list-style-position:="" outside="" word-break:="" break-all;"="">            String uri = request.getRequestURI();  
  91.             if (uri == null) {  
  92. <libox-sizing: border-box;="" outline:="" 0px;="" padding:="" 0px="" 3px="" 10px="" !important;="" margin-top:="" 8px;="" margin-right:="" margin-bottom:="" margin-left:="" 40px;="" border-top:="" none;="" border-right:="" border-bottom:="" border-left:="" solid="" rgb(108,="" 226,="" 108);="" border-image:="" initial;="" list-style-type:="" decimal;="" list-style-image:="" background-color:="" rgb(248,="" 248,="" 248);="" color:="" rgb(92,="" 92,="" 92);="" line-height:="" 20px;="" list-style-position:="" outside="" word-break:="" break-all;"="">                return null;  
  93.             } else {  
  94. <libox-sizing: border-box;="" outline:="" 0px;="" padding:="" 0px="" 3px="" 10px="" !important;="" margin-top:="" 8px;="" margin-right:="" margin-bottom:="" margin-left:="" 40px;="" border-top:="" none;="" border-right:="" border-bottom:="" border-left:="" solid="" rgb(108,="" 226,="" 108);="" border-image:="" initial;="" list-style-type:="" decimal;="" list-style-image:="" background-color:="" rgb(248,="" 248,="" 248);="" color:="" rgb(92,="" 92,="" 92);="" line-height:="" 20px;="" list-style-position:="" outside="" word-break:="" break-all;"="">                int queryStartIndex = uri.indexOf(63);  
  95.                 if (queryStartIndex >= 0) {  
  96. <libox-sizing: border-box;="" outline:="" 0px;="" padding:="" 0px="" 3px="" 10px="" !important;="" margin-top:="" 8px;="" margin-right:="" margin-bottom:="" margin-left:="" 40px;="" border-top:="" none;="" border-right:="" border-bottom:="" border-left:="" solid="" rgb(108,="" 226,="" 108);="" border-image:="" initial;="" list-style-type:="" decimal;="" list-style-image:="" background-color:="" rgb(248,="" 248,="" 248);="" color:="" rgb(92,="" 92,="" 92);="" line-height:="" 20px;="" list-style-position:="" outside="" word-break:="" break-all;"="">                    uri = uri.substring(0, queryStartIndex);  
  97.                 }  
  98. <libox-sizing: border-box;="" outline:="" 0px;="" padding:="" 0px="" 3px="" 10px="" !important;="" margin-top:="" 8px;="" margin-right:="" margin-bottom:="" margin-left:="" 40px;="" border-top:="" none;="" border-right:="" border-bottom:="" border-left:="" solid="" rgb(108,="" 226,="" 108);="" border-image:="" initial;="" list-style-type:="" decimal;="" list-style-image:="" background-color:="" rgb(248,="" 248,="" 248);="" color:="" rgb(92,="" 92,="" 92);="" line-height:="" 20px;="" list-style-position:="" outside="" word-break:="" break-all;"="">  
  99.                 int index = uri.indexOf(59);  
  100. <libox-sizing: border-box;="" outline:="" 0px;="" padding:="" 0px="" 3px="" 10px="" !important;="" margin-top:="" 8px;="" margin-right:="" margin-bottom:="" margin-left:="" 40px;="" border-top:="" none;="" border-right:="" border-bottom:="" border-left:="" solid="" rgb(108,="" 226,="" 108);="" border-image:="" initial;="" list-style-type:="" decimal;="" list-style-image:="" background-color:="" rgb(248,="" 248,="" 248);="" color:="" rgb(92,="" 92,="" 92);="" line-height:="" 20px;="" list-style-position:="" outside="" word-break:="" break-all;"="">                if (index < 0) {  
  101.                     return null;  
  102. <libox-sizing: border-box;="" outline:="" 0px;="" padding:="" 0px="" 3px="" 10px="" !important;="" margin-top:="" 8px;="" margin-right:="" margin-bottom:="" margin-left:="" 40px;="" border-top:="" none;="" border-right:="" border-bottom:="" border-left:="" solid="" rgb(108,="" 226,="" 108);="" border-image:="" initial;="" list-style-type:="" decimal;="" list-style-image:="" background-color:="" rgb(248,="" 248,="" 248);="" color:="" rgb(92,="" 92,="" 92);="" line-height:="" 20px;="" list-style-position:="" outside="" word-break:="" break-all;"="">                } else {  
  103.                     String TOKEN = paramName + "=";  
  104. <libox-sizing: border-box;="" outline:="" 0px;="" padding:="" 0px="" 3px="" 10px="" !important;="" margin-top:="" 8px;="" margin-right:="" margin-bottom:="" margin-left:="" 40px;="" border-top:="" none;="" border-right:="" border-bottom:="" border-left:="" solid="" rgb(108,="" 226,="" 108);="" border-image:="" initial;="" list-style-type:="" decimal;="" list-style-image:="" background-color:="" rgb(248,="" 248,="" 248);="" color:="" rgb(92,="" 92,="" 92);="" line-height:="" 20px;="" list-style-position:="" outside="" word-break:="" break-all;"="">                    uri = uri.substring(index + 1);  
  105.                     index = uri.lastIndexOf(TOKEN);  
  106. <libox-sizing: border-box;="" outline:="" 0px;="" padding:="" 0px="" 3px="" 10px="" !important;="" margin-top:="" 8px;="" margin-right:="" margin-bottom:="" margin-left:="" 40px;="" border-top:="" none;="" border-right:="" border-bottom:="" border-left:="" solid="" rgb(108,="" 226,="" 108);="" border-image:="" initial;="" list-style-type:="" decimal;="" list-style-image:="" background-color:="" rgb(248,="" 248,="" 248);="" color:="" rgb(92,="" 92,="" 92);="" line-height:="" 20px;="" list-style-position:="" outside="" word-break:="" break-all;"="">                    if (index < 0) {  
  107.                         return null;  
  108. <libox-sizing: border-box;="" outline:="" 0px;="" padding:="" 0px="" 3px="" 10px="" !important;="" margin-top:="" 8px;="" margin-right:="" margin-bottom:="" margin-left:="" 40px;="" border-top:="" none;="" border-right:="" border-bottom:="" border-left:="" solid="" rgb(108,="" 226,="" 108);="" border-image:="" initial;="" list-style-type:="" decimal;="" list-style-image:="" background-color:="" rgb(248,="" 248,="" 248);="" color:="" rgb(92,="" 92,="" 92);="" line-height:="" 20px;="" list-style-position:="" outside="" word-break:="" break-all;"="">                    } else {  
  109.                         uri = uri.substring(index + TOKEN.length());  
  110. <libox-sizing: border-box;="" outline:="" 0px;="" padding:="" 0px="" 3px="" 10px="" !important;="" margin-top:="" 8px;="" margin-right:="" margin-bottom:="" margin-left:="" 40px;="" border-top:="" none;="" border-right:="" border-bottom:="" border-left:="" solid="" rgb(108,="" 226,="" 108);="" border-image:="" initial;="" list-style-type:="" decimal;="" list-style-image:="" background-color:="" rgb(248,="" 248,="" 248);="" color:="" rgb(92,="" 92,="" 92);="" line-height:="" 20px;="" list-style-position:="" outside="" word-break:="" break-all;"="">                        index = uri.indexOf(59);  
  111.                         if (index >= 0) {  
  112. <libox-sizing: border-box;="" outline:="" 0px;="" padding:="" 0px="" 3px="" 10px="" !important;="" margin-top:="" 8px;="" margin-right:="" margin-bottom:="" margin-left:="" 40px;="" border-top:="" none;="" border-right:="" border-bottom:="" border-left:="" solid="" rgb(108,="" 226,="" 108);="" border-image:="" initial;="" list-style-type:="" decimal;="" list-style-image:="" background-color:="" rgb(248,="" 248,="" 248);="" color:="" rgb(92,="" 92,="" 92);="" line-height:="" 20px;="" list-style-position:="" outside="" word-break:="" break-all;"="">                            uri = uri.substring(0, index);  
  113.                         }  
  114. <libox-sizing: border-box;="" outline:="" 0px;="" padding:="" 0px="" 3px="" 10px="" !important;="" margin-top:="" 8px;="" margin-right:="" margin-bottom:="" margin-left:="" 40px;="" border-top:="" none;="" border-right:="" border-bottom:="" border-left:="" solid="" rgb(108,="" 226,="" 108);="" border-image:="" initial;="" list-style-type:="" decimal;="" list-style-image:="" background-color:="" rgb(248,="" 248,="" 248);="" color:="" rgb(92,="" 92,="" 92);="" line-height:="" 20px;="" list-style-position:="" outside="" word-break:="" break-all;"="">  
  115.                         return uri;  
  116. <libox-sizing: border-box;="" outline:="" 0px;="" padding:="" 0px="" 3px="" 10px="" !important;="" margin-top:="" 8px;="" margin-right:="" margin-bottom:="" margin-left:="" 40px;="" border-top:="" none;="" border-right:="" border-bottom:="" border-left:="" solid="" rgb(108,="" 226,="" 108);="" border-image:="" initial;="" list-style-type:="" decimal;="" list-style-image:="" background-color:="" rgb(248,="" 248,="" 248);="" color:="" rgb(92,="" 92,="" 92);="" line-height:="" 20px;="" list-style-position:="" outside="" word-break:="" break-all;"="">                    }  
  117.                 }  
  118. <libox-sizing: border-box;="" outline:="" 0px;="" padding:="" 0px="" 3px="" 10px="" !important;="" margin-top:="" 8px;="" margin-right:="" margin-bottom:="" margin-left:="" 40px;="" border-top:="" none;="" border-right:="" border-bottom:="" border-left:="" solid="" rgb(108,="" 226,="" 108);="" border-image:="" initial;="" list-style-type:="" decimal;="" list-style-image:="" background-color:="" rgb(248,="" 248,="" 248);="" color:="" rgb(92,="" 92,="" 92);="" line-height:="" 20px;="" list-style-position:="" outside="" word-break:="" break-all;"="">            }  
  119.         }  
  120. <libox-sizing: border-box;="" outline:="" 0px;="" padding:="" 0px="" 3px="" 10px="" !important;="" margin-top:="" 8px;="" margin-right:="" margin-bottom:="" margin-left:="" 40px;="" border-top:="" none;="" border-right:="" border-bottom:="" border-left:="" solid="" rgb(108,="" 226,="" 108);="" border-image:="" initial;="" list-style-type:="" decimal;="" list-style-image:="" background-color:="" rgb(248,="" 248,="" 248);="" color:="" rgb(92,="" 92,="" 92);="" line-height:="" 20px;="" list-style-position:="" outside="" word-break:="" break-all;"="">    }  
  121.   
  122. <libox-sizing: border-box;="" outline:="" 0px;="" padding:="" 0px="" 3px="" 10px="" !important;="" margin-top:="" 8px;="" margin-right:="" margin-bottom:="" margin-left:="" 40px;="" border-top:="" none;="" border-right:="" border-bottom:="" border-left:="" solid="" rgb(108,="" 226,="" 108);="" border-image:="" initial;="" list-style-type:="" decimal;="" list-style-image:="" background-color:="" rgb(248,="" 248,="" 248);="" color:="" rgb(92,="" 92,="" 92);="" line-height:="" 20px;="" list-style-position:="" outside="" word-break:="" break-all;"="">    // copy super  
  123.     private String getSessionIdName() {  
  124. <libox-sizing: border-box;="" outline:="" 0px;="" padding:="" 0px="" 3px="" 10px="" !important;="" margin-top:="" 8px;="" margin-right:="" margin-bottom:="" margin-left:="" 40px;="" border-top:="" none;="" border-right:="" border-bottom:="" border-left:="" solid="" rgb(108,="" 226,="" 108);="" border-image:="" initial;="" list-style-type:="" decimal;="" list-style-image:="" background-color:="" rgb(248,="" 248,="" 248);="" color:="" rgb(92,="" 92,="" 92);="" line-height:="" 20px;="" list-style-position:="" outside="" word-break:="" break-all;"="">        String name = this.getSessionIdCookie() != null ? this.getSessionIdCookie().getName() : null;  
  125.         if (name == null) {  
  126. <libox-sizing: border-box;="" outline:="" 0px;="" padding:="" 0px="" 3px="" 10px="" !important;="" margin-top:="" 8px;="" margin-right:="" margin-bottom:="" margin-left:="" 40px;="" border-top:="" none;="" border-right:="" border-bottom:="" border-left:="" solid="" rgb(108,="" 226,="" 108);="" border-image:="" initial;="" list-style-type:="" decimal;="" list-style-image:="" background-color:="" rgb(248,="" 248,="" 248);="" color:="" rgb(92,="" 92,="" 92);="" line-height:="" 20px;="" list-style-position:="" outside="" word-break:="" break-all;"="">            name = "JSESSIONID";  
  127.         }  
  128. <libox-sizing: border-box;="" outline:="" 0px;="" padding:="" 0px="" 3px="" 10px="" !important;="" margin-top:="" 8px;="" margin-right:="" margin-bottom:="" margin-left:="" 40px;="" border-top:="" none;="" border-right:="" border-bottom:="" border-left:="" solid="" rgb(108,="" 226,="" 108);="" border-image:="" initial;="" list-style-type:="" decimal;="" list-style-image:="" background-color:="" rgb(248,="" 248,="" 248);="" color:="" rgb(92,="" 92,="" 92);="" line-height:="" 20px;="" list-style-position:="" outside="" word-break:="" break-all;"="">  
  129.         return name;  
  130. <libox-sizing: border-box;="" outline:="" 0px;="" padding:="" 0px="" 3px="" 10px="" !important;="" margin-top:="" 8px;="" margin-right:="" margin-bottom:="" margin-left:="" 40px;="" border-top:="" none;="" border-right:="" border-bottom:="" border-left:="" solid="" rgb(108,="" 226,="" 108);="" border-image:="" initial;="" list-style-type:="" decimal;="" list-style-image:="" background-color:="" rgb(248,="" 248,="" 248);="" color:="" rgb(92,="" 92,="" 92);="" line-height:="" 20px;="" list-style-position:="" outside="" word-break:="" break-all;"="">    }  
  131. }  
  132. </libox-sizing:></libox-sizing:></libox-sizing:></libox-sizing:></libox-sizing:></libox-sizing:></libox-sizing:></libox-sizing:></libox-sizing:></libox-sizing:></libox-sizing:></libox-sizing:></libox-sizing:></libox-sizing:></libox-sizing:></libox-sizing:></libox-sizing:></libox-sizing:></libox-sizing:></libox-sizing:></libox-sizing:></libox-sizing:></libox-sizing:></libox-sizing:></libox-sizing:></libox-sizing:></libox-sizing:></libox-sizing:></libox-sizing:></libox-sizing:></libox-sizing:></libox-sizing:></libox-sizing:></libox-sizing:></libox-sizing:></libox-sizing:></libox-sizing:></libox-sizing:></libox-sizing:></libox-sizing:></libox-sizing:></libox-sizing:></libox-sizing:></libox-sizing:></libox-sizing:></libox-sizing:></libox-sizing:></libox-sizing:></libox-sizing:></libox-sizing:></libox-sizing:></libox-sizing:></libox-sizing:></libox-sizing:></libox-sizing:></libox-sizing:></libox-sizing:></libox-sizing:></libox-sizing:></libox-sizing:></libox-sizing:></libox-sizing:></libox-sizing:></libox-sizing:></libox-sizing:>

在重写这个方法的时候,调用了一个父类的私有方法,然而私有方法是不能重写的,所以重写的目的在于调用子类的同名私有方法也就是getReferencedSessionId方法,在这个方法中就是获取sessionId的步骤,在里面调用了三个父类的私有方法,getSessionIdCookieValue、getUriPathSegmentParamValue、getSessionIdName,也就是我使用copy super标注的部分,这部分是我复制父类的方法,让在不使用ajax的情况下能够正常使用cookie+session模式,而我们在步骤中加入从请求头中获取sessionId的部分也就是:

[java] view plain copy
<embed id="ZeroClipboardMovie_3" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="17" height="16" name="ZeroClipboardMovie_3" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=3&width=17&height=16" wmode="transparent" style="outline: 0px; word-break: break-all;">
<embed id="ZeroClipboardMovie_7" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="17" height="16" name="ZeroClipboardMovie_7" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=7&width=17&height=16" wmode="transparent" style="outline: 0px; word-break: break-all;">
  1. // 获取请求头中的session  
  2. <libox-sizing: border-box;="" outline:="" 0px;="" padding:="" 0px="" 3px="" 10px="" !important;="" margin-top:="" 8px;="" margin-right:="" margin-bottom:="" margin-left:="" 40px;="" border-top:="" none;="" border-right:="" border-bottom:="" border-left:="" solid="" rgb(108,="" 226,="" 108);="" border-image:="" initial;="" list-style-type:="" decimal;="" list-style-image:="" background-color:="" rgb(248,="" 248,="" 248);="" color:="" rgb(92,="" 92,="" 92);="" line-height:="" 20px;="" list-style-position:="" outside="" word-break:="" break-all;"="">id = WebUtils.toHttp(request).getHeader(this.authorization);  </libox-sizing:>

当该id 存在就不从cookie中获取id,自然达到了通过Authorization请求头获取sessionId并获取session的目的。

接下来就是Spring中xml中的配置会话管理器

[html] view plain copy
<embed id="ZeroClipboardMovie_4" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="17" height="16" name="ZeroClipboardMovie_4" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=4&width=17&height=16" wmode="transparent" style="outline: 0px; word-break: break-all;">
<embed id="ZeroClipboardMovie_8" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="17" height="16" name="ZeroClipboardMovie_8" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=8&width=17&height=16" wmode="transparent" style="outline: 0px; word-break: break-all;">
  1. <!-- 会话管理器 -->  
  2. <libox-sizing: border-box;="" outline:="" 0px;="" padding:="" 0px="" 3px="" 10px="" !important;="" margin-top:="" 8px;="" margin-right:="" margin-bottom:="" margin-left:="" 40px;="" border-top:="" none;="" border-right:="" border-bottom:="" border-left:="" solid="" rgb(108,="" 226,="" 108);="" border-image:="" initial;="" list-style-type:="" decimal;="" list-style-image:="" background-color:="" rgb(248,="" 248,="" 248);="" color:="" rgb(92,="" 92,="" 92);="" line-height:="" 20px;="" list-style-position:="" outside="" word-break:="" break-all;"=""><bean id="sessionManager" class="com.cangshi.shiro.ssesion.SessionManager">  
  3.     <!--其它相关设置-->  
  4. <libox-sizing: border-box;="" outline:="" 0px;="" padding:="" 0px="" 3px="" 10px="" !important;="" margin-top:="" 8px;="" margin-right:="" margin-bottom:="" margin-left:="" 40px;="" border-top:="" none;="" border-right:="" border-bottom:="" border-left:="" solid="" rgb(108,="" 226,="" 108);="" border-image:="" initial;="" list-style-type:="" decimal;="" list-style-image:="" background-color:="" rgb(248,="" 248,="" 248);="" color:="" rgb(92,="" 92,="" 92);="" line-height:="" 20px;="" list-style-position:="" outside="" word-break:="" break-all;"=""></bean>  </libox-sizing:></libox-sizing:>

这样我们在跨域的ajax请求中登录获取token,然后在接下来的请求的请求头中加上Authorization:[token]就可以使用shrio所自带的会话管理器,从而使用shiro的权限控制。 

原文地址:https://blog.csdn.net/palerock/article/details/73457415/ 

正文到此结束
Loading...