有时候需要和老系统做一些的数据同步,如组织用户同步接口等。由于老系统使用的axis2的方式发布的webservice,所以我又整合了一下webservice 和伪单点。
项目地址
发布Webservice
1.准备工作
- 配置文件加入需要同步的webservice 信息
#平台登录url
platform_user_webserver_url=http://192.142.24.94/newlogin/services/singleLoginService
#组织用户推送
platform_info_webserver.url=http://192.142.24.94/newlogin/services/synInfoService
#qname
platform_user_webserver_url_qname=http://service.auth.taiji.com
#发布的webservice 地址
sso.url=http://127.0.0.1:8089/SsoWebservice
- 在pom.xml添加相关依赖
<!--axis2 begin-->
<dependency>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2-spring</artifactId>
<version>${axis2.version}</version>
</dependency>
<dependency>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2-transport-http</artifactId>
<version>${axis2.version}</version>
</dependency>
<dependency>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2-transport-local</artifactId>
<version>${axis2.version}</version>
</dependency>
<dependency>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2-xmlbeans</artifactId>
<version>${axis2.version}</version>
</dependency>
<!-- webservice -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
<version>1.5.2.RELEASE</version>
</dependency>
<dependency>
<groupId>wsdl4j</groupId>
<artifactId>wsdl4j</artifactId>
<version>1.6.3</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.4</version>
</dependency>
<!--poi-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.16</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>3.16</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.16</version>
</dependency>
2.在工程包sso下创建 SsoService 接口和SsoServiceImpl
使用RPCServiceClient 调用webservice
- SsoService
package com.taiji.dianthus.sso;
import javax.jws.WebMethod;
import javax.jws.WebService;
/**
* @ClassName SsoService
* @Description
* @Author H.M
* @Date 2020/4/1
*/
@WebService
public interface SsoService {
@WebMethod
public String SynchronizedInfo(int operateId, String operateCode);
}
- SsoServiceImpl
@WebService(targetNamespace = "http://renshi/services", name = "sso", serviceName = "ssoWebservice")
public class SsoServiceImpl implements SsoService {
@Autowired
private PasswordEncoder passwordEncoder;
@Value("${platform_info_webserver.url}")
private String platformInfoWebserverUrl;
@Value("${platform_user_webserver_url_qname}")
private String userWebserverUrlQname;
@Autowired
BeanConfig beanIns;
@Autowired
private UserRepository userRepository;
@Resource
private OrgRepository orgRepository;
@Resource
private RoleUserRepository roleUserRepository;
@Override
public String SynchronizedInfo(int operateId, String operateCode) {
if (operateId == 11 || operateId == 12) {
Map map = getUserOrOrg(operateCode, "getUserInfoByXml");
msg = saveOrUpdateUser(map);
}
//代码省略
......
}
private Map getUserOrOrg(String operateCode, String localPart) throws AxisFault, DocumentException {
RPCServiceClient serviceClient;
serviceClient = new RPCServiceClient();
Options options = serviceClient.getOptions();
options.setProperty(HTTPConstants.CONNECTION_TIMEOUT, new Integer(48000000));
EndpointReference targetEPR = new EndpointReference(platformInfoWebserverUrl + "?temp=00");
options.setTo(targetEPR);
options.setManageSession(true);
options.setProperty(HTTPConstants.REUSE_HTTP_CLIENT, true);
QName opGetAllLegalInfor = new QName(userWebserverUrlQname, localPart);
Object[] opGetAllLegalInforArgs = new Object[]{operateCode};
Class[] returnTypes = new Class[]{String.class};
Object[] response = serviceClient.invokeBlocking(opGetAllLegalInfor, opGetAllLegalInforArgs, returnTypes);
serviceClient.cleanupTransport();
String result = (String) response[0];
result = result.replace("<userinfo>", "").replace("</userinfo>", "");
return ResXmlUtil.parseXmlStr(result);
}
}
3.Endpoint来发布webservice
一般是通过启动类main方法来发布,但是由于需要打成war包在单独的中间件中运行,我又添加了启动类TomcatApplication,导致了DianthusApplication类中 main方法中SpringApplication.run(DianthusApplication.class, args) 这行代码之后的代码都不运行,所以使用@Configuration配置@Bean 注解 来发布webservice
在config 包下 新建BeanConfig 类,使用Endpoint.publish()方法发布webservice
@Configuration
public class BeanConfig {
@Value("${sso.url}")
private String ssoUrl;
@Bean(name = "beanIns")
public BeanConfig getBeanIns() {
BeanConfig bean = new BeanConfig();
return bean;
}
@Bean(name = "SsoServiceImpl")
public SsoService getSsoServiceImp() {
SsoService logService = new SsoServiceImpl();
return logService;
}
@Bean
public Endpoint endpoint() {
Endpoint endpoint = Endpoint.publish(ssoUrl, getSsoServiceImp());
System.out.println("发布单点登录webservice成功!service-addr: " + ssoUrl + "?wsdl");
return endpoint;
}
}
4.启动项目,访问http://127.0.0.1:8089/SsoWebservice?wsdl
出现如下界面 则表示接口发布成功
伪单点登录
实现步骤是
- 在SecurityConfig开放一个外部访问链接
- 系统通过链接带过来的userCode 查询数据库的用户信息
- 将用户信息放到UsernamePasswordAuthenticationToken中, 添加到Authentication认证, 完成SpringSecurity的认证。
.antMatchers("/ssoLogin").permitAll()
.antMatchers("/ssoLogin/*").permitAll()
/**
* http://localhost:7777/dianthus/ssoLogin?userCode=f07bf386da044ae48fe7bbd08478ceb6
* @param model
* @param userCode
* @param request
* @return
*/
@RequestMapping(value = "/ssoLogin")
public String ssoLogin(Model model, String userCode, HttpServletRequest request) {
System.out.println("用户唯一标识:" + userCode);
DSysUser user = userService.getByPushId(userCode);
// String roleId = roleUserService.getRoleIdByUserCode(user.getId());
// if (null == roleId) {
// return "pages/login_error_noRole";
// }
UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(user.getUserLoginName(),user.getUserPassword());
authentication.setDetails(new WebAuthenticationDetails((HttpServletRequest) request));
//UserDetails userDetails = userDetailsService.loadUserByUsername(user.getUserLoginName());
this.dAuthenticationProvider.setPmUserDetailService( dUserDetailService);
Authentication result = dAuthenticationProvider.authenticate(authentication);
SecurityContextHolder.getContext().setAuthentication(result);
model.addAttribute("userName",user.getName());
System.out.println("认证:"+SecurityContextHolder.getContext().getAuthentication());
return "redirect:/";
}
启动项目后 通过访问
http://localhost:7777/dianthus/ssoLogin?userCode=XXXXX可以免输入账号密码进入系统。
以上。