Commit 285d51e88780b712a574ef60cf70286df672f5f0

Authored by 王通
1 parent 61b9d8ce

1.提供给博协响应式公交数据接口

src/main/java/com/bsth/CXFConfig.java
@@ -14,6 +14,7 @@ import com.bsth.server_rs.bigdata.BigscreenService; @@ -14,6 +14,7 @@ import com.bsth.server_rs.bigdata.BigscreenService;
14 import com.bsth.server_rs.departure.DepartureRestService; 14 import com.bsth.server_rs.departure.DepartureRestService;
15 import com.bsth.server_rs.destroy.DestroyDetailRestService; 15 import com.bsth.server_rs.destroy.DestroyDetailRestService;
16 import com.bsth.server_rs.directive.DirectiveRestService; 16 import com.bsth.server_rs.directive.DirectiveRestService;
  17 +import com.bsth.server_rs.dks.BxRestService;
17 import com.bsth.server_rs.dks.DksRestService; 18 import com.bsth.server_rs.dks.DksRestService;
18 import com.bsth.server_rs.electric.ElectricService; 19 import com.bsth.server_rs.electric.ElectricService;
19 import com.bsth.server_rs.exception.AesExceptionMapper; 20 import com.bsth.server_rs.exception.AesExceptionMapper;
@@ -139,6 +140,9 @@ public class CXFConfig { @@ -139,6 +140,9 @@ public class CXFConfig {
139 @Autowired 140 @Autowired
140 private ManHoursRestService manHoursRestService; 141 private ManHoursRestService manHoursRestService;
141 142
  143 + @Autowired
  144 + private BxRestService bxRestService;
  145 +
142 @Bean 146 @Bean
143 public Server rsServer() { 147 public Server rsServer() {
144 JAXRSServerFactoryBean endpoint = new JAXRSServerFactoryBean(); 148 JAXRSServerFactoryBean endpoint = new JAXRSServerFactoryBean();
@@ -167,7 +171,8 @@ public class CXFConfig { @@ -167,7 +171,8 @@ public class CXFConfig {
167 departureRestService, 171 departureRestService,
168 dksRestService, 172 dksRestService,
169 xxfbRestService, 173 xxfbRestService,
170 - manHoursRestService)); 174 + manHoursRestService,
  175 + bxRestService));
171 endpoint.setProviders(Arrays.asList(new JacksonJsonProvider(), new AesExceptionMapper())); 176 endpoint.setProviders(Arrays.asList(new JacksonJsonProvider(), new AesExceptionMapper()));
172 //endpoint.setFeatures(Arrays.asList(new Swagger2Feature())); 177 //endpoint.setFeatures(Arrays.asList(new Swagger2Feature()));
173 endpoint.getInInterceptors().add(new AuthorizeInterceptor_IN()); 178 endpoint.getInInterceptors().add(new AuthorizeInterceptor_IN());
src/main/java/com/bsth/StartCommand.java
@@ -7,6 +7,7 @@ import com.bsth.server_rs.man_hours.ManHoursRefreshScheduler; @@ -7,6 +7,7 @@ import com.bsth.server_rs.man_hours.ManHoursRefreshScheduler;
7 import com.bsth.server_rs.schedule.real.thread.ExecSchDataRefreshThread; 7 import com.bsth.server_rs.schedule.real.thread.ExecSchDataRefreshThread;
8 import com.bsth.server_rs.schedule.real.thread.SchInOutDataRefreshThread; 8 import com.bsth.server_rs.schedule.real.thread.SchInOutDataRefreshThread;
9 import com.bsth.server_rs.thread.RfidCardInfoPersistenceThread; 9 import com.bsth.server_rs.thread.RfidCardInfoPersistenceThread;
  10 +import com.bsth.service.SystemParamService;
10 import org.springframework.beans.factory.annotation.Autowired; 11 import org.springframework.beans.factory.annotation.Autowired;
11 import org.springframework.boot.CommandLineRunner; 12 import org.springframework.boot.CommandLineRunner;
12 import org.springframework.stereotype.Component; 13 import org.springframework.stereotype.Component;
@@ -37,6 +38,9 @@ public class StartCommand implements CommandLineRunner{ @@ -37,6 +38,9 @@ public class StartCommand implements CommandLineRunner{
37 @Autowired 38 @Autowired
38 ManHoursRefreshScheduler manHoursRefreshScheduler; 39 ManHoursRefreshScheduler manHoursRefreshScheduler;
39 40
  41 + @Autowired
  42 + SystemParamService systemParamService;
  43 +
40 @Override 44 @Override
41 public void run(String... arg0){ 45 public void run(String... arg0){
42 46
@@ -53,6 +57,7 @@ public class StartCommand implements CommandLineRunner{ @@ -53,6 +57,7 @@ public class StartCommand implements CommandLineRunner{
53 //定时刷新基础信息 57 //定时刷新基础信息
54 Application.mainServices.scheduleWithFixedDelay(basicDataRefreshThread, 30, 30, TimeUnit.MINUTES); 58 Application.mainServices.scheduleWithFixedDelay(basicDataRefreshThread, 30, 30, TimeUnit.MINUTES);
55 manHoursRefreshScheduler.refresh(); 59 manHoursRefreshScheduler.refresh();
  60 + systemParamService.refresh();
56 } catch (Exception e) { 61 } catch (Exception e) {
57 e.printStackTrace(); 62 e.printStackTrace();
58 } 63 }
src/main/java/com/bsth/server_rs/AuthorizeInterceptor_IN.java
1 -package com.bsth.server_rs;  
2 -  
3 -import com.bsth.server_rs.exception.AesException;  
4 -import com.bsth.service.UserService;  
5 -import org.apache.commons.lang3.StringEscapeUtils;  
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.eclipse.jetty.util.MultiMap;  
11 -import org.eclipse.jetty.util.UrlEncoded;  
12 -import org.slf4j.Logger;  
13 -import org.slf4j.LoggerFactory;  
14 -import org.springframework.beans.BeansException;  
15 -import org.springframework.context.ApplicationContext;  
16 -import org.springframework.context.ApplicationContextAware;  
17 -import org.springframework.stereotype.Component;  
18 -  
19 -import java.security.MessageDigest;  
20 -import java.util.Arrays;  
21 -import java.util.HashMap;  
22 -import java.util.Map;  
23 -import java.util.Set;  
24 -  
25 -/**  
26 - * rest 接口授权校验(IN 输入拦截)  
27 - * Created by panzhao on 2017/3/28.  
28 - */  
29 -@Component  
30 -public class AuthorizeInterceptor_IN extends AbstractPhaseInterceptor<Message> implements ApplicationContextAware {  
31 -  
32 - private static final String SIGN = "sign";  
33 - private static final String TIMESTAMP = "timestamp";  
34 - private static final String NONCE = "nonce";  
35 - private static final String PASSWORD = "password";  
36 - private static final int MAX_TIME_DIFF = 1000 * 60 * 10;  
37 - private static Map<String, String> pwd2nonceMap;  
38 -  
39 - static UserService userService;  
40 -  
41 - static Logger logger = LoggerFactory.getLogger(AuthorizeInterceptor_IN.class);  
42 -  
43 - public AuthorizeInterceptor_IN() {  
44 - super(Phase.RECEIVE);  
45 - }  
46 -  
47 - static {  
48 - pwd2nonceMap = new HashMap<>();  
49 - }  
50 -  
51 - @Override  
52 - public void handleMessage(Message message) throws Fault {  
53 -  
54 - long t = System.currentTimeMillis();  
55 - if (message.get(Message.QUERY_STRING) == null) {  
56 - throw new AesException(AesException.MISS_SIGN);  
57 - }  
58 -  
59 - //放行wadl  
60 - if(message.get(Message.QUERY_STRING).equals("_wadl")  
61 - && message.get(Message.PATH_INFO).equals("/webservice/rest")){  
62 - return ;  
63 - }  
64 -  
65 - //获取参数,不包括 url 路径参数 只包括?号之后的  
66 - String queryString = StringEscapeUtils.unescapeHtml4(message.get(Message.QUERY_STRING).toString());  
67 - MultiMap<String> params = new MultiMap<>();  
68 - UrlEncoded.decodeTo(queryString, params, "utf-8");  
69 - Map<String, String> map = multi2One(params);  
70 -  
71 - if (!map.containsKey(SIGN)) {  
72 - throw new AesException(AesException.MISS_SIGN);  
73 - }  
74 - if (!map.containsKey(TIMESTAMP)) {  
75 - throw new AesException(AesException.MISS_TIMESTAMP);  
76 - }  
77 -  
78 - try{  
79 - long timestamp = Long.parseLong(map.get(TIMESTAMP));  
80 - if(Math.abs(t - timestamp) > MAX_TIME_DIFF){  
81 - throw new AesException(AesException.INVALID_TIMESTAMP);  
82 - }  
83 - }catch(Exception e){  
84 - throw new AesException(AesException.INVALID_TIMESTAMP);  
85 - }  
86 -  
87 - if (!map.containsKey(NONCE)) {  
88 - throw new AesException(AesException.MISS_NONCE);  
89 - }  
90 - if (!map.containsKey(PASSWORD)) {  
91 - throw new AesException(AesException.MISS_PWD);  
92 - }  
93 -  
94 - String prevNonce = pwd2nonceMap.get(map.get(PASSWORD));  
95 - if(prevNonce != null && prevNonce.equals(map.get(NONCE)))  
96 - throw new AesException(AesException.NO_RANDOM_NONCE);  
97 -  
98 - if (userService.get(map.get(PASSWORD)) == null) {  
99 - throw new AesException(AesException.INVALID_PWD);  
100 - }  
101 -  
102 - String sign = map.get(SIGN);  
103 - map.remove(SIGN);  
104 - String sh1 = "";  
105 - try {  
106 - sh1 = getSHA1(map);  
107 - } catch (Exception e) {  
108 - throw new AesException(AesException.SIGN_CHECK_ERROR);  
109 - }  
110 -  
111 - if (!sign.equals(sh1)) {  
112 - throw new AesException(AesException.SIGN_CHECK_FAIL);  
113 - }  
114 - }  
115 -  
116 - public static Map<String, String> multi2One(MultiMap<String> params) {  
117 - Map<String, String> map = new HashMap<>();  
118 - Set<String> ks = params.keySet();  
119 - for (String k : ks) {  
120 - map.put(k, params.getString(k));  
121 - }  
122 - return map;  
123 - }  
124 -  
125 -  
126 - public static String getSHA1(Map<String, String> map) throws Exception {  
127 -  
128 - try {  
129 - String[] array = new String[map.size()];  
130 - map.values().toArray(array);  
131 - StringBuffer sb = new StringBuffer();  
132 -  
133 - // 字符串排序  
134 - Arrays.sort(array);  
135 - for (int i = 0; i < array.length; i++) {  
136 - sb.append(array[i]);  
137 - }  
138 - String str = sb.toString();  
139 - // SHA1签名生成  
140 - MessageDigest md = MessageDigest.getInstance("SHA-1");  
141 - md.update(str.getBytes());  
142 - byte[] digest = md.digest();  
143 -  
144 - StringBuffer hexstr = new StringBuffer();  
145 - String shaHex = "";  
146 - for (int i = 0; i < digest.length; i++) {  
147 - shaHex = Integer.toHexString(digest[i] & 0xFF);  
148 - if (shaHex.length() < 2) {  
149 - hexstr.append(0);  
150 - }  
151 - hexstr.append(shaHex);  
152 - }  
153 - return hexstr.toString();  
154 - } catch (Exception e) {  
155 - logger.error("", e);  
156 - throw e;  
157 - }  
158 - }  
159 -  
160 - @Override  
161 - public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {  
162 - userService = applicationContext.getBean(UserService.class);  
163 - }  
164 -} 1 +package com.bsth.server_rs;
  2 +
  3 +import com.bsth.common.SystemParamKeys;
  4 +import com.bsth.server_rs.exception.AesException;
  5 +import com.bsth.service.SystemParamService;
  6 +import com.bsth.service.UserService;
  7 +import org.apache.commons.lang3.StringEscapeUtils;
  8 +import org.apache.cxf.interceptor.Fault;
  9 +import org.apache.cxf.message.Message;
  10 +import org.apache.cxf.phase.AbstractPhaseInterceptor;
  11 +import org.apache.cxf.phase.Phase;
  12 +import org.eclipse.jetty.util.MultiMap;
  13 +import org.eclipse.jetty.util.UrlEncoded;
  14 +import org.slf4j.Logger;
  15 +import org.slf4j.LoggerFactory;
  16 +import org.springframework.beans.BeansException;
  17 +import org.springframework.beans.factory.InitializingBean;
  18 +import org.springframework.context.ApplicationContext;
  19 +import org.springframework.context.ApplicationContextAware;
  20 +import org.springframework.stereotype.Component;
  21 +
  22 +import java.security.MessageDigest;
  23 +import java.util.Arrays;
  24 +import java.util.HashMap;
  25 +import java.util.Map;
  26 +import java.util.Set;
  27 +
  28 +/**
  29 + * rest 接口授权校验(IN 输入拦截)
  30 + * Created by panzhao on 2017/3/28.
  31 + */
  32 +@Component
  33 +public class AuthorizeInterceptor_IN extends AbstractPhaseInterceptor<Message> implements ApplicationContextAware {
  34 +
  35 + private static final String SIGN = "sign";
  36 + private static final String TIMESTAMP = "timestamp";
  37 + private static final String NONCE = "nonce";
  38 + private static final String PASSWORD = "password";
  39 + private static final int MAX_TIME_DIFF = 1000 * 60 * 10;
  40 + private static Map<String, String> pwd2nonceMap;
  41 +
  42 + static UserService userService;
  43 +
  44 + static SystemParamService systemParamService;
  45 +
  46 + static Logger logger = LoggerFactory.getLogger(AuthorizeInterceptor_IN.class);
  47 +
  48 + public AuthorizeInterceptor_IN() {
  49 + super(Phase.RECEIVE);
  50 + }
  51 +
  52 + static {
  53 + pwd2nonceMap = new HashMap<>();
  54 + }
  55 +
  56 + @Override
  57 + public void handleMessage(Message message) throws Fault {
  58 +
  59 + long t = System.currentTimeMillis();
  60 + if (message.get(Message.QUERY_STRING) == null) {
  61 + throw new AesException(AesException.MISS_SIGN);
  62 + }
  63 +
  64 + //放行wadl
  65 + if(message.get(Message.QUERY_STRING).equals("_wadl")
  66 + && message.get(Message.PATH_INFO).equals("/webservice/rest")){
  67 + return ;
  68 + }
  69 +
  70 + //获取参数,不包括 url 路径参数 只包括?号之后的
  71 + String queryString = StringEscapeUtils.unescapeHtml4(message.get(Message.QUERY_STRING).toString());
  72 + MultiMap<String> params = new MultiMap<>();
  73 + UrlEncoded.decodeTo(queryString, params, "utf-8");
  74 + Map<String, String> map = multi2One(params);
  75 +
  76 + if (!map.containsKey(SIGN)) {
  77 + throw new AesException(AesException.MISS_SIGN);
  78 + }
  79 + if (!map.containsKey(TIMESTAMP)) {
  80 + throw new AesException(AesException.MISS_TIMESTAMP);
  81 + }
  82 +
  83 + try{
  84 + long timestamp = Long.parseLong(map.get(TIMESTAMP));
  85 + if(Math.abs(t - timestamp) > MAX_TIME_DIFF){
  86 + throw new AesException(AesException.INVALID_TIMESTAMP);
  87 + }
  88 + }catch(Exception e){
  89 + throw new AesException(AesException.INVALID_TIMESTAMP);
  90 + }
  91 +
  92 + if (!map.containsKey(NONCE)) {
  93 + throw new AesException(AesException.MISS_NONCE);
  94 + }
  95 + if (!map.containsKey(PASSWORD)) {
  96 + throw new AesException(AesException.MISS_PWD);
  97 + }
  98 +
  99 + String prevNonce = pwd2nonceMap.get(map.get(PASSWORD));
  100 + if(prevNonce != null && prevNonce.equals(map.get(NONCE)))
  101 + throw new AesException(AesException.NO_RANDOM_NONCE);
  102 +
  103 + if (userService.get(map.get(PASSWORD)) == null) {
  104 + throw new AesException(AesException.INVALID_PWD);
  105 + }
  106 +
  107 + String sign = map.get(SIGN);
  108 + map.remove(SIGN);
  109 + String sh1 = "";
  110 + try {
  111 + sh1 = getSHA1(map);
  112 + } catch (Exception e) {
  113 + throw new AesException(AesException.SIGN_CHECK_ERROR);
  114 + }
  115 +
  116 + if (!sign.equals(sh1)) {
  117 + throw new AesException(AesException.SIGN_CHECK_FAIL);
  118 + }
  119 +
  120 + validate(map, message);
  121 + }
  122 +
  123 + private static void validate(Map<String, String> map, Message message) {
  124 + String limitPasswords = systemParamService.getValue(SystemParamKeys.LIMIT_PASSWORDS);
  125 + String limitUris = systemParamService.getValue(SystemParamKeys.LIMIT_URIS);
  126 + if (limitPasswords != null && limitPasswords.indexOf(String.format("%s,", map.get(PASSWORD))) > -1) {
  127 + if (limitUris != null && limitUris.indexOf(String.format("%s,", message.get(Message.REQUEST_URI))) == -1) {
  128 + throw new AesException(AesException.INVALID_URI);
  129 + }
  130 + }
  131 + }
  132 +
  133 + public static Map<String, String> multi2One(MultiMap<String> params) {
  134 + Map<String, String> map = new HashMap<>();
  135 + Set<String> ks = params.keySet();
  136 + for (String k : ks) {
  137 + map.put(k, params.getString(k));
  138 + }
  139 + return map;
  140 + }
  141 +
  142 +
  143 + public static String getSHA1(Map<String, String> map) throws Exception {
  144 +
  145 + try {
  146 + String[] array = new String[map.size()];
  147 + map.values().toArray(array);
  148 + StringBuffer sb = new StringBuffer();
  149 +
  150 + // 字符串排序
  151 + Arrays.sort(array);
  152 + for (int i = 0; i < array.length; i++) {
  153 + sb.append(array[i]);
  154 + }
  155 + String str = sb.toString();
  156 + // SHA1签名生成
  157 + MessageDigest md = MessageDigest.getInstance("SHA-1");
  158 + md.update(str.getBytes());
  159 + byte[] digest = md.digest();
  160 +
  161 + StringBuffer hexstr = new StringBuffer();
  162 + String shaHex = "";
  163 + for (int i = 0; i < digest.length; i++) {
  164 + shaHex = Integer.toHexString(digest[i] & 0xFF);
  165 + if (shaHex.length() < 2) {
  166 + hexstr.append(0);
  167 + }
  168 + hexstr.append(shaHex);
  169 + }
  170 + return hexstr.toString();
  171 + } catch (Exception e) {
  172 + logger.error("", e);
  173 + throw e;
  174 + }
  175 + }
  176 +
  177 + @Override
  178 + public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
  179 + userService = applicationContext.getBean(UserService.class);
  180 + systemParamService = applicationContext.getBean(SystemParamService.class);
  181 + }
  182 +}
src/main/java/com/bsth/server_rs/exception/AesException.java
1 -package com.bsth.server_rs.exception;  
2 -  
3 -/**  
4 - * Created by panzhao on 2017/3/28.  
5 - */  
6 -public class AesException extends RuntimeException {  
7 -  
8 - public final static int OK = 0;  
9 - public final static int MISS_SIGN = -30001;  
10 - public final static int MISS_TIMESTAMP = -30002;  
11 - public final static int MISS_NONCE = -30003;  
12 - public final static int NO_RANDOM_NONCE = -30005;  
13 - public final static int MISS_PWD = -30004;  
14 - public final static int SIGN_CHECK_ERROR = -40001;  
15 - public final static int SIGN_CHECK_FAIL = -40002;  
16 - public final static int INVALID_PWD = -40003;  
17 - public final static int INVALID_TIMESTAMP = -40004;  
18 -  
19 - private int code;  
20 -  
21 - private static String getMessage(int code) {  
22 - switch (code) {  
23 - case MISS_SIGN:  
24 - return "sign参数丢失";  
25 - case MISS_TIMESTAMP:  
26 - return "timestamp参数丢失";  
27 - case MISS_NONCE:  
28 - return "nonce参数丢失";  
29 - case NO_RANDOM_NONCE:  
30 - return "nonce参数异常";  
31 - case MISS_PWD:  
32 - return "密码参数丢失";  
33 - case INVALID_PWD:  
34 - return "无效的密码";  
35 - case SIGN_CHECK_ERROR:  
36 - return "签名校验时出现异常";  
37 - case SIGN_CHECK_FAIL:  
38 - return "无效的签名";  
39 - case INVALID_TIMESTAMP:  
40 - return "无效的时间戳";  
41 - default:  
42 - return null;  
43 - }  
44 - }  
45 -  
46 - public int getCode() {  
47 - return code;  
48 - }  
49 -  
50 - public AesException(int code) {  
51 - super(getMessage(code));  
52 - this.code = code;  
53 - }  
54 -} 1 +package com.bsth.server_rs.exception;
  2 +
  3 +/**
  4 + * Created by panzhao on 2017/3/28.
  5 + */
  6 +public class AesException extends RuntimeException {
  7 +
  8 + public final static int OK = 0;
  9 + public final static int MISS_SIGN = -30001;
  10 + public final static int MISS_TIMESTAMP = -30002;
  11 + public final static int MISS_NONCE = -30003;
  12 + public final static int NO_RANDOM_NONCE = -30005;
  13 + public final static int MISS_PWD = -30004;
  14 + public final static int SIGN_CHECK_ERROR = -40001;
  15 + public final static int SIGN_CHECK_FAIL = -40002;
  16 + public final static int INVALID_PWD = -40003;
  17 + public final static int INVALID_TIMESTAMP = -40004;
  18 + public final static int INVALID_URI = -40005;
  19 +
  20 + private int code;
  21 +
  22 + private static String getMessage(int code) {
  23 + switch (code) {
  24 + case MISS_SIGN:
  25 + return "sign参数丢失";
  26 + case MISS_TIMESTAMP:
  27 + return "timestamp参数丢失";
  28 + case MISS_NONCE:
  29 + return "nonce参数丢失";
  30 + case NO_RANDOM_NONCE:
  31 + return "nonce参数异常";
  32 + case MISS_PWD:
  33 + return "密码参数丢失";
  34 + case INVALID_PWD:
  35 + return "无效的密码";
  36 + case SIGN_CHECK_ERROR:
  37 + return "签名校验时出现异常";
  38 + case SIGN_CHECK_FAIL:
  39 + return "无效的签名";
  40 + case INVALID_TIMESTAMP:
  41 + return "无效的时间戳";
  42 + case INVALID_URI:
  43 + return "无效的URI";
  44 + default:
  45 + return null;
  46 + }
  47 + }
  48 +
  49 + public int getCode() {
  50 + return code;
  51 + }
  52 +
  53 + public AesException(int code) {
  54 + super(getMessage(code));
  55 + this.code = code;
  56 + }
  57 +}