javaweb添加拦截器
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添加拦截器的更多相关文章
- spring boot 添加拦截器
构建一个spring boot项目. 添加拦截器需要添加一个configuration @Configuration @ComponentScan(basePackageClasses = Appli ...
- (七)CXF添加拦截器
今天开始讲下拦截器,前面大家学过servlet,struts2 都有拦截器概念,主要作用是做一些权限过滤,编码处理等: webservice也可以加上拦截器,我们可以给webservice请求加权限判 ...
- 使用CXF为webservice添加拦截器
拦截器分为Service端和Client端 拦截器是在发送soap消息包的某一个时机拦截soap消息包,对soap消息包的数据进行分析或处理.分为CXF自带的拦截器和自定义的拦截器 1.Servi ...
- SpringBoot如何添加拦截器
在web开发的过程中,为了实现登录权限验证,我们往往需要添加一个拦截器在用户的的请求到达controller层的时候实现登录验证,那么SpringBoot如何添加拦截器呢? 步骤如下: 1.继承Web ...
- CXF添加拦截器和自定义拦截器
前面讲了如何采用CXF开发webservice,现在来讲如何添加拦截器和自定义拦截器. 服务端代码: HelloWorld implementor=new HelloWorldImpl(); Stri ...
- (八)CXF之用spring添加拦截器
一.案例 本章案例是基于CXF之自定义拦截器基础之上改造的,目的是在服务端中用spring添加拦截器 配置web.xml <?xml version="1.0" encodi ...
- (五)CXF之添加拦截器
一.需求分析 webService中的拦截器类似于servlet的Filter过滤器.一般用于调用服务前后先调用拦截器的方法. 二.案例 本章案例是基于上一章节的基础上添加拦截器的 2.1 服务端添加 ...
- 在Java后端如何添加拦截器
在安全编码规范中,在Java后端controller层接口需要对调用者的身份进行确认,以防非法用户进行访问.若是在controller层的每个接口处都添加逻辑判断,那么代码重复度高,并且费力费时.此时 ...
- Spring Boot 2.X 如何添加拦截器?
最近使用SpringBoot2.X搭建了一个项目,大部分接口都需要做登录校验,所以打算使用注解+拦截器来实现,在此记录下实现过程. 一.实现原理 1. 自定义一个注解@NeedLogin,如果接口需要 ...
随机推荐
- SICK激光雷达LMS511测量数据说明
帧结构说明 LMS511的官方手册存在几个版本,在<Laser Measurement Systems of the LMS500 Product Family>的英文手册中,对单次(连续 ...
- Cache类缓存
此处主要总结System.Web.Caching.Cache类 该类是用于存储常用信息的类,HttpRuntime.Cache以及HttpContext.Current.Cache都是该类的实例. 该 ...
- alex python of day1
Print("hello word!")#打印hello word!向python世界发生第一声呐喊,仪式很重要 定义变量 name="Alex Li" nam ...
- SpringMVC 表单验证
SpringMVC 表单验证 本章节内容很丰富,主要有基本的表单操作,数据的格式化,数据的校验,以及提示信息的国际化等实用技能. 首先看效果图 项目结构图 接下来用代码重点学习SpringMVC的表单 ...
- Drools文档(八) 规则语言参考
规则语言参考 概述 Drools有一个"本地"的规则语言.这种格式在标点符号上非常轻,并且通过"扩展器"支持自然语言和领域特定的语言,使语言能够变形到您的问题领 ...
- 一个可扩展的深度学习框架的Python实现(仿keras接口)
一个可扩展的深度学习框架的Python实现(仿keras接口) 动机 keras是一种非常优秀的深度学习框架,其具有较好的易用性,可扩展性.keras的接口设计非常优雅,使用起来非常方便.在这里,我将 ...
- angularJS的一些用法
AngularJS 事件指令: ng-click/dblclick ng-mousedown/up ng-mouseenter/leave ng-mousemove/over/out ng-keydo ...
- 解决thymeleaf layout布局不生效
今天使用thymeleaf layout布局时总是不生效,特此把解决问题的步骤和几个关键点记录下来备忘. 一.检查依赖 1.thymeleaf必备maven依赖: <dependency> ...
- C# winform 程序开发知识点总结(干货)
1.数据库连接及操作 在说数据库操作之前,先说一下数据库连接操作字符串的获取 首先,点击服务器资源管理器,接下来选中数据连接右键点击添加连接,填入你要连接的服务器名称,点击单选框使用SQL Serve ...
- APP加固技术历程及未来级别方案:虚机源码保护
传统App加固技术,前后经历了四代技术变更,保护级别每一代都有所提升,但其固有的安全缺陷和兼容性问题始终未能得到解决.而下一代加固技术-虚机源码保护,适用代码类型更广泛,App保护级别更高,兼容性更强 ...