Spring +restful 构造webservice

[java]  view plain copy
  1. package com.jd.help.center.service.restful;  
  2.   
  3. import org.springframework.stereotype.Service;  
  4.   
  5. import javax.ws.rs.*;  
  6.   
  7. /** 
  8.  * Created with IntelliJ IDEA. 
  9.  * User: syyjy 
  10.  * Date: 13-7-24 
  11.  * Time: 上午9:06 
  12.  * To change this template use File | Settings | File Templates. 
  13.  */  
  14.   
  15. @Service("testWebService")  
  16. @Produces("application/json;charset=UTF-8")  
  17. @Consumes("application/json;charset=UTF-8")  
  18. @Path("/testWebService")  
  19. public class TestRestfulServiceImpl  implements  TestRestfulService{  
  20.     @Produces("application/json;charset=UTF-8")  
  21.     @GET  
  22.     @Path("/test")  
  23.     public String test(@QueryParam("name")  String name,@QueryParam("name")String id){  
  24.         return "Hello World" + "name :"+name +"id :"+id;  
  25.     }  
  26. }  

[html]  view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxrs="http://cxf.apache.org/jaxrs"  
  4.        xmlns:cxf="http://cxf.apache.org/core"  
  5.        xsi:schemaLocation="  
  6.     http://www.springframework.org/schema/beans  
  7.     http://www.springframework.org/schema/beans/spring-beans.xsd  
  8.     http://cxf.apache.org/jaxrs  
  9.     http://cxf.apache.org/schemas/jaxrs.xsd  
  10.     http://cxf.apache.org/core  
  11.     http://cxf.apache.org/schemas/core.xsd">  
  12.   
  13.     <import resource="classpath:META-INF/cxf/cxf.xml"/>  
  14.     <import resource="classpath:META-INF/cxf/osgi/cxf-extension-osgi.xml"/>  
  15.     <bean id="securityInterceptor" class="com.jd.help.center.service.restful.SecurityInterceptor">  
  16.         <property name="serverAuthHeader" ref="serverAuthHeader"/>  
  17.     </bean>  
  18.     <jaxrs:server id="restfulService" address="/">  
  19.         <jaxrs:features>  
  20.             <cxf:logging/>  
  21.         </jaxrs:features>  
  22.         <jaxrs:serviceBeans>  
  23.             <ref bean="testRestfulService"/>  
  24.         </jaxrs:serviceBeans>  
  25.         <jaxrs:inInterceptors>  
  26.             <ref bean="securityInterceptor"/>  
  27.         </jaxrs:inInterceptors>  
  28.     </jaxrs:server>  
  29.   
  30.     <bean id="serverAuthHeader" class="com.jd.help.center.service.restful.ServerAuthHeader">  
  31.         <property name="content" value="${help-center.restful.server.content}"/>  
  32.         <property name="seed" value="${help-center.restful.server.seed}"/>  
  33.     </bean>  
  34.     <bean id="testRestfulService" class="com.jd.help.center.service.restful.TestRestfulServiceImpl"/>  
  35. </beans>  

