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 LV55
2021年12月10日
志犟CR7 LV8
2021年11月10日
苏屿999 LV9
2021年10月18日
最代码-宋家辉 LV61
2021年8月22日

最近浏览