一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - springBoot整合CXF并實現用戶名密碼校驗的方法

springBoot整合CXF并實現用戶名密碼校驗的方法

2021-05-25 13:11__sky_ Java教程

這篇文章主要介紹了springBoot整合CXF并實現用戶名密碼校驗的方法,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

準備工作:

創建springboot項目webservice_server

創建springboot項目webservice_client

分別添加cxf的依賴:

?
1
2
3
4
5
6
7
<!-- cxf webservice -->
<dependency>
  <groupid>org.apache.cxf</groupid>
  <artifactid>cxf-spring-boot-starter-jaxws</artifactid>
  <version>3.1.11</version>
</dependency>
<!-- cxf webservice -->

一.定義要發布的接口和實現類

接口:

?
1
2
3
4
5
6
7
8
9
@webservice
public interface appservice {
 
 
  @webmethod
  string getusername(@webparam(name = "id") string id) throws unsupportedencodingexception;
  @webmethod
  public user getuser(string id) throws unsupportedencodingexception;
}

實現類:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//name暴露的服務名稱, targetnamespace:命名空間,設置為接口的包名倒寫(默認是本類包名倒寫). endpointinterface接口地址
@webservice(name = "test" ,targetnamespace ="http://cxf.wolfcode.cn/" ,endpointinterface = "cn.wolfcode.cxf.appservice")
public class appserviceimpl implements appservice {
  jsonresult jsonresult = jsonresult.getjsonresult();
  @override
  public string getusername(string id) throws unsupportedencodingexception {
    system.out.println("==========================="+id);
    jsonresult result= jsonresult.getjsonresult();
    result.setsuccess(true);
    result.setmessage("明哥");
    return result.tojsonobject();
  }
  @override
  public user getuser(string id)throws unsupportedencodingexception {
    system.out.println("==========================="+id);
    return new user(1l,"明哥");
  }
}

二.發布服務

1.定義配置類

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
@configuration
public class cxfconfig {
  //默認servlet路徑/*,如果覆寫則按照自己定義的來
  @bean
  public servletregistrationbean dispatcherservlet() {
    return new servletregistrationbean(new cxfservlet(), "/services/*");
  }
 
  @bean(name = bus.default_bus_id)
  public springbus springbus() {
    return new springbus();
  }
 
  //把實現類交給spring管理
  @bean
  public appservice appservice() {
    return new appserviceimpl();
  }
 
  //終端路徑
  @bean
  public endpoint endpoint() {
    endpointimpl endpoint = new endpointimpl(springbus(), appservice());
    endpoint.getininterceptors().add(new authinterceptor());//添加校驗攔截器
    endpoint.publish("/user");
    return endpoint;
  }
}

2.發布服務

?
1
2
3
4
5
6
7
@springbootapplication
public class webserviceapplication {
 
  public static void main(string[] args) {
    springapplication.run(webserviceapplication.class, args);
  }
}

因為我添加了用戶名和密碼校驗所以在發布之前還需要定義自己校驗用戶名和密碼的interceptor

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
public class authinterceptor extends abstractphaseinterceptor<soapmessage> {
  logger logger = loggerfactory.getlogger(this.getclass());
  private static final string username="root";
  private static final string password="admin";
 
  public authinterceptor() {
    //定義在哪個階段進行攔截
    super(phase.pre_protocol);
  }
 
  @override
  public void handlemessage(soapmessage soapmessage) throws fault {
    list<header> headers = null;
    string username=null;
    string password=null;
    try {
      headers = soapmessage.getheaders();
    } catch (exception e) {
      logger.error("getsoapheader error: {}",e.getmessage(),e);
    }
 
    if (headers == null) {
      throw new fault(new illegalargumentexception("找不到header,無法驗證用戶信息"));
    }
    //獲取用戶名,密碼
    for (header header : headers) {
      soapheader soapheader = (soapheader) header;
      element e = (element) soapheader.getobject();
      nodelist usernamenode = e.getelementsbytagname("username");
      nodelist pwdnode = e.getelementsbytagname("password");
       username=usernamenode.item(0).gettextcontent();
       password=pwdnode.item(0).gettextcontent();
      if( stringutils.isempty(username)||stringutils.isempty(password)){
        throw new fault(new illegalargumentexception("用戶信息為空"));
      }
    }
    //校驗用戶名密碼
    if(!(username.equals(username) && password.equals(password))){
      soapexception soapexc = new soapexception("認證失敗");
      logger.debug("用戶認證信息錯誤");
      throw new fault(soapexc);
    }
  }
}

現在可以發布服務了.....

發布完成后訪問http://localhost:8888/services/user?wsdl

能夠出現以下界面就是發布ok

springBoot整合CXF并實現用戶名密碼校驗的方法

三.調用服務

