Example usage for org.apache.shiro.session Session removeAttribute

List of usage examples for org.apache.shiro.session Session removeAttribute

Introduction

In this page you can find the example usage for org.apache.shiro.session Session removeAttribute.

Prototype

Object removeAttribute(Object key) throws InvalidSessionException;

Source Link

Document

Removes (unbinds) the object bound to this session under the specified key name.

Usage

From source file:cn.com.xl.core.shiro.ShiroKit.java

License:Apache License

/**
 * shirosessionKey/*  w  w w .j  av a 2  s.  c  o  m*/
 */
public static void removeSessionAttr(String key) {
    Session session = getSession();
    if (session != null)
        session.removeAttribute(key);
}

From source file:cn.dreampie.common.plugin.shiro.MyFormAuthenticationFilter.java

License:Apache License

protected void clearFailureAttribute(ServletRequest request, ServletResponse response) {
    Session session = getSubject(request, response).getSession();
    session.removeAttribute(getFailureKeyAttribute());
    session.removeAttribute(AppConstants.LOGIN_USER_NAME);
}

From source file:cn.dreampie.shiro.ShiroFormAuthenticationFilter.java

License:Apache License

protected void clearFailureAttribute(ServletRequest request, ServletResponse response, boolean returnJson) {
    if (returnJson) {
        request.setAttribute("user", SubjectKit.getUser());
        request.removeAttribute(getFailureKeyAttribute());
    } else {/*www .  j a  v a  2  s  .  c  o m*/
        Session session = getSubject(request, response).getSession();
        session.removeAttribute(getFailureKeyAttribute());
        session.removeAttribute(usernameParam);
    }
}

From source file:com.baguaz.module.user.realm.AdminAuthorizingRealm.java

License:Apache License

@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
    UsernamePasswordToken upToken = (UsernamePasswordToken) token;
    String username = upToken.getUsername();

    if (username == null) {
        log.warn("???");
        throw new AccountException("???");
    }//from w  w  w. j  av  a  2  s  .c om
    User admin = null;
    try {
        admin = User.dao.getAdminByUsername(username);
        log.debug("???" + username + "?");
    } catch (Exception ex) {
        log.warn("?\n" + ex.getMessage());
    }
    if (admin == null) {
        log.warn("?");
        throw new UnknownAccountException("?!");
    }
    /*      if(!admin.getBoolean("isAccountEnabled")) {
              log.warn("?");
              throw new UnknownAccountException("?!");
          }
          if(admin.getBoolean("isAccountLocked")){
             log.warn("??");
             throw new LockedAccountException("?!");
          }*/
    UserPrincipal principal = new UserPrincipal(admin);

    Session session = SecurityUtils.getSubject().getSession();
    String tokenV = (String) session.getAttribute(IndexAdminController.TOKEN_NAME);
    session.removeAttribute(IndexAdminController.TOKEN_NAME);
    String password = admin.getStr("password");
    password = DigestUtils.sha256Hex(password + tokenV);

    //AdminRoleM role=AdminRoleM.dao.findById(admin.getInt("roleid"));
    //principal.setRole(role);
    //List<String> authorities = AdminRolePrivM.dao.getAuthoritiesName(admin.getInt("roleid"));
    //principal.setAuthorities(authorities);
    //principal.setAuthorized(true);
    return new SimpleAuthenticationInfo(principal, password, getName());
}

From source file:com.flowlogix.ejb.StatefulUtil.java

License:Apache License

/**
 * Pings all pingable SFSBs in the session
 * /*from   www. j  a v  a2  s.  co  m*/
 * @param session 
 * @return true if successful, false if any of the pings failed
 */
public static boolean pingStateful(Session session) {
    boolean rv = true;

    List<String> attrNames = FluentIterable.from(session.getAttributeKeys())
            .transform(new Function<Object, String>() {
                @Override
                public String apply(Object f) {
                    if (f instanceof String) {
                        return (String) f;
                    } else {
                        return null;
                    }
                }
            }).filter(Predicates.and(Predicates.notNull(), Predicates.contains(ejbPattern))).toList();
    for (String attrName : attrNames) {
        synchronized (session.getId().toString().intern()) {
            try {
                Object _pingable = session.getAttribute(attrName);
                if (_pingable instanceof Pingable) {
                    Pingable pingable = (Pingable) _pingable;
                    pingable.ping();
                }
            } catch (EJBException e) {
                log.debug("Failed to Ping Stateful EJB: ", e);
                rv = false; // signal failure if any of the pings fail
                session.removeAttribute(attrName);
            }
        }
    }

    return rv;
}

From source file:com.ineunet.knife.security.Server.java

License:Apache License

/**
 * @param session//from w  w w. j  a  va2  s.  c  om
 * @since 1.2.1
 */
public static void logout(Session session) {
    session.removeAttribute(DefaultSubjectContext.AUTHENTICATED_SESSION_KEY);
    session.removeAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY);
}

From source file:com.smallchill.core.base.controller.BladeController.java

License:Apache License

