短視訊開發app,webservice自定義加入攔截器

zhibo系統開發發表於2022-03-28

短視訊開發app,webservice自定義加入攔截器實現的相關程式碼

1 pom.xml檔案

<project xmlns="
  <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自定義加入攔截器實現的相關程式碼, 更多內容歡迎關注之後的文章


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69978258/viewspace-2884517/,如需轉載,請註明出處,否則將追究法律責任。

相關文章