Brap 远程访问调用 和Spring整合(三)
??? Brap和Spring整合的学习,当需要验证时必须实现AuthenticationProvider接口中相关的方法:
public interface AuthorizationProvider { /** * The authorization call. Is made from the <code>ProxyServlet</code> * after an incoming invocation request is authenticated, and before the * method is invoked on the exposed service. * * If a successful auhtorization is made, true is returned. * * Normally the <code>AuthenticationContext#getPrincipal()</code> method is consulted * to retrieve the principal, so that the principal and the invocationRequest * can be matched. */ void authorize(InvocationRequest invocationRequest) throws AuthorizationFailedException;}??在Brap中提供的权限授权供应认证器(AuthorizationProvider):
?
AuthenticationNotRequiredAuthenticator:权限不需要认证的处理器。
SingleUsernamePasswordAuthenticator:单用户权限认证处理器。
DatabaseUsernamePasswordAuthenticator:数据库认证处理器。
?
在Brap中提供的权限认证处理提供者(AuthenticationProvider):
AuthenticationNotRequiredAuthorizer:不需要认证。
AuthenticationRequiredAuthorizer:需要认证。
?
服务接口:
package com.easyway.brap.spring.auth;/** * 服务端的接口 * @author longgangbai * */public interface HelloService { public String sayHello(String name);}?
?服务接口的实现:
package com.easyway.brap.spring.auth;import java.io.Serializable;import no.tornado.brap.auth.AnonymousPrincipal;import no.tornado.brap.auth.AuthenticationContext;import no.tornado.brap.common.UsernamePasswordPrincipal;/** * 服务端的接口的实现 * @author longgangbai * */public class HelloServiceImpl implements HelloService{ /** * 服务端的验证方法的实现 */ public String sayHello(String name) { //获取验证信息 Serializable principal = AuthenticationContext.getPrincipal(); //验证信息 if (principal instanceof UsernamePasswordPrincipal) { UsernamePasswordPrincipal upp = (UsernamePasswordPrincipal) AuthenticationContext.getPrincipal(); return "Hello there, " + name + ", your username is " + upp.getUsername() + " and your password is " + upp.getPassword(); //匿名验证 } else if (principal instanceof AnonymousPrincipal) { return "Hello there, " + name + ", you are authenticated anonymously."; } else { return "Hello there, " + name; } }}?
Spring配置如下:
<?xml version="1.0" encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <!-- 所要 暴露的服务--> <bean id="helloService" /> </property> <property name="authorizationProvider"> <ref bean="authorizationProvider"/> </property> </bean> </beans>
?
web.xml配置:
<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"><!-- Spring的配置信息 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 远程服务对应的Spring --><servlet><servlet-name>helloService</servlet-name><servlet-class>no.tornado.brap.spring.SpringProxyServlet</servlet-class> <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>helloService</servlet-name><url-pattern>/remoting/helloService</url-pattern></servlet-mapping></web-app>
?
?
?
客服端实现:
package com.easyway.brap.spring.auth;import no.tornado.brap.client.ServiceProxyFactory;import no.tornado.brap.common.UsernamePasswordPrincipal;/** * 服务器客户端的 * @author longgangbai * */public class HelloRemoteServiceClient {public static void main(String[] args) {UsernamePasswordPrincipal upp = new UsernamePasswordPrincipal("john", "secret");HelloService service = ServiceProxyFactory.createProxy(HelloService.class, "http://localhost:8080/BrapSpring/remoting/helloService", upp);System.out.println(service.sayHello("Hello"));}}?