/**
 * Remove Object in session./*  w  w  w. jav a  2 s  . co m*/
 * 
 * @param key
 *            a String specifying the key of the Object stored in session
 */
public BladeController removeSessionAttr(String key) {
    Session session = getSession();
    if (session != null)
        session.removeAttribute(key);
    return this;
}

From source file:com.zrx.authority.web.controller.system.login.LoginController.java

/**
 * ?//from www  . j  a v  a  2 s  .co m
 *
 * @throws java.lang.Exception
 */
@RequestMapping(value = "/login_login")
@ResponseBody
public Object login() throws Exception {
    Map<String, String> map = new HashMap<String, String>();
    PageData pd = new PageData();
    pd = this.getPageData();
    String errInfo = "";
    String KEYDATA[] = pd.getString("KEYDATA").replaceAll("qq12345678fh", "").replaceAll("QQ98765423fh", "")
            .split(",fh,");

    if (null != KEYDATA && KEYDATA.length == 3) {
        //shiro?session
        Subject currentUser = SecurityUtils.getSubject();
        Session session = currentUser.getSession();
        String sessionCode = (String) session.getAttribute(Const.SESSION_SECURITY_CODE); //?session??

        String code = KEYDATA[2];
        //??
        if (null == code || "".equals(code)) {
            errInfo = "nullcode"; //??
        } else {
            String USERNAME = KEYDATA[0];
            String PASSWORD = KEYDATA[1];

            if (Tools.notEmpty(sessionCode) && sessionCode.equalsIgnoreCase(code)) {
                String passwd = new SimpleHash("SHA-1", USERNAME, PASSWORD).toString(); //?
                pd.put("PASSWORD", passwd);
                pd.put("USERNAME", USERNAME);
                pd = userService.getUserByNameAndPwd(pd);

                if (pd != null) {
                    pd.put("LAST_LOGIN", DateUtil.getTime().toString());
                    userService.updateLastLogin(pd);
                    User user = new User();
                    user.setUSER_ID(pd.getString("USER_ID"));
                    user.setUSERNAME(pd.getString("USERNAME"));
                    user.setPASSWORD(pd.getString("PASSWORD"));
                    user.setNAME(pd.getString("NAME"));
                    user.setRIGHTS(pd.getString("RIGHTS"));
                    user.setROLE_ID(pd.getString("ROLE_ID"));
                    user.setLAST_LOGIN(pd.getString("LAST_LOGIN"));
                    user.setIP(pd.getString("IP"));
                    user.setSTATUS(pd.getString("STATUS"));

                    session.setAttribute(Const.SESSION_USER, user);
                    session.removeAttribute(Const.SESSION_SECURITY_CODE);

                    //shiro?
                    Subject subject = SecurityUtils.getSubject();
                    UsernamePasswordToken token = new UsernamePasswordToken(USERNAME, PASSWORD);//authc
                    try {
                        subject.login(token);//?
                    } catch (AuthenticationException e) {
                        errInfo = "??";
                    }
                } else {
                    errInfo = "usererror";//???
                }
            } else {
                errInfo = "codeerror"; //??
            }

            if (Tools.isEmpty(errInfo)) {
                errInfo = "success"; //??
            }
        }

    } else {
        errInfo = "error"; //?
    }

    map.put("result", errInfo);
    System.out.println("...." + errInfo);
    return AppUtil.returnObject(new PageData(), map);// map
    //return map;
}

From source file:com.zrx.authority.web.controller.system.login.LoginController.java

/**
 * //from  www. j  a v  a 2 s .com
 */
