Example usage for org.apache.shiro SecurityUtils getSubject

List of usage examples for org.apache.shiro SecurityUtils getSubject

Introduction

In this page you can find the example usage for org.apache.shiro SecurityUtils getSubject.

Prototype

public static Subject getSubject() 

Source Link

Document

Returns the currently accessible Subject available to the calling code depending on runtime environment.

Usage

From source file:cn.itganhuo.app.common.utils.HttpUtil.java

License:Apache License

/**
 * Shiro??/*from w  ww  .ja  v a  2  s.c  o  m*/
 * 
 * @version 0.0.1-SNAPSHOT
 * @author -?
 * @param key
 * @return ?
 */
public static Object getValue(String key) {
    Subject current_user = SecurityUtils.getSubject();
    return current_user.getSession().getAttribute(key);
}

From source file:cn.itganhuo.app.service.impl.ArticleServiceImpl.java

License:Apache License

@Override
public ModelAndView getArticleById(String ymd, Integer id, HttpServletRequest request) {
    ModelAndView mav = new ModelAndView();
    // ?????????????
    Map<String, Object> param = new HashMap<String, Object>();
    param.put("id", id);
    param.put("ymd", ymd);
    Article article_detail = this.getArticleDetailById(param);
    if (article_detail != null && article_detail.getId() > 0) {
        // ???//from   w w  w  . j a  v  a2  s .c om
        Object obj = request.getSession().getAttribute(ConstantPool.VISITS_FLAG);
        if (obj == null) {
            this.addVisitorNumById(id);
            request.getSession().setAttribute(ConstantPool.VISITS_FLAG, 1);
        }

        // ???
        Subject current_user = SecurityUtils.getSubject();
        User user = (User) current_user.getSession().getAttribute(ConstantPool.USER_SHIRO_SESSION_ID);

        // ??
        List<Article> related_article = this.getSameLabelArticleById(id);

        //??
        Map<String, String> param2 = new HashMap<String, String>();
        param2.put("articleId", String.valueOf(id));
        param2.put("type", String.valueOf(3));
        int collectionNumber = attentionDao.countAttentionByCondition(param2);

        //???
        param.put("userId", article_detail.getUserId());
        int articleNumber = articleDao.countArticleRows(param);

        // ??
        mav.addObject("articleNumber", articleNumber);
        mav.addObject("collectionNumber", collectionNumber);
        mav.addObject("article", article_detail);
        mav.addObject("user", user);
        mav.addObject("path", request.getContextPath());
        mav.addObject("related_article", related_article);
        mav.setViewName("article_detail");
    } else {
        mav.setViewName("error/error");
    }
    return mav;
}

From source file:cn.itganhuo.app.service.impl.AttentionServiceImpl.java

License:Apache License

@Override
public RespMsg saveAttentionInfo(Attention attention) {
    RespMsg respMsg = new RespMsg();
    Subject current_user = SecurityUtils.getSubject();
    User user_model = (User) current_user.getSession().getAttribute(ConstantPool.USER_SHIRO_SESSION_ID);
    if (user_model != null) {
        // ?//from ww  w. j a v  a 2  s  .c  o  m
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("userId", user_model.getId());
        param.put("labelId", attention.getLabelId());
        param.put("byUserId", attention.getByUserId());
        param.put("articleId", attention.getArticleId());
        List<Attention> attentions = this.find(param);
        if (attentions == null || attentions.size() == 0) {
            attention.setUserId(user_model.getId());
            attention.setPostDate(DateUtil.getNowDateTimeStr(null));
            if (!attentionDao.insert(attention)) {
                respMsg.setStatus("9999");
                respMsg.setMessage(ConfigPool.getString("respMsg.attention.SaveConcernInfoFailed"));
            }
        } else {
            respMsg.setStatus("2000");
            respMsg.setMessage(ConfigPool.getString("respMsg.attention.YouBeenConcernedAboutLabel"));
        }
    } else {
        respMsg.setStatus("1000");
    }
    return respMsg;
}

From source file:cn.itganhuo.app.service.impl.UserServiceImpl.java

License:Apache License

