短视频开发app,webservice自定义加入拦截器
短视频开发app,webservice自定义加入拦截器实现的相关代码
1 pom.xml文件
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.jaxWsService</groupId>
<artifactId>jaxWsService</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>01_jaxws_server</name>
<dependencies>
<!-- 要进行jaxws 服务开发 -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.0.1</version>
</dependency>
<!-- 内置jetty web服务器 -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http-jetty</artifactId>
<version>3.0.1</version>
</dependency>
<!-- 日志实现 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.12</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
<showWarnings>true</showWarnings>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
2.自定义拦截器
package util;
import java.util.List;
import org.apache.cxf.binding.soap.SoapMessage;
import org.apache.cxf.headers.Header;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.phase.AbstractPhaseInterceptor;
import org.apache.cxf.phase.Phase;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
public class ServiceInterceptor extends AbstractPhaseInterceptor<SoapMessage> {
private static String tokenKey="gacfbgjxybtrcbugaxbdgvjtertxbjFQXDVFVEWTFBA462432";
//在调用之前拦截
public ServiceInterceptor() {
super(Phase.PRE_INVOKE);
}
@Override
public void handleMessage(SoapMessage soap) throws Fault {
//获取请求头部信息
List<Header> headers = soap.getHeaders();
if(headers == null | headers.size()<1){
throw new Fault(new IllegalArgumentException("找不到Header,无法验证token信息"));
}
Header header = headers.get(0);
Element el = (Element)header.getObject();
NodeList tokens = el.getElementsByTagName("token");
if(tokens.getLength()<1){
throw new Fault(new IllegalArgumentException("无token信息"));
}
String token = tokens.item(0).getTextContent().trim();
//检查用户名和密码是否正确
if(!token.equals(tokenKey)) {
throw new Fault(new IllegalArgumentException("token验证失败"));
}else{
System.out.println("token验证成功");
}
//NodeList users = el.getElementsByTagName("username");
//NodeList passwords = el.getElementsByTagName("password");
////检查是否有用户名和密码元素
//if(users.getLength()<1){
//throw new Fault(new IllegalArgumentException("找不到用户信息"));
//}
//String username = users.item(0).getTextContent().trim();
//
//if(passwords.getLength()<1){
//throw new Fault(new IllegalArgumentException("找不到密码信息"));
//}
//String password = passwords.item(0).getTextContent();
//
////检查用户名和密码是否正确
//if(!"admin".equals(username) || !"admin".equals(password)){
//throw new Fault(new IllegalArgumentException("用户名或密码不正确"));
//}else{
//System.out.println("用户名密码正确允许访问");
//}
}
}
以上就是短视频开发app,webservice自定义加入拦截器实现的相关代码, 更多内容欢迎关注之后的文章
浙公网安备 33010602011771号