[java]  view plain copy
  1. package com.jd.help.center.service.restful;  
  2.   
  3. import com.jd.common.util.StringUtils;  
  4. import org.apache.commons.logging.Log;  
  5. import org.apache.commons.logging.LogFactory;  
  6. import org.apache.cxf.interceptor.Fault;  
  7. import org.apache.cxf.message.Message;  
  8. import org.apache.cxf.phase.AbstractPhaseInterceptor;  
  9. import org.apache.cxf.phase.Phase;  
  10. import org.apache.cxf.transport.http.AbstractHTTPDestination;  
  11. import org.springframework.beans.factory.annotation.Autowired;  
  12.   
  13. import javax.servlet.http.HttpServletRequest;  
  14.   
  15. /** 
  16.  * 服务端验证 
  17.  * <br>author:sunyilin@jd.com 
  18.  * <br>DateTime: 2012-12-5 下午03:28:51 
  19.  * <br>Version 1.0 
  20.  */  
  21. public class SecurityInterceptor extends AbstractPhaseInterceptor<Message> {  
  22.   
  23.     private static final Log LOGGER = LogFactory.getLog(SecurityInterceptor.class);  
  24.   
  25.   
  26.     private ServerAuthHeader serverAuthHeader;  
  27.   
  28.     public SecurityInterceptor() {  
  29.         this(Phase.RECEIVE);  
  30.     }  
  31.   
  32.     public SecurityInterceptor(String phase) {  
  33.         super(phase);  
  34.     }  
  35.   
  36.     public void handleMessage(Message message) throws Fault {  
  37.         // 获取当前的http请求  
  38.         HttpServletRequest request = (HttpServletRequest) message.get(AbstractHTTPDestination.HTTP_REQUEST);  
  39.         // 判断token  
  40.         String token = request.getHeader("token");  
  41.         if (!checkToken(token)) {  
  42.             LOGGER.error("SecurityInterceptor    ========    [Request Service] Invalid Token");  
  43.             throw new Fault(new Exception("Invalid Token"));  
  44.         }  
  45.         // 判断当前的请求方法是否是对应的合法 ip 地址  
  46.         String uri = (String) message.get(org.apache.cxf.message.Message.PATH_INFO);  
  47.         LOGGER.debug("SecurityInterceptor request =============URI:" + uri + "=============");  
  48.         String ip = request.getHeader("X-Forwarded-For");  
  49.         LOGGER.debug("SecurityInterceptor request =============X_FORWARDED_FOR IP:" + ip + "=============");  
  50.     }  
  51.   
  52.     /** 
  53.      * 校验token的正确性 
  54.      * 
  55.      * @param token 
  56.      * @return 
  57.      */  
  58.     private boolean checkToken(String token) {  
  59.         if (StringUtils.isEmpty(serverAuthHeader.getToken()) && StringUtils.isEmpty(token)) {  
  60.             return true;  
  61.         }  
  62.         if (!StringUtils.isEmpty(token) && token.equals(serverAuthHeader.getToken())) {  
  63.             return true;  
  64.         }  
  65.   
  66.         return false;  
  67.     }  
  68.   
  69.   
  70.     public ServerAuthHeader getServerAuthHeader() {  
  71.         return serverAuthHeader;  
  72.     }  
  73.   
  74.     public void setServerAuthHeader(ServerAuthHeader serverAuthHeader) {  
  75.         this.serverAuthHeader = serverAuthHeader;  
  76.     }  
  77.   
  78. }  

[java]  view plain copy
  1. package com.jd.help.center.service.restful;  
  2.   
  3. import org.apache.commons.codec.binary.Base64;  
  4. import org.apache.commons.lang.StringUtils;  
  5.   
  6. import java.security.MessageDigest;  
  7. import java.security.NoSuchAlgorithmException;  
  8.   
  9. /** 
  10.  * jd token 验证提取 
  11.  * <br>author: zhaotianming@360buy.com 
  12.  * <br>DateTime: 2012-12-5 下午03:39:21 
  13.  * <br>Version 1.0 
  14.  */  
  15. public class AuthHeader {  
  16.   
  17.     private String seed;  
  18.   
  19.     private String content;  
  20.   
  21.     private String token = "";  
  22.   
  23.     public void setToken(String token) {  
  24.         this.token = token;  
  25.     }  
  26.   
  27.     public String getSeed() {  
  28.         return seed;  
  29.     }  
  30.   
  31.     public void setSeed(String seed) {  
  32.         this.seed = seed;  
  33.     }  
  34.   
  35.     public String getContent() {  
  36.         return content;  
  37.     }  
  38.   
  39.     public void setContent(String content) {  
  40.         this.content = content;  
  41.     }  
  42.   
  43.     public String getToken() {  
  44.         if (StringUtils.isNotEmpty(content)) {  
  45.             if (StringUtils.isNotEmpty(seed)) {  
  46.                 byte[] bb = md5(content + "-" + seed);  
  47.                  System.out.print(new String(Base64.encodeBase64(bb)));      //todo 用完修改  
  48.                 return new String(Base64.encodeBase64(bb));  
  49.             } else {  
  50.                 return content;  
  51.             }  
  52.         }  
  53.         return token;  
  54.     }  
  55.   
  56.     //测试使用 上线 废除  
  57.  /*   public String getToken(){ 
  58.          return seed+content; 
  59.     }*/  
  60.   
  61.     private byte[] md5(String data) {  
  62.         try {  
  63.             return MessageDigest.getInstance("MD5").digest(data.getBytes());  
  64.         } catch (NoSuchAlgorithmException e) {  
  65.             throw new RuntimeException(e);  
  66.         }  
  67.     }  
  68.   
  69. }  

