package com.njcn.shiro.filter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import java.io.PrintWriter;

public class ShiroFilterUtils {
	private static final Logger logger= LoggerFactory.getLogger(ShiroFilterUtils.class);
	final static Class<? extends ShiroFilterUtils> CLAZZ = ShiroFilterUtils.class;

	//踢出登录提示
	final static String KICKED_OUT = "/auth/kickedOut";
	//没有权限提醒
	final static String UNAUTHORIZED = "/auth/unauthorized";

	/**
	 * 是否是Ajax请求
	 * @param request
	 * @return
	 */
	public static boolean isAjax(ServletRequest request){
		return "XMLHttpRequest".equalsIgnoreCase(((HttpServletRequest) request).getHeader("X-Requested-With"));
	}


	/**
	 * response 输出JSON
	 */
	public static void out(ServletResponse response, String status){
		PrintWriter out = null;
		try {
			response.setCharacterEncoding("UTF-8");
			out = response.getWriter();
			out.println(status);
		} catch (Exception e) {
			logger.error("输出JSON报错。"+e.toString());
		}finally{
			if(null != out){
				out.flush();
				out.close();
			}
		}
	}
}
