- package com.jd.help.center.service.restful;
- import org.springframework.stereotype.Service;
- import javax.ws.rs.*;
- /**
- * Created with IntelliJ IDEA.
- * User: syyjy
- * Date: 13-7-24
- * Time: 上午9:06
- * To change this template use File | Settings | File Templates.
- */
- @Service("testWebService")
- @Produces("application/json;charset=UTF-8")
- @Consumes("application/json;charset=UTF-8")
- @Path("/testWebService")
- public class TestRestfulServiceImpl implements TestRestfulService{
- @Produces("application/json;charset=UTF-8")
- @GET
- @Path("/test")
- public String test(@QueryParam("name") String name,@QueryParam("name")String id){
- return "Hello World" + "name :"+name +"id :"+id;
- }
- }
- <?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:jaxrs="http://cxf.apache.org/jaxrs"
- xmlns:cxf="http://cxf.apache.org/core"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans.xsd
- http://cxf.apache.org/jaxrs
- http://cxf.apache.org/schemas/jaxrs.xsd
- http://cxf.apache.org/core
- http://cxf.apache.org/schemas/core.xsd">
- <import resource="classpath:META-INF/cxf/cxf.xml"/>
- <import resource="classpath:META-INF/cxf/osgi/cxf-extension-osgi.xml"/>
- <bean id="securityInterceptor" class="com.jd.help.center.service.restful.SecurityInterceptor">
- <property name="serverAuthHeader" ref="serverAuthHeader"/>
- </bean>
- <jaxrs:server id="restfulService" address="/">
- <jaxrs:features>
- <cxf:logging/>
- </jaxrs:features>
- <jaxrs:serviceBeans>
- <ref bean="testRestfulService"/>
- </jaxrs:serviceBeans>
- <jaxrs:inInterceptors>
- <ref bean="securityInterceptor"/>
- </jaxrs:inInterceptors>
- </jaxrs:server>
- <bean id="serverAuthHeader" class="com.jd.help.center.service.restful.ServerAuthHeader">
- <property name="content" value="${help-center.restful.server.content}"/>
- <property name="seed" value="${help-center.restful.server.seed}"/>
- </bean>
- <bean id="testRestfulService" class="com.jd.help.center.service.restful.TestRestfulServiceImpl"/>
- </beans>
- package com.jd.help.center.service.restful;
- import com.jd.common.util.StringUtils;
- import org.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
- import org.apache.cxf.interceptor.Fault;
- import org.apache.cxf.message.Message;
- import org.apache.cxf.phase.AbstractPhaseInterceptor;
- import org.apache.cxf.phase.Phase;
- import org.apache.cxf.transport.http.AbstractHTTPDestination;
- import org.springframework.beans.factory.annotation.Autowired;
- import javax.servlet.http.HttpServletRequest;
- /**
- * 服务端验证
- * <br>author:sunyilin@jd.com
- * <br>DateTime: 2012-12-5 下午03:28:51
- * <br>Version 1.0
- */
- public class SecurityInterceptor extends AbstractPhaseInterceptor<Message> {
- private static final Log LOGGER = LogFactory.getLog(SecurityInterceptor.class);
- private ServerAuthHeader serverAuthHeader;
- public SecurityInterceptor() {
- this(Phase.RECEIVE);
- }
- public SecurityInterceptor(String phase) {
- super(phase);
- }
- public void handleMessage(Message message) throws Fault {
- // 获取当前的http请求
- HttpServletRequest request = (HttpServletRequest) message.get(AbstractHTTPDestination.HTTP_REQUEST);
- // 判断token
- String token = request.getHeader("token");
- if (!checkToken(token)) {
- LOGGER.error("SecurityInterceptor ======== [Request Service] Invalid Token");
- throw new Fault(new Exception("Invalid Token"));
- }
- // 判断当前的请求方法是否是对应的合法 ip 地址
- String uri = (String) message.get(org.apache.cxf.message.Message.PATH_INFO);
- LOGGER.debug("SecurityInterceptor request =============URI:" + uri + "=============");
- String ip = request.getHeader("X-Forwarded-For");
- LOGGER.debug("SecurityInterceptor request =============X_FORWARDED_FOR IP:" + ip + "=============");
- }
- /**
- * 校验token的正确性
- *
- * @param token
- * @return
- */
- private boolean checkToken(String token) {
- if (StringUtils.isEmpty(serverAuthHeader.getToken()) && StringUtils.isEmpty(token)) {
- return true;
- }
- if (!StringUtils.isEmpty(token) && token.equals(serverAuthHeader.getToken())) {
- return true;
- }
- return false;
- }
- public ServerAuthHeader getServerAuthHeader() {
- return serverAuthHeader;
- }
- public void setServerAuthHeader(ServerAuthHeader serverAuthHeader) {
- this.serverAuthHeader = serverAuthHeader;
- }
- }
- package com.jd.help.center.service.restful;
- import org.apache.commons.codec.binary.Base64;
- import org.apache.commons.lang.StringUtils;
- import java.security.MessageDigest;
- import java.security.NoSuchAlgorithmException;
- /**
- * jd token 验证提取
- * <br>author: zhaotianming@360buy.com
- * <br>DateTime: 2012-12-5 下午03:39:21
- * <br>Version 1.0
- */
- public class AuthHeader {
- private String seed;
- private String content;
- private String token = "";
- public void setToken(String token) {
- this.token = token;
- }
- public String getSeed() {
- return seed;
- }
- public void setSeed(String seed) {
- this.seed = seed;
- }
- public String getContent() {
- return content;
- }
- public void setContent(String content) {
- this.content = content;
- }
- public String getToken() {
- if (StringUtils.isNotEmpty(content)) {
- if (StringUtils.isNotEmpty(seed)) {
- byte[] bb = md5(content + "-" + seed);
- System.out.print(new String(Base64.encodeBase64(bb))); //todo 用完修改
- return new String(Base64.encodeBase64(bb));
- } else {
- return content;
- }
- }
- return token;
- }
- //测试使用 上线 废除
- /* public String getToken(){
- return seed+content;
- }*/
- private byte[] md5(String data) {
- try {
- return MessageDigest.getInstance("MD5").digest(data.getBytes());
- } catch (NoSuchAlgorithmException e) {
- throw new RuntimeException(e);
- }
- }
- }
- <?xml version="1.0" encoding="gb2312"?>
- <web-app version="2.4" 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">
- <display-name>help-center</display-name>
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath:spring-config.xml;classpath:spring-config-struts.xml</param-value>
- </context-param>
- <listener>
- <listener-class>com.jd.help.center.listener.CategoryListener</listener-class>
- </listener>
- <filter>
- <filter-name>UrlRewriteFilter</filter-name>
- <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
- <init-param>
- <param-name>logLevel</param-name>
- <param-value>DEBUG</param-value>
- </init-param>
- </filter>
- <filter>
- <filter-name>charsetFilter</filter-name>
- <filter-class>com.jd.common.web.filter.CharsetFilter</filter-class>
- </filter>
- <filter>
- <filter-name>struts2</filter-name>
- <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
- </filter>
- <filter-mapping>
- <filter-name>charsetFilter</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- <filter-mapping>
- <filter-name>struts2</filter-name>
- <url-pattern>*.action</url-pattern>
- <dispatcher>REQUEST</dispatcher>
- <dispatcher>FORWARD</dispatcher>
- </filter-mapping>
- <filter-mapping>
- <filter-name>UrlRewriteFilter</filter-name>
- <url-pattern>/*</url-pattern>
- <dispatcher>REQUEST</dispatcher>
- <dispatcher>FORWARD</dispatcher>
- </filter-mapping>
- <servlet>
- <servlet-name>velocitylayout</servlet-name>
- <servlet-class>org.apache.velocity.tools.view.servlet.VelocityViewServlet</servlet-class>
- </servlet>
- <resource-ref>
- <description>helpCenterDataSource</description>
- <res-ref-name>jdbc/helpCenterDataSource</res-ref-name>
- <res-type>javax.sql.DataSource</res-type>
- <res-auth>Container</res-auth>
- </resource-ref>
- <welcome-file-list>
- <welcome-file>home.html</welcome-file>
- </welcome-file-list>
- <servlet>
- <servlet-name>CXFServlet</servlet-name>
- <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>CXFServlet</servlet-name>
- <url-pattern>/services/*</url-pattern>
- </servlet-mapping>
- </web-app>