shiro cookie登录根据角色跳转不同页面的解决方案

 

先拿到cookie,再根据jsessionid来获取username,再进行角色判断即可。

@RequestMapping("/common/checkCookie")
    public void getUserInfo(HttpServletRequest request, HttpServletResponse response){
        String sessionID = null;
        Cookie[] cookies = request.getCookies();
        if (cookies == null){
            System.out.println("cookie null");
        }else {
            for (Cookie cookie : cookies) {
                System.out.println(cookie.getName() + " " + cookie.getValue());
                if ("JSESSIONID".equals(cookie.getName())){
                    sessionID = cookie.getValue();
                }
            }
            SessionKey key = new WebSessionKey(sessionID,request,response);
            try{
                Session se = SecurityUtils.getSecurityManager().getSession(key);
                Object obj = se.getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY);
                //org.apache.shiro.subject.SimplePrincipalCollection cannot be cast to com.hncxhd.bywl.entity.manual.UserInfo
                SimplePrincipalCollection coll = (SimplePrincipalCollection) obj;
                System.out.println(coll.getPrimaryPrincipal());
            }catch(Exception e){
                e.printStackTrace();
            }finally{
            }
        }

    }

 

posted @ 2020-02-14 07:49  由良大小姐  阅读(1014)  评论(0编辑  收藏  举报