首页>代码>ssm开发组织机构后台管理系统>/human/src/main/java/com/ycxm/controller/BaseController.java
package com.ycxm.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ExceptionHandler;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.ycxm.pojo.SysUser;



public class BaseController {
	
	@Autowired
	private HttpServletRequest request;
	
	/**
	 * 分页查询
	 */
	protected void startPage() {
		String page = request.getParameter("page");
		String rows = request.getParameter("rows");
		String sort = request.getParameter("sort");
		String order= request.getParameter("order");
		String orderBy=null;
		if(order!=""&&order!=null) {
			orderBy = sort +" "+order;
		}
		
		Integer pageNum = 1;
		Integer pageSize = 10;
		if (page != null && page != "") {
			pageNum = Integer.parseInt(page);
		}
		if (rows != null && rows != "") {
			pageSize = Integer.parseInt(rows);
		}
		PageHelper.startPage(pageNum, pageSize,orderBy);
	};
	
	/**
	 * 获取当前用户
	 * @return
	 */
	public static SysUser getCurrentUser() {
		  SysUser user= (SysUser) SecurityUtils.getSubject().getPrincipal();
		  return user;
	}
	
	
	@ExceptionHandler({ UnauthorizedException.class, AuthorizationException.class })
    public String authorizationException(HttpServletRequest request, HttpServletResponse response) {
        if (isAjaxRequest(request)) {
            // 输出JSON
            Map<String,Object> map = new HashMap<String,Object>();
            map.put("MSG", "-998");
            map.put("message", "无权限");
            writeJson(map, response);
            return null;
        } else {
            return "redirect:/403.do";
        }
    }

    /**
     * 输出JSON
     */
    private void writeJson(Map<String,Object> map, HttpServletResponse response) {
        PrintWriter out = null;
        ObjectMapper json = new ObjectMapper();
        try {
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/json; charset=utf-8");
            out = response.getWriter();
            out.write(json.writeValueAsString(map));
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (out != null) {
                out.close();
            }
        }
    }
    
    
    
    private  boolean isAjaxRequest(HttpServletRequest request) {
        String requestedWith = request.getHeader("x-requested-with");
        if (requestedWith != null && requestedWith.equalsIgnoreCase("XMLHttpRequest")) {
            return true;
        } else {
            return false;
        }
    }
	
	
}
最近下载更多
zxc131313  LV12 2022年10月22日
45618955  LV8 2022年6月16日
我是helloworld  LV23 2022年5月25日
wuying8208  LV15 2022年5月12日
893213895  LV18 2021年12月19日
噜噜啦啦露露  LV8 2021年12月14日
wanglinddad  LV54 2021年12月10日
志犟CR7  LV3 2021年11月10日
苏屿999  LV9 2021年10月18日
最代码-宋家辉  LV61 2021年8月22日
最近浏览更多
wddq123 4月3日
暂无贡献等级
minjing123  LV8 1月4日
WBelong  LV7 2023年12月26日
shuangfu  LV24 2023年12月2日
uni-code_0123  LV1 2023年11月27日
政政123456  LV3 2023年10月22日
泓鼎168  LV19 2023年10月16日
wwewww  LV4 2023年10月15日
zhangjianghe 2023年9月7日
暂无贡献等级
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友