1.新建調用端項目,添加依賴

2.因為示例演示了兩種調用方式,其中一種需要用到接口,所以先把服務接口拷貝一份到調用端項目中(代碼就是上面接口的代碼)

3.因為服務端添加了用戶名密碼校驗,所以調用的時候需要添加用戶名密碼信息, 所以需要使用下面的interceptor完成添加用戶名密碼信息

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/**
 * created by sky on 2018/2/27.
 */
public class logininterceptor extends abstractphaseinterceptor<soapmessage> {
  private string username="root";
  private string password="admin";
  public logininterceptor(string username, string password) {
    //設置在發送請求前階段進行攔截
    super(phase.prepare_send);
    this.username=username;
    this.password=password;
  }
 
  @override
  public void handlemessage(soapmessage soapmessage) throws fault {
    list<header> headers = soapmessage.getheaders();
    document doc = domutils.createdocument();
    element auth = doc.createelementns("http://cxf.wolfcode.cn/","securityheader");
    element username = doc.createelement("username");
    element userpass = doc.createelement("password");
 
    username.settextcontent(username);
    userpass.settextcontent(password);
 
    auth.appendchild(username);
    auth.appendchild(userpass);
 
    headers.add(0, new header(new qname("securityheader"),auth));
  }
}

4.調用接口

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/**
 * created by sky on 2018/2/27.
 */
public class cxfclient {
  //webservice接口地址
  private static string address = "http://localhost:8888/services/user?wsdl";
 
  //測試
  public static void main(string[] args) {
    test1();
    test2();
  }
 
  /**
   * 方式1:使用代理類工廠,需要拿到對方的接口
   */
  public static void test1() {
    try {
      // 代理工廠
      jaxwsproxyfactorybean jaxwsproxyfactorybean = new jaxwsproxyfactorybean();
      // 設置代理地址
      jaxwsproxyfactorybean.setaddress(address);
      //添加用戶名密碼攔截器
      jaxwsproxyfactorybean.getoutinterceptors().add(new logininterceptor("root","admin"));;
      // 設置接口類型
      jaxwsproxyfactorybean.setserviceclass(appservice.class);
      // 創建一個代理接口實現
      appservice cs = (appservice) jaxwsproxyfactorybean.create();
      // 數據準備
      string lineid = "1";
      // 調用代理接口的方法調用并返回結果
      user result = (user)cs.getuser(lineid);
      system.out.println("==============返回結果:" + result);
    } catch (exception e) {
      e.printstacktrace();
    }
  }
 
  /**
   * 動態調用方式
   */
  public static void test2() {
    // 創建動態客戶端
    jaxwsdynamicclientfactory dcf = jaxwsdynamicclientfactory.newinstance();
    client client = dcf.createclient(address);
    // 需要密碼的情況需要加上用戶名和密碼
     client.getoutinterceptors().add(new logininterceptor("root","admin"));
    object[] objects = new object[0];
    try {
      // invoke("方法名",參數1,參數2,參數3....);
      system.out.println("======client"+client);
      objects = client.invoke("getusername", "1");
      system.out.println("返回數據:" + objects[0]);
    } catch (exception e) {
      e.printstacktrace();
    }
  }
}

嗯...總體上就是這么簡單

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:https://blog.csdn.net/weixin_41138656/article/details/79393366

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 精品无码乱码AV | 久久99精品涩AV毛片观看 | 日韩在线二区 | 久久丫线这里只精品 | 亚洲欧美一级夜夜爽w | 日韩理论片在线看免费观看 | 日本肉体xxxx | bt天堂午夜国产精品 | 国产日日操 | 欧美成黑人性猛交xxoo | 黑人日白人 | 日本草草视频 | 亚洲卡一卡2卡三卡4麻豆 | 十六一下岁女子毛片免费 | 亚洲精品丝袜在线一区波多野结衣 | 欧美成黑人性猛交xxoo | 亚洲乱码一二三四五六区 | 国产乱叫456在线 | 国产高清露脸学生在线观看 | 91啦在线视频 | 国产精品成人免费观看 | 欧美一卡二卡科技有限公司 | 91探花在线观看 | 久久强奷乱码老熟女 | 娇妻被老外疯狂调教 | 91短视频版高清在线观看免费 | 羞羞私人影院可以直接免费观影吗 | 甜蜜调教 | 我的青梅竹马是消防员2季未增删免费 | aaa毛片视频免费观看 | 欧美猛男同志同性video | 欧洲美女啪啪 | 免费视频片在线观看 | 四虎影音在线 | 闺蜜调教我做她的脚奴 | 海角社区在线登录 | 妇乱子伦激情 | 好吊操这里有精品 | 高清在线一区二区 | 国产精品一区牛牛影视 | 精品欧美男同同性videos |