先拿到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{ } } }