Brap和Spring整合(简单权限验证)
???? 在使用Spring的发布WebService的实现,如果采用Brap实现更加简单,权限验证只需要添加相关的验证接口
服务接口:
package com.easyway.remoting.braps;/** * 服务实现的接口 * @author longgangbai * */public interface HelloService { public String sayHello(String name);}?
服务的实现类:
package com.easyway.remoting.braps;/** * 服务端的服务实现类 * @author longgangbai * */public class HelloServiceImpl implements HelloService { public String sayHello(String name) { return "Hello world, " + name; }}?spring的配置如下:
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:lang="http://www.springframework.org/schema/lang"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"default-autowire="no" default-lazy-init="true"><!-- 服务包装类--><bean id="helloRemoteService"ref="helloService" /><property name="authenticationProvider"ref="authenticationProvider" /><property name="authorizationProvider"ref="authorizationProvider" /></bean><!-- 服务端服务实现类 --><bean id="helloService"/><!-- 用户验证的信息--><bean id="authenticationProvider"value="admin" /><property name="password" value="admin" /></bean><!-- 权限验证器--><bean id="authorizationProvider"/></beans>
?
web.xml配置:
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4"><display-name>BRAP Test</display-name> <!-- 设置Spring的配置 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!-- 需要暴露spring的一种服务 --><servlet><servlet-name>hello</servlet-name><servlet-class>no.tornado.brap.spring.SpringProxyServlet</servlet-class><!-- 需要暴露的bean名称 --> <init-param> <param-name>beanName</param-name> <param-value>helloRemoteService</param-value> </init-param> <load-on-startup>1</load-on-startup></servlet> <!-- 暴露的路径名称 --><servlet-mapping><servlet-name>hello</servlet-name><url-pattern>/remoting/hello</url-pattern></servlet-mapping> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener></web-app>
?
客户端调用如下:
package com.easyway.ws.brap.service.client;import java.net.MalformedURLException;import no.tornado.brap.client.ServiceProxyFactory;import no.tornado.brap.common.UsernamePasswordPrincipal;import com.easyway.remoting.braps.HelloService;/** * 客户端调用代码 * @author longgangbai * */public class BrapClient {public static void main(String[] args) throws MalformedURLException {//客户端验证的用户名和密码UsernamePasswordPrincipal upp = new UsernamePasswordPrincipal("admin","admin");//客户端的服务信息HelloService service = ServiceProxyFactory.createProxy(HelloService.class,"http://localhost:8080/BrapServiceWS/remoting/hello", upp);//客户端的操作System.out.println(service.sayHello("Hello"));}}?
?
?