java spring 配置登陆拦截器

spring配置文件中添加

<!-- 后端拦截器——是否登录 -->
<mvc:interceptor>
    <mvc:mapping path="/portal/**"/>
    <bean class="com.connxun.config.interceptor.CheckLoginInterceptor"/>
</mvc:interceptor>
import com.connxun.util.json.JsonUtil;
import com.connxun.util.session.SessionUtil;
import com.connxun.util.session.UserSession;
import com.connxun.util.string.StringUtil;
import com.connxun.util.web.WebUtil;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

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

/**
 * 功能描述:校验是否登录拦截器
 *
 * @author 高宇飞
 * @version 1.0.0
 */
public class CheckLoginInterceptor implements HandlerInterceptor {

    /**
     * 操作前先判断是否登录,未登录跳转到登录界面
     */
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        UserSession userSession = SessionUtil.getUserSession(request);
        if (userSession == null) {
            boolean isAjaxRequest = StringUtil.checkAjaxRequest(request);
            if (isAjaxRequest) {
                Message msg = new Message();
                msg.setSuccess(false);
                msg.setExName("错误提示");
                msg.setMsgText("连接超时,请重新登录");
                msg.setIsSessionOut(true);
                WebUtil.out(response, JsonUtil.toStr(msg));
            } else {
                String str = "<script>top.location.href='" + request.getContextPath() + "/login.do'</script>";
                WebUtil.out(response, str);
            }
            return false;
        } else {
            return true;
        }

    }

    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值