@RequestMapping(value = "/login_index")
public ModelAndView login_index() {
    ModelAndView mv = this.getModelAndView();
    PageData pd = new PageData();
    pd = this.getPageData();
    try {
        //shiro?session
        Subject currentUser = SecurityUtils.getSubject();
        Session session = currentUser.getSession();

        User user = (User) session.getAttribute(Const.SESSION_USER);

        if (user != null) {
            User userr = (User) session.getAttribute(Const.SESSION_USERROL);
            if (null == userr) {
                user = userService.getUserAndRoleById(user.getUSER_ID());//ID?
                session.setAttribute(Const.SESSION_USERROL, user);
            } else {
                user = userr;
            }
            Role role = user.getRole();
            String roleRights = role != null ? role.getRIGHTS() : "";
            //???????????session
            session.setAttribute(Const.SESSION_ROLE_RIGHTS, roleRights); //??session
            session.setAttribute(Const.SESSION_USERNAME, user.getUSERNAME()); //??

            List<Menu> allmenuList = new ArrayList<Menu>();
            if (null == session.getAttribute(Const.SESSION_allmenuList)) {
                allmenuList = menuService.listAllMenu();//?????
                if (Tools.notEmpty(roleRights)) {
                    for (Menu menu : allmenuList) {
                        menu.setHasMenu(RightsHelper.testRights(roleRights, menu.getMENU_ID()));//???
                        if (menu.isHasMenu()) {
                            List<Menu> subMenuList = menu.getSubMenu();
                            for (Menu sub : subMenuList) {
                                sub.setHasMenu(RightsHelper.testRights(roleRights, sub.getMENU_ID()));
                            }
                        }
                    }
                }
                session.setAttribute(Const.SESSION_allmenuList, allmenuList); //????session

            } else {
                allmenuList = (List<Menu>) session.getAttribute(Const.SESSION_allmenuList);
            }
            //???=====
            List<Menu> menuList = new ArrayList<Menu>();
            if (null == session.getAttribute(Const.SESSION_menuList)
                    || ("yes".equals(pd.getString("changeMenu")))) {
                List<Menu> menuList1 = new ArrayList<Menu>();
                List<Menu> menuList2 = new ArrayList<Menu>();

                //??
                for (int i = 0; i < allmenuList.size(); i++) {
                    Menu menu = allmenuList.get(i);
                    if ("2".equals(menu.getMENU_TYPE())) {
                        menuList1.add(menu);
                    } else {
                        menuList2.add(menu);
                    }
                }

                session.removeAttribute(Const.SESSION_menuList);
                if ("1".equals(session.getAttribute("changeMenu"))) {
                    session.setAttribute(Const.SESSION_menuList, menuList1);
                    session.removeAttribute("changeMenu");
                    session.setAttribute("changeMenu", "2");
                    menuList = menuList1;
                } else {
                    session.setAttribute(Const.SESSION_menuList, menuList2);
                    session.removeAttribute("changeMenu");
                    session.setAttribute("changeMenu", "2");
                    menuList = menuList2;
                }
            } else {
                menuList = (List<Menu>) session.getAttribute(Const.SESSION_menuList);
            }

            //???=====
            if (null == session.getAttribute(Const.SESSION_QX)) {
                session.setAttribute(Const.SESSION_QX, this.getUQX(session)); //??session
            }

            mv.setViewName("system/admin/index");
            mv.addObject("user", user);
            mv.addObject("menuList", menuList);//???
        } else {
            System.out.println("=== login_index  user=null    ===");
            mv.setViewName("system/admin/login");//session??
        }

    } catch (Exception e) {
        mv.setViewName("system/admin/login");
        logger.error(e.getMessage(), e);
    }

    System.out.println("===  login_index ===");

    pd.put("SYSNAME", "FH"); //???
    mv.addObject("pd", pd);
    return mv;
}

From source file:com.zrx.authority.web.interceptor.LoginHandlerInterceptor.java

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
        throws Exception {
    // TODO Auto-generated method stub
    String path = request.getServletPath();
    if (path.matches(Const.NO_INTERCEPTOR_PATH)) {
        return true;
    } else {//from  w ww  .  ja v  a 2  s  .c o  m
        //shiro?session
        Subject currentUser = SecurityUtils.getSubject();
        Session session = currentUser.getSession();
        User user = (User) session.getAttribute(Const.SESSION_USER);
        if (user != null) {

            //??????,url????
            /**
             * ???xxx.do??URL??????????? ????(??????)
             */
            Boolean b = true;
            List<Menu> menuList = (List) session.getAttribute(Const.SESSION_allmenuList); //???
            path = path.substring(1, path.length());
            for (int i = 0; i < menuList.size(); i++) {
                for (int j = 0; j < menuList.get(i).getSubMenu().size(); j++) {
                    if (menuList.get(i).getSubMenu().get(j).getMENU_URL().split(".do")[0]
                            .equals(path.split(".do")[0])) {
                        if (!menuList.get(i).getSubMenu().get(j).isHasMenu()) { //????
                            response.sendRedirect(request.getContextPath() + Const.LOGIN);
                            return false;
                        } else { //
                            Map<String, String> map = (Map<String, String>) session
                                    .getAttribute(Const.SESSION_QX);//??
                            map.remove("add");
                            map.remove("del");
                            map.remove("edit");
                            map.remove("cha");
                            String MENU_ID = menuList.get(i).getSubMenu().get(j).getMENU_ID();
                            String USERNAME = session.getAttribute(Const.SESSION_USERNAME).toString(); //??loginname
                            Boolean isAdmin = "admin".equals(USERNAME);
                            map.put("add",
                                    RightsHelper.testRights(map.get("adds"), MENU_ID) || isAdmin ? "1" : "0");
                            map.put("del",
                                    RightsHelper.testRights(map.get("dels"), MENU_ID) || isAdmin ? "1" : "0");
                            map.put("edit",
                                    RightsHelper.testRights(map.get("edits"), MENU_ID) || isAdmin ? "1" : "0");
                            map.put("cha",
                                    RightsHelper.testRights(map.get("chas"), MENU_ID) || isAdmin ? "1" : "0");
                            session.removeAttribute(Const.SESSION_QX);
                            session.setAttribute(Const.SESSION_QX, map); //????
                        }
                    }
                }
            }
            return true;
        } else {
            //
            response.sendRedirect(request.getContextPath() + Const.LOGIN);
            return false;
            //return true;
        }
    }
}