[html]  view plain copy
  1.         <?xml version="1.0" encoding="gb2312"?>  
  2. <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.          xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">  
  4. <display-name>help-center</display-name>  
  5. <listener>  
  6.     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  7. </listener>  
  8. <context-param>  
  9.     <param-name>contextConfigLocation</param-name>  
  10.     <param-value>classpath:spring-config.xml;classpath:spring-config-struts.xml</param-value>  
  11. </context-param>  
  12. <listener>  
  13.     <listener-class>com.jd.help.center.listener.CategoryListener</listener-class>  
  14. </listener>  
  15. <filter>  
  16.     <filter-name>UrlRewriteFilter</filter-name>  
  17.     <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>  
  18.     <init-param>  
  19.         <param-name>logLevel</param-name>  
  20.         <param-value>DEBUG</param-value>  
  21.     </init-param>  
  22. </filter>  
  23.   
  24. <filter>  
  25.     <filter-name>charsetFilter</filter-name>  
  26.     <filter-class>com.jd.common.web.filter.CharsetFilter</filter-class>  
  27. </filter>  
  28. <filter>  
  29.     <filter-name>struts2</filter-name>  
  30.     <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>  
  31. </filter>  
  32. <filter-mapping>  
  33.     <filter-name>charsetFilter</filter-name>  
  34.     <url-pattern>/*</url-pattern>  
  35. </filter-mapping>  
  36. <filter-mapping>  
  37.     <filter-name>struts2</filter-name>  
  38.     <url-pattern>*.action</url-pattern>  
  39.     <dispatcher>REQUEST</dispatcher>  
  40.     <dispatcher>FORWARD</dispatcher>  
  41. </filter-mapping>  
  42. <filter-mapping>  
  43.     <filter-name>UrlRewriteFilter</filter-name>  
  44.     <url-pattern>/*</url-pattern>  
  45.     <dispatcher>REQUEST</dispatcher>  
  46.     <dispatcher>FORWARD</dispatcher>  
  47. </filter-mapping>  
  48. <servlet>  
  49.     <servlet-name>velocitylayout</servlet-name>  
  50.     <servlet-class>org.apache.velocity.tools.view.servlet.VelocityViewServlet</servlet-class>  
  51. </servlet>  
  52.   
  53. <resource-ref>  
  54.     <description>helpCenterDataSource</description>  
  55.     <res-ref-name>jdbc/helpCenterDataSource</res-ref-name>  
  56.     <res-type>javax.sql.DataSource</res-type>  
  57.     <res-auth>Container</res-auth>  
  58. </resource-ref>  
  59. <welcome-file-list>  
  60.     <welcome-file>home.html</welcome-file>  
  61. </welcome-file-list>  
  62.   
  63. <servlet>  
  64.     <servlet-name>CXFServlet</servlet-name>  
  65.     <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>  
  66.     <load-on-startup>1</load-on-startup>  
  67. </servlet>  
  68. <servlet-mapping>  
  69.     <servlet-name>CXFServlet</servlet-name>  
  70.     <url-pattern>/services/*</url-pattern>  
  71. </servlet-mapping>  
  72. </web-app>  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值