为webService添加Interceptor(拦截器)
今天写一个简单的拦截器,以webService接口为例:
背景:H5的一个项目,只要调用H5webService 接口下面的方法都会触发一个AuthorityInterceptor去验证是否调用类型是H5,session是否失效.
1.需要自己定义一个Interceptor,我定义的Interceptor去验证调用类型moduleType和session:
package com.lcc.h5.ws; import com.lcc.api.dto.session.SessionInfo;
import com.lcc.api.exception.AccessDeniedException;
import com.lcc.api.web.common.ModuleType;
import com.lcc.logger.Logger;
import com.lcc.logger.LoggerFactory;
import com.lcc.service.BaseAuthorityService;
import org.apache.commons.lang3.StringUtils;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.message.Message;
import org.apache.cxf.phase.AbstractPhaseInterceptor;
import org.apache.cxf.transport.http.AbstractHTTPDestination; import javax.servlet.http.HttpServletRequest; public class AuthorityInterceptor extends AbstractPhaseInterceptor<Message> { private static final Logger LOGGER = LoggerFactory.getLogger(AuthorityInterceptor.class); private BaseAuthorityService authorityService; public AuthorityInterceptor(String phase) {
super(phase);
} public AuthorityInterceptor() {
this("post-stream");
} @Override
public void handleMessage(Message message) throws Fault {
Fault fault = new Fault(new AccessDeniedException("illeagl moduleType access"));
fault.setStatusCode(421); HttpServletRequest httpRequest = (HttpServletRequest) message.get(AbstractHTTPDestination.HTTP_REQUEST);
String sessionId = httpRequest.getHeader("Token");
if (StringUtils.isBlank(sessionId)) {
LOGGER.info("blank session");
throw fault;
}
LOGGER.info("session authority, session id {}", sessionId); String moduleKey = httpRequest.getHeader("moduleType");
if (StringUtils.isEmpty(moduleKey)) {
LOGGER.info("moduleType is empty");
throw fault;
}
ModuleType module = ModuleType.fromKey(moduleKey); SessionInfo sessionInfo = null;
if (ModuleType.H5.equals(module)) {
sessionInfo = authorityService.getSessionInfo(sessionId);
if (sessionInfo == null) {
throw fault;
}
} else {
throw fault;
}
} public void setAuthorityService(BaseAuthorityService authorityService) {
this.authorityService = authorityService;
}
}
上面Interceptor用到的java bean:
public abstract class SessionInfo implements Serializable {
private static final long serialVersionUID = 6544973626519192604L;
private String key;
// timestamp
private Long createdAt;
// unit: second
private Long expiryTime;
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public Long getCreatedAt() {
return createdAt;
}
public void setCreatedAt(Long createdAt) {
this.createdAt = createdAt;
}
public Long getExpiryTime() {
return expiryTime;
}
public void setExpiryTime(Long expiryTime) {
this.expiryTime = expiryTime;
}
@Override
public String toString() {
return new StringBuilder().append("{key: ").append(key).append(", createdAt: ").append(createdAt)
.append(", expiryTime: ").append(expiryTime).append("}").toString();
}
}
=====================
为了防止别人恶意访问接口,我们可以给调用类型加密,内部调用直接传入加密后的String,在后台去转换验证即可.
public enum ModuleType {
H5("md5加密码");
private String key;
ModuleType(String key) {
this.key = key;
}
public String getKey() {
return key;
}
}
BaseAuthorityService及其实现类 请参考http://www.cnblogs.com/cc-java/p/6625998.html
2.Interceptor写好了,接下来就看下怎么在xml配置文件里面为webService配置Interceptor
<bean id="authorityInterceptor" class="com.lcc.ws.AuthorityInterceptor" >
<property name="authorityService" ref="authorityService" />
</bean> <bean id="authorityService" class="com.lcc.authority.AuthorityService" >
<property name="sessionTemplate" ref="redisSessionSerializationTemplate" />
</bean> <bean id="h5WebService" class="com.lcc.ws.impl.H5WebService">
</bean>
<jaxrs:server id="h5WebServiceContainer"
address="/h5">
<jaxrs:serviceBeans>
<ref bean="h5WebService" />
</jaxrs:serviceBeans> <jaxrs:providers>
<ref bean="wadlGenerator" />
<bean class="org.codehaus.jackson.jaxrs.JacksonJsonProvider">
<constructor-arg ref="handshakeJacksonMapper" />
</bean>
</jaxrs:providers> <jaxrs:inInterceptors>
<ref bean="fileSizeInterceptor" />
<ref bean="authorityInterceptor" />
</jaxrs:inInterceptors>
<jaxrs:outInterceptors>
<ref bean="outInterceptor" />
</jaxrs:outInterceptors>
</jaxrs:server>
到这里就已经为h5WebService接口配置好AuthorityInterceptor拦截器了;只要访问这个接口都会先进入拦截器里面去验证session和项目调用的类型;
为webService添加Interceptor(拦截器)的更多相关文章
- SpringMVC中使用Interceptor拦截器
SpringMVC 中的Interceptor 拦截器也是相当重要和相当有用的,它的主要作用是拦截用户的请求并进行相应的处理.比如通过它来进行权限验证,或者是来判断用户是否登陆,或者是像12306 那 ...
- SpringMVC 中的Interceptor 拦截器
1.配置拦截器 在springMVC.xml配置文件增加: <mvc:interceptors> <!-- 日志拦截器 --> <mvc:interceptor> ...
- SpringBoot-SpringMvc的Interceptor拦截器配置
Interceptor拦截器实现对每一个用户请求处理前后的业务处理,比如我们需要对用户请求进行响应时间的记录,需要记录请求从开始到结束所耗的时间,这时我们就需要用到拦截器了 下面我们以记录请求处理时间 ...
- Spring中的Interceptor 拦截器 专题
spring-webmvc-4.3.14.RELEASE.jar org.springframework.web.servlet.DispatcherServlet#doDispatch /** * ...
- Spring MVC中使用Interceptor拦截器
SpringMVC 中的Interceptor 拦截器也是相当重要和相当有用的,它的主要作用是拦截用户的请求并进行相应的处理.比如通过它来进行权限验证,或者是来判断用户是否登陆,或者是像12306 那 ...
- SpringMvc中Interceptor拦截器用法
SpringMVC 中的Interceptor 拦截器也是相当重要和相当有用的,它的主要作用是拦截用户的请求并进行相应的处理.比如通过它来进行权限验证,或者是来判断用户是否登陆等. 一. 使用场景 1 ...
- SpringMVC中使用Interceptor拦截器顺序
一.简介 SpringMVC 中的Interceptor 拦截器也是相当重要和相当有用的,它的主要作用是拦截用户的请求并进行相应的处理.比如通过它来进行权限验 证,或者是来判断用户是否登陆,或者是像1 ...
- Vue添加请求拦截器
一.现象 统一处理错误及配置请求信息 二.解决 1.安装 axios , 命令: npm install axios --save-dev 2.在根目录的config目录下新建文件 axios.js ...
- [代码笔记]VUE路由根据返回状态判断添加响应拦截器
//返回状态判断(添加响应拦截器) Axios.interceptors.response.use( res => { //对响应数据做些事 if (res.data && !r ...
随机推荐
- Visual Studio下__cplusplus宏为199711L的问题
Visual Studio下__cplusplus宏为199711L的问题 / Zc:__ cplusplus(启用更新的__cplusplus宏) 该/ ZC:__ CPLUSPLUS编译器选项使_ ...
- node之events 模块,并通过实例化 EventEmitter 类来绑定和监听事件
例子来源:http://www.runoob.com/nodejs/nodejs-event-loop.html http://www.runoob.com/nodejs/nodejs-event.h ...
- HGOI20190815 题解
Problem A modsum 求$\sum\limits_{i=1}^{n} \sum\limits_{j=1 , i \neq j}^{m} (n \ mod \ i)(m \ mod \ j) ...
- C++自动糖果贩卖机
#include<map> #include<vector> #include<cstdio> #include<iostream> #include& ...
- 把execel表数据导入mysql数据库
今天,是我来公司第二周的第一天. 作为新入职的实习生,目前还没适合我的实质项目工作,今天的学习任务是: 把execel表数据导入到mysql数据库,再练习下java操作JDBC. 先了解下execel ...
- Redis数据结构常用命令
Redis数据结构--字符串
- JavaBean,EJB,POJO,Spring Bean 的演进历程
JavaBean Sun公司对类提出的规范:1,类是public的2,有一个无参构造方法3,属性修饰要用private,通过get set操作4,实现Serializable接口5,对事件使用Swin ...
- Docker安装Zookeeper并进行操作
Docker安装Zookeeper 下载Zookeeper镜像docker pull zookeeper1启动容器并添加映射docker run --privileged=true -d --name ...
- 数据库-MongoDb
*本文总结下使用Mongodb遇到的问题: 1. 安装完MongoDb后先启动服务端,然后再启动客户端: 直接上图: 注意点:mongod.exe :是用来连接到mongo数据库服务器的,即服务器端. ...
- IDEA全局配置
进入全局设置界面: 取消每次启动IDEA就默认打开上一次最后关闭的项目 编译器代码字体设置: 控制台字体大小和颜色设置 同一个文件代码里面的各个不同方法之间显示分割线 代码自动提示不区分大小写 格式化 ...