js请求后台代码添加拦截器:

package com.ctzj.biz.isale.deploy.controller;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.List;
import java.util.Map; import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession; import org.apache.curator.utils.ZKPaths;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.socket.WebSocketSession; import com.ctzj.biz.isale.deploy.config.ApplicationProperties;
import com.ctzj.biz.isale.deploy.model.Menu;
import com.ctzj.biz.isale.deploy.model.ResponseResult;
import com.ctzj.biz.isale.deploy.model.dataobject.DeployConfigDo;
import com.ctzj.biz.isale.deploy.model.query.DeployConfigQuery;
import com.ctzj.biz.isale.deploy.service.DeployConfigService;
import com.ctzj.biz.isale.deploy.service.MenuConfigService;
import com.ctzj.biz.isale.deploy.util.Interceptor;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps; @RestController
@RequestMapping(value = "/dispatcher")
public class DispatcherController extends WebMvcConfigurerAdapter{ @Autowired
private DeployConfigService deployConfigService; @Autowired
private MenuConfigService menuConfigService;
@Autowired
private ApplicationProperties properties; /**
* 登录验证
* @param request
* @param response
* @return
* @throws UnsupportedEncodingException
*/
@RequestMapping("/login")
@ResponseBody
public ResponseResult<String> login(HttpServletRequest request,
HttpServletResponse response,String username,String password) throws UnsupportedEncodingException {
HttpSession session = request.getSession(); if(properties.getPstUsername().equals(username)&&properties.getPstPassword().equals(password)){
session.setAttribute("user", username);
}else if(properties.getProdUsername().equals(username)&&properties.getProdPassword().equals(password)){
session.setAttribute("user", username);
}else if(properties.getSuperUsername().equals(username)&&properties.getSuperPassword().equals(password)){
session.setAttribute("user", username);
}else{
username="用户名或密码错误!!!";
return ResponseResult.buildFailure(username);
} return ResponseResult.buildSuccessInstance
(username);
} public void addInterceptors(InterceptorRegistry registry) {
// 多个拦截器组成一个拦截器链
// addPathPatterns 用于添加拦截规则
// excludePathPatterns 用户排除拦截
registry.addInterceptor(new Interceptor()).addPathPatterns("/dispatcher/*").excludePathPatterns("/dispatcher/login");
super.addInterceptors(registry);
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
super.addResourceHandlers(registry);
} @Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/toLogin").setViewName("login.html");
super.addViewControllers(registry);
} }

只要继承

WebMvcConfigurerAdapter类后重写addInterceptors方法
再新建拦截器的类
package com.ctzj.biz.isale.deploy.util;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession; import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView; import com.ctzj.biz.isale.deploy.websocket.LocalShellThread; /**
* Created by 风流丶沙.
* Date :2017/10/25 9:36
*/
public class Interceptor implements HandlerInterceptor {
private static final Logger LOG = LoggerFactory.getLogger(LocalShellThread.class); @Override
public void afterCompletion(HttpServletRequest arg0,
HttpServletResponse arg1, Object arg2, Exception arg3)
throws Exception {
// TODO Auto-generated method stub } @Override
public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
Object arg2, ModelAndView arg3) throws Exception {
// TODO Auto-generated method stub } @Override
public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1,
Object arg2) throws Exception {
LOG.info("method={}",arg0.getMethod());
String role = (String) arg0.getSession().getAttribute("user");
if(null==role||"".equals(role)){
arg1.sendRedirect("/login.html");
// arg0.getRequestDispatcher("/login.html").forward(arg0, arg1);
return false;
}else{
return true;
}
} }
preHandle这个方法再调用controller前调用,返回true继续执行下面代码,返回false不执行后面的代码!
 

