其他分享
首页 > 其他分享> > hessian客户端调用服务端测试类

hessian客户端调用服务端测试类

作者:互联网

1,pom.xml

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
        </exclusion>
    </exclusions>
</dependency>

<dependency>
    <groupId>com.caucho</groupId>
    <artifactId>hessian</artifactId>
    <version>4.0.38</version>
</dependency>

2,服务端

2-1 接口类

package hessian.server;

public interface MessageHessianService {

    /**
     * 通知更新用户状态
     */
    String noticePasswordUpdate(String json);

}

2-2 接口实现类

package hessian.server;

public interface MessageHessianServiceImpl  {

    /**
     * 通知更新用户状态
     */

    public String noticePasswordUpdate(String json){
        log.info("触发接口-通知更新用户状态:{}",json);
        if(StringUtils.isEmpty(json)){
            return "0";
        }
        return "1";
    }
}

2-3 发布

package hessian.server.config;

import bynonco.com.shareehelper.service.MessageHessianService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.remoting.caucho.HessianServiceExporter;

/**
 * hessian作为服务端
 * 需要暴露的接口
 */
@Configuration
public class HessianExporterConfig {

    @Autowired
    private MessageHessianService messageHessianService;

    @Bean(name = "/server/messageHessianService.hessian")
    public HessianServiceExporter messageHessianService() {
        HessianServiceExporter exporter = new HessianServiceExporter();
        exporter.setService(messageHessianService);
        exporter.setServiceInterface(MessageHessianService.class);
        return exporter;
    }

}

2-3 过滤器拦截处理Header,可忽略

package hessian.server.security;

import hessian.server.security.JwtAuthenticationToken;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;

/**
 * 授权提供者
 * 实现jwt头部信息的token转化成我们自己的授权对象AuthTokenDetails。
 * 然后把解析之后的对象返回给Spring Security。
 */
public class JwtAuthenticationProvider implements AuthenticationProvider {

    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {

        JwtAuthenticationToken jwtAuthenticationToken = (JwtAuthenticationToken) authentication;
        String token = jwtAuthenticationToken.getToken();

        // ....解析token
        return authentication;
    }

    @Override
    public boolean supports(Class<?> authentication) {
        return authentication.equals(JwtAuthenticationToken.class);
    }

}
View Code
JwtAuthenticationToken类设计:
package hessian.server.security;

import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;
import java.util.Collection;

public class JwtAuthenticationToken extends UsernamePasswordAuthenticationToken {

    private String token;

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }

    public JwtAuthenticationToken(String token) {
        super(null,null,null);
        this.token = token;
    }

    public JwtAuthenticationToken(Object principal, Object credentials, String token) {
        super(principal, credentials);
        this.token = token;
    }

    public JwtAuthenticationToken(Object principal, Object credentials, Collection<? extends GrantedAuthority> authorities, String token) {
        super(principal, credentials, authorities);
        this.token = token;
    }

}
View Code

3,客户端

3-1 接口类- 必须和服务端接口设计一致

package hessian.client;

public interface MessageHessianService implements MessageHessianService {

    /**
     * 通知更新用户状态
     */
    String noticePasswordUpdate(String json);

}

3-2 HessianProxyFactoryUtil 工厂类

package hessian.client;

import hessian.client.HessianClientProxyFactory;
import com.caucho.hessian.client.HessianProxyFactory;
import java.util.HashMap;
import java.util.Map;


public class HessianProxyFactoryUtil {

    public final static String url = "http://localhost:8080/api/server/messageHessianService.hessian";

    private final static HessianProxyFactory factory = new HessianProxyFactory();

    private static Map<Class,Object> hessianBeans = new HashMap<>();

    /**
     *  获取调用端对象
     * @param clazz 实体对象泛型
     * @param <T>
     * @return 业务对象
     */
    public static <T> T getHessianClientBean(Class<T> clazz, String code) throws Exception{
        factory.setOverloadEnabled(true);
        String token = UnitTokenTest.createToken(code); // 该类自定义创建token,可忽略
        HessianClientProxyFactory hessianConnectionFactory = new HessianClientProxyFactory(token);
        hessianConnectionFactory.setHessianProxyFactory(factory);
        factory.setConnectionFactory(hessianConnectionFactory);
        T result = null;
        if(!hessianBeans.containsKey(clazz)) {
            result = (T)factory.create(clazz,url);
            hessianBeans.put(clazz, result);
        }else {
            result = (T)hessianBeans.get(clazz);
        }
        return result;
    }

    /**
     * 移除hessionClientBean
     * @param clazz
     */
    public static void removeHessianClientBean(Class clazz) {
        if(hessianBeans.containsKey(clazz)) {
            hessianBeans.remove(clazz);
        }
    }
}

3-3 测试连接

 

package hessian.client;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.List;


@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class MessageHessianClientTest {

    /**
     * 测试用户密码变更通知
     */
    @Test
    public void noticePasswordUpdate(){
        String code = "EEEE";
        String sid = "123456";
        try {
            MessageHessianService messageHessianService = HessianProxyFactoryUtil.getHessianClientBean(MessageHessianService.class,code);
            String msg = messageHessianService.noticePasswordUpdate(sid);
            ConsoleLogger.info("触发接口-通知更新用户状态:{}",sid);
            Assert.assertTrue(msg.equals("1"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

 

 

标签:String,token,服务端,org,import,public,hessian,客户端
来源: https://www.cnblogs.com/zhutouying/p/11677702.html