public RespMsg login(User user, HttpServletRequest request) {
    RespMsg respMsg = new RespMsg();
    Subject current_user = SecurityUtils.getSubject();
    // ??????//  w ww . ja  v a2  s  .  c  o m
    if (!current_user.isAuthenticated()) {
        try {
            // ?
            UsernamePasswordToken token = new UsernamePasswordToken(user.getAccount(), user.getPassword());
            token.setRememberMe(true);
            // 
            current_user.login(token);
            // ???HTTP?
            User d_user = this.loadByAccount(user.getAccount());
            current_user.getSession().setAttribute(ConstantPool.USER_SHIRO_SESSION_ID, d_user);
            respMsg.setAppendInfo(d_user.getAccount());
            // IP
            User tmp = new User();
            tmp.setAccount(user.getAccount());
            tmp.setIsLock(0);
            tmp.setLastLoginDate(DateUtil.getNowDateTimeStr(null));
            tmp.setLastLoginIp(StringUtil.getNowHttpIp(request));
            this.updateInfoByAccount(tmp);
        } catch (UnknownAccountException e) {
            respMsg.setMessage(ConfigPool.getString("respMsg.login.UnknownAccount"));
            respMsg.setStatus("1000");
        } catch (IncorrectCredentialsException e) {
            respMsg.setMessage(ConfigPool.getString("respMsg.login.IncorrectCredentials"));
            respMsg.setStatus("1001");
        } catch (LockedAccountException e) {
            respMsg.setMessage(ConfigPool.getString("respMsg.login.LockedAccount"));
            respMsg.setStatus("1002");
        } catch (EmailUnauthorizedException e) {
            respMsg.setMessage(ConfigPool.getString("respMsg.login.EmailUnauthorized"));
            respMsg.setStatus("1003");
        } catch (AuthenticationException e) {
            respMsg.setMessage(ConfigPool.getString("respMsg.login.Authentication"));
            respMsg.setStatus("1004");
        }
    }
    return respMsg;
}

From source file:cn.itganhuo.app.service.impl.UserServiceImpl.java

License:Apache License

@Override
public ModelAndView center() {
    ModelAndView mav = new ModelAndView();
    Subject current_user = SecurityUtils.getSubject();
    String account = (String) current_user.getPrincipal();
    if (StringUtil.hasText(account)) {
        User user = userDao.loadByAccount(account);
        if (user != null) {
            Map<String, Object> param = new HashMap<String, Object>();
            param.put("userId", user.getId());
            param.put("offrow", 0);
            param.put("rows", 5);

            // ??5
            List<Article> articles = articleService.getArticleByUserId(param);
            // ??5
            List<Article> dynamicArticles = articleService.getDynamicArticleByUserId(param);

            //?//from w  ww .  j a  v  a2s . c o  m
            Map<String, String> param3 = new HashMap<String, String>();
            param3.put("userId", String.valueOf(user.getId()));
            param3.put("type", String.valueOf(1));
            int attentionNumber1 = attentionDao.countAttentionByCondition(param3);
            param3.put("type", String.valueOf(2));
            int attentionNumber2 = attentionDao.countAttentionByCondition(param3);

            //??
            Map<String, String> param4 = new HashMap<String, String>();
            param4.put("byUserId", String.valueOf(user.getId()));
            param4.put("type", String.valueOf(1));
            int fansNumber1 = attentionDao.countAttentionByCondition(param4);
            param4.put("type", String.valueOf(2));
            int fansNumber2 = attentionDao.countAttentionByCondition(param4);

            //??
            Map<String, String> param2 = new HashMap<String, String>();
            param2.put("userId", String.valueOf(user.getId()));
            param2.put("type", String.valueOf(3));
            int collectionNumber = attentionDao.countAttentionByCondition(param2);

            mav.addObject("fansNumber", fansNumber1 + fansNumber2);
            mav.addObject("attentionNumber", attentionNumber1 + attentionNumber2);
            mav.addObject("collectionNumber", collectionNumber);
            mav.addObject("dynamicArticles", dynamicArticles);
            mav.addObject("articles", articles);
            mav.addObject("user", user);
            mav.setViewName("user/center");
        } else {
            mav.setViewName("user/signin");
        }
    } else {
        mav.setViewName("user/signin");
    }
    return mav;
}

From source file:cn.itganhuo.app.web.controller.ArticleController.java

License:Apache License

/**
 * ????t_reply/* w  ww  . j  a v  a 2  s  . c o m*/
 *
 * @param reply
 * @return JSON???
 * @author -?(504487927)
 * @version 2014-11-21
 */
@RequiresAuthentication
@Transactional
@RequestMapping(value = "/article/saveReply")
@ResponseBody
public RespMsg saveReply(Reply reply, @RequestParam Integer comment_id) {
    // ?
    String content = StringUtil.ifContainsSpecialStrReplace(reply.getContent());
    reply.setContent(content);
    // ???
    Subject current_user = SecurityUtils.getSubject();
    User user_model = (User) current_user.getSession().getAttribute(ConstantPool.USER_SHIRO_SESSION_ID);
    // ???
    reply.setUserId(user_model.getId());
    reply.setCommentId(comment_id);
    reply.setPostDate(DateUtil.getNowDateTimeStr(null));
    reply.setParentId(0);
    RespMsg respMsg = new RespMsg();
    if (replyService.addReply(reply) > 0) {
        respMsg.setStatus("0000");
        respMsg.setMessage(ConfigPool.getString("respMsg.reply.SaveReplySuccess"));
    } else {
        respMsg.setStatus("9999");
        respMsg.setMessage(ConfigPool.getString("respMsg.reply.SaveReplyFailure"));
    }
    return respMsg;
}