javaweb添加拦截器的更多相关文章

  1. spring boot 添加拦截器

    构建一个spring boot项目. 添加拦截器需要添加一个configuration @Configuration @ComponentScan(basePackageClasses = Appli ...

  2. (七)CXF添加拦截器

    今天开始讲下拦截器,前面大家学过servlet,struts2 都有拦截器概念,主要作用是做一些权限过滤,编码处理等: webservice也可以加上拦截器,我们可以给webservice请求加权限判 ...

  3. 使用CXF为webservice添加拦截器

      拦截器分为Service端和Client端 拦截器是在发送soap消息包的某一个时机拦截soap消息包,对soap消息包的数据进行分析或处理.分为CXF自带的拦截器和自定义的拦截器 1.Servi ...

  4. SpringBoot如何添加拦截器

    在web开发的过程中,为了实现登录权限验证,我们往往需要添加一个拦截器在用户的的请求到达controller层的时候实现登录验证,那么SpringBoot如何添加拦截器呢? 步骤如下: 1.继承Web ...

  5. CXF添加拦截器和自定义拦截器

    前面讲了如何采用CXF开发webservice,现在来讲如何添加拦截器和自定义拦截器. 服务端代码: HelloWorld implementor=new HelloWorldImpl(); Stri ...

  6. (八)CXF之用spring添加拦截器

    一.案例 本章案例是基于CXF之自定义拦截器基础之上改造的,目的是在服务端中用spring添加拦截器 配置web.xml <?xml version="1.0" encodi ...

  7. (五)CXF之添加拦截器

    一.需求分析 webService中的拦截器类似于servlet的Filter过滤器.一般用于调用服务前后先调用拦截器的方法. 二.案例 本章案例是基于上一章节的基础上添加拦截器的 2.1 服务端添加 ...

  8. 在Java后端如何添加拦截器

    在安全编码规范中,在Java后端controller层接口需要对调用者的身份进行确认,以防非法用户进行访问.若是在controller层的每个接口处都添加逻辑判断,那么代码重复度高,并且费力费时.此时 ...

  9. Spring Boot 2.X 如何添加拦截器?

    最近使用SpringBoot2.X搭建了一个项目,大部分接口都需要做登录校验,所以打算使用注解+拦截器来实现,在此记录下实现过程. 一.实现原理 1. 自定义一个注解@NeedLogin,如果接口需要 ...

随机推荐

  1. springmvc跨域+token验证(app后台框架搭建二)

    这是app后台框架搭建的第二课,主要针对app应用是跨域的运用,讲解怎么配置跨域服务:其次讲解怎么进行token验证,通过拦截器设置token验证和把token设置到http报文中.主要有如下:   ...

  2. java面向对象整理

    1.局部变量与全局变量的区别 区别一:定义的位置不同 定义在类中的变量是成员变量 定义在方法中或者{}语句里面的变量是局部变量定义 区别二:在内存中的位置不同 成员变量存储在对内存的对象中 局部变量存 ...

  3. 使用vue2.x+webpack+vuex+sass+axios+elementUI等快速搭建前端项目框架

    一.本文将分享如何快速搭起基于webpack+vue的前端项目框架,利用vue的自己的脚手架工具vue-cli搭建起基本的环境配置,再通过npm包管理工具引入相应的依赖来完善项目的各种依赖框架.下面是 ...

  4. jquery总结(来自于一个讲师的总结)

    选择器 基本选择器:id class 标签 eq()查找具体的列表中的元素:$('ul li:eq(n)').eq(n) 层 :div p,div>p 查找:find 选中元素中再查找子元素,p ...

  5. CCLuaObjcBridge - Lua 与 Objective-C 互操作的简单解决方案

    http://dualface.github.io/blog/2013/01/27/call-objectivec-from-lua/ 月初的时候,发了一篇关于 Lua 与 Java 互操作的文章,里 ...

  6. 08-图8 How Long Does It Take

    原题: Given the relations of all the activities of a project, you are supposed to find the earliest co ...

  7. 软件测试管理QC

    一.QC简介 1)是HP公司的产品,是B/S结构的产品 2)在QC服务器中,打开IE浏览器,在地址栏中输入QC服务器的网址或者IP地址. 查看虚拟机的IP地址: 本地连接-属性-TCP/IP协议(重点 ...

  8. TeamTalk安装测试

    TeamTalk介绍 项目框架 TeamTalk是蘑菇街的开源项目,github维护的最后时间是2015但是仍然是一款值得学习的好项目,麻雀虽小五脏俱全,本项目涉及到多个平台.多种语言,简单关系如下图 ...

  9. angularjs 给封装的模态框元素传值,和实现兄弟传值

    本例实现封装的元素所放的位置不同,而选择不同的传值,这里举例封装了bootstrap模态框,以后也方便大家去直接使用.方法举例如下:首先主页调用css/js有: <link rel=" ...

  10. NGUI_Input

    九.输入框Input 1.凡是用户可以输入文本的地方,几乎都用输入框,有登录账号和密码.输入角色名称.输入聊天内容 2.手动拼接输入框,拖动预制体的就不再说了 (1).创建一个Sprite作为输入框的 ...