怎么在springBoot中利用CXF实现用户名密码校验

  介绍

今天就跟大家聊聊有关怎么在springBoot中利用CXF实现用户名密码校验,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

<强>准备工作:

创建springBoot项目webservice_server

创建springBoot项目webservice_client

分别添加CXF的依赖:

& lt; !——, CXF  webservice ——比;   & lt; dependency>   & lt;才能groupId> org.apache.cxf</groupId>   & lt;才能artifactId> cxf-spring-boot-starter-jaxws</artifactId>   & lt;才能version> 3.1.11</version>   & lt;/dependency>   & lt; !——, CXF  webservice ——在

<强>一。定义要发布的接口和实现类

接口:

@ webservice   public  interface  AppService  {         @WebMethod才能   String 才能getUserName (@WebParam (=name “id"), String  id), throws  UnsupportedEncodingException;   @WebMethod才能   public 才能;User  getUser (String  id), throws  UnsupportedEncodingException;   }

实现类:

//名字暴露的服务名称,,targetNamespace:命名空间,设置为接口的包名倒写(默认是本类包名倒写)只endpointInterface接口地址=@ webservice (name “test",, targetNamespace =癶ttp://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 结果=,JSONResult.getJsonResult ();   ,,,result.setSuccess(真正的);   ,,,result.setMessage(“明哥“);   ,,,return  result.toJsonObject ();   ,,}   @Override才能   public 才能;User  getUser (String  id) throws  UnsupportedEncodingException  {   ,,,System.out.println (“===========================? id);   ,,,return  new 用户(1 l,“明哥“);   ,,}   }

<强>二。发布服务

1。定义配置类

@ configuration   public  class  CxfConfig  {//才能默认servlet路径/*,如果覆写则按照自己定义的来   ,@ bean   public 才能;ServletRegistrationBean  dispatcherServlet (), {   ,,,return  new  ServletRegistrationBean (CXFServlet(),由new “/服务/*“);   ,,}      @ bean才能(name =, Bus.DEFAULT_BUS_ID)   public 才能;SpringBus  springBus (), {   ,,,return  new  SpringBus ();   ,,}//把才能实现类交给弹簧管理   ,@ bean   public 才能;AppService  appService (), {   ,,,return  new  AppServiceImpl ();   ,,}//才能终端路径   ,@ bean   public 才能;Endpoint 端点(),{   ,,,EndpointImpl  endpoint =, new  EndpointImpl (springBus (),, appService ());   ,,,endpoint.getInInterceptors阀门()(new  AuthInterceptor());//添加校验拦截器   ,,,endpoint.publish (“/user");   ,,,return 端点;   ,,}   }

2。发布服务

@SpringBootApplication   public  class  WebserviceApplication  {      public 才能;static  void  main (String [], args), {   ,,,SpringApplication.run (WebserviceApplication.class, args);   ,,}   }

因为我添加了用户名和密码校验所以在发布之前还需要定义自己校验用户名和密码的拦截器

public  class  AuthInterceptor  extends  AbstractPhaseInterceptor, {   Logger 才能;Logger =, LoggerFactory.getLogger (this.getClass ());   private 才能static  final  String 用户名=皉oot";   private 才能static  final  String 密码=癮dmin";      public 才能;AuthInterceptor (), {   ,,,//定义在哪个阶段进行拦截   ,,,超级(Phase.PRE_PROTOCOL);   ,,}      @Override才能   public 才能;void  handleMessage (SoapMessage  soapMessage), throws  Fault  {   ,,,List
, headers =,空;   ,,,String 用户名=零;   ,,,String 密码=零;   ,,,try  {   ,,,,,headers =, soapMessage.getHeaders ();   ,,,},catch  (Exception  e), {   ,,,,,logger.error (“getSOAPHeader 错误:{}“,e.getMessage (), e);   ,,,}      null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null

怎么在springBoot中利用CXF实现用户名密码校验