From source file:cn.itganhuo.app.web.controller.ArticleController.java

License:Apache License

/**
 * <h2></h2>//w  w w.  j av a  2  s .  c  om
 * <dl>
 * <dt>??</dt>
 * <dd></dd>
 * <dt></dt>
 * <dd></dd>
 * </dl>
 *
 * @param id   ID
 * @param type 1,2,3,4,5
 * @return
 * @version 0.0.1-SNAPSHOT
 * @author -?
 */
@RequiresAuthentication
@Transactional
@RequestMapping(value = "/article/appraise", method = RequestMethod.POST)
@ResponseBody
public RespMsg addUsefulById(@RequestParam Integer id, @RequestParam Integer type) {
    RespMsg respMsg = new RespMsg();
    // ??
    Subject current_user = SecurityUtils.getSubject();
    if (current_user == null) {
        respMsg.setStatus("9000");
        return respMsg;
    }
    User user = (User) current_user.getSession().getAttribute(ConstantPool.USER_SHIRO_SESSION_ID);
    if (user == null) {
        respMsg.setStatus("9000");
        return respMsg;
    }
    // 
    if (2 == type || 3 == type || 4 == type || 5 == type) {
        // ?
        Article article = articleService.getArticleById(id);
        // ??
        if (!article.getUserId().equals(user.getId())) {
            // ???
            if (!commentService.isInvolvedComment(id, user.getId())) {
                // 
                Comment comment_model = new Comment();
                comment_model.setType(type);
                comment_model.setObjId(id);
                comment_model.setUserId(user.getId());
                comment_model.setPostDate(DateUtil.getNowDateTimeStr(null));
                if (2 == type) {
                    comment_model.setContent("");
                } else if (3 == type) {
                    comment_model.setContent("");
                } else if (4 == type) {
                    comment_model.setContent("");
                } else if (5 == type) {
                    comment_model.setContent("");
                }
                commentService.addComment(comment_model);
                // 
                if (2 == type) {
                    articleService.addPraiseNum(id);
                } else if (3 == type) {
                    articleService.addTrampleNum(id);
                } else if (4 == type) {
                    commentService.addPraiseById(id);
                } else if (5 == type) {
                    commentService.addTrampleById(id);
                }
            } else {
                respMsg.setStatus("1001");
                respMsg.setMessage(
                        ConfigPool.getString("respMsg.comment.AddUsefulOrUseless.RepetitiveOperation"));
            }
        } else {
            respMsg.setStatus("1000");
            respMsg.setMessage(ConfigPool.getString("respMsg.comment.AddUsefulOrUseless.SamePerson"));
        }
    } else {
        respMsg.setStatus("9999");
        respMsg.setMessage(ConfigPool.getString("respMsg.EvaluationTypeIncorrect"));
    }
    return respMsg;
}

From source file:cn.itganhuo.app.web.controller.CommonController.java

License:Apache License

/**
 * //from w  ww .j  av a  2 s . c o m
 *
 * @return ?
 * @version 0.0.1-SNAPSHOT
 * @author -?
 */
@RequestMapping(value = "/logout", method = RequestMethod.GET)
public String logout() {
    Subject current_user = SecurityUtils.getSubject();
    current_user.logout();
    return "redirect:/user/signin";
}

From source file:cn.itganhuo.app.web.controller.CommonController.java

License:Apache License

/**
 * ?//from  w  w w .  jav a  2  s . c  om
 *
 * @return ??
 * @version 0.0.1-SNAPSHOT
 * @author -?
 */
@RequestMapping(value = "/labels", method = RequestMethod.GET)
public String refurlLabels(Model model, HttpServletRequest request) {
    Subject current_user = SecurityUtils.getSubject();
    String account = (String) current_user.getPrincipal();
    if (StringUtil.hasText(account)) {
        User user = userService.loadByAccount(account);
        // 1?
        List<Map<String, String>> lists = labelService.getLabelByCondition2(user.getId());
        model.addAttribute("lists", lists);
    } else {
        // 2?
        List<Label> ls = labelService.getLabelByCondition(null);
        model.addAttribute("ls", ls);
    }
    model.addAttribute("path", request.getContextPath());
    model.addAttribute("servletPath", request.getServletPath());
    return "labels";
}

From source file:cn.itganhuo.app.web.controller.UserController.java

License:Apache License

/**
 * ?//from  w  w w  . j  a  va2  s.  c o m
 *
 * @return ?
 * @version 0.0.1-SNAPSHOT
 * @author -?
 */
@RequestMapping(value = "/signin", method = RequestMethod.GET)
public String refurlSignin() {
    Subject current_user = SecurityUtils.getSubject();
    current_user.logout();
    return "user/signin";
}