<commit_before>package org.scy.common.web.interceptor;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.scy.common.annotation.Auth;
import org.scy.common.configs.AppConfigs;
import org.scy.common.web.controller.HttpResult;
import org.scy.common.web.session.SessionManager;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.lang.reflect.Method;
import java.net.URLEncoder;

/**
 * 登录拦截器
 * Created by shicy on 2017/9/2
 */
@Component
public class LoginInterceptor extends HandlerInterceptorAdapter {

    // 系统配置信息
    protected AppConfigs configs;

    /**
     * 构造方法
     * @param configs
     */
    public LoginInterceptor(AppConfigs configs) {
        this.configs = configs;
    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        boolean validate = super.preHandle(request, response, handler);

        if (validate) {
            if (!handler.getClass().isAssignableFrom(HandlerMethod.class)) {
                return false;
            }

            Method method = ((HandlerMethod)handler).getMethod();
            Class cls = method.getDeclaringClass();
            if (method.isAnnotationPresent(Auth.class) ||  cls.isAnnotationPresent(Auth.class)) {
                if (!getSessionManager().isSessionValidate(request)) {
                    if (method.isAnnotationPresent(ResponseBody.class) || cls.isAnnotationPresent(ResponseBody.class)) {
                        this.writeWithNoAuth(response);
                    }
                    else {
                        this.gotoLogin(request, response);
                    }
                    return false;
                }
            }

            return true;
        }

        return false;
    }

    /**
     * 没有权限
     * @param response
     */
    private void writeWithNoAuth(HttpServletResponse response) throws Exception {
        response.setCharacterEncoding("utf-8");
        PrintWriter writer = null;
        try {
            writer = response.getWriter();<commit_msg>设置拦截器编码<commit_after>package org.scy.common.web.interceptor;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.scy.common.annotation.Auth;
import org.scy.common.configs.AppConfigs;
import org.scy.common.web.controller.HttpResult;
import org.scy.common.web.session.SessionManager;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.lang.reflect.Method;
import java.net.URLEncoder;

/**
 * 登录拦截器
 * Created by shicy on 2017/9/2
 */
@Component
public class LoginInterceptor extends HandlerInterceptorAdapter {

    // 系统配置信息
    protected AppConfigs configs;

    /**
     * 构造方法
     * @param configs
     */
    public LoginInterceptor(AppConfigs configs) {
        this.configs = configs;
    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        boolean validate = super.preHandle(request, response, handler);

        if (validate) {
            if (!handler.getClass().isAssignableFrom(HandlerMethod.class)) {
                return false;
            }

            Method method = ((HandlerMethod)handler).getMethod();
            Class cls = method.getDeclaringClass();
            if (method.isAnnotationPresent(Auth.class) ||  cls.isAnnotationPresent(Auth.class)) {
                if (!getSessionManager().isSessionValidate(request)) {
                    if (method.isAnnotationPresent(ResponseBody.class) || cls.isAnnotationPresent(ResponseBody.class)) {
                        this.writeWithNoAuth(response);
                    }
                    else {
                        this.gotoLogin(request, response);
                    }
                    return false;
                }
            }

            return true;
        }

        return false;
    }

    /**
     * 没有权限
     * @param response
     */
    private void writeWithNoAuth(HttpServletResponse response) throws Exception {
        response.setContentType("application/json;charset=utf-8");
        PrintWriter writer = null;
        try {
            writer = response.getWriter();
{'fraction_non_alphanumeric': 0.09581702039975273, 'fraction_numerical': 0.00329692973418504, 'mean_word_length': 2.517391304347826, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3316021', 'n_tokens_mistral': 1314, 'n_tokens_neox': 1303, 'n_words': 235}