Commit 7faf9d3a by liyanlin

Oauth1

parent 342464d1
......@@ -255,6 +255,28 @@
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<!--oauth1.0-->
<dependency>
<groupId>oauth.signpost</groupId>
<artifactId>signpost-core</artifactId>
<version>1.2.1.1</version>
</dependency>
<dependency>
<groupId>oauth.signpost</groupId>
<artifactId>signpost-commonshttp4</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.3.1</version>
</dependency>
</dependencies>
......
package com.blt.other.common.job;
import com.blt.other.module.cost.service.UserCostFinansysService;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;
/**
* @Author: li.yanlin
* @Description:
* @Date: Created in 17:12 2021-04-24
* @Modified by:
*/
public class NetSuiteSyncJob extends QuartzJobBean {
private static Logger logger = LoggerFactory.getLogger(NetSuiteSyncJob.class);
@Autowired
private UserCostFinansysService userCostFinansysService;
@Override
protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
String result = userCostFinansysService.syncUserFinance();
if (!"新增 0 条记录,更新 0 条记录".equals(result)) {
logger.info("Job:从财务系统获取财务信息," + result);
}
}
}
package com.blt.other.common.job;
import org.quartz.*;
import org.springframework.context.annotation.Bean;
/**
* @Author: li.yanlin
* @Description:
* @Date: Created in 17:12 2021-04-24
* @Modified by:
*/
public class NetSuiteSyncJobConfiguration {
// 扫描主体列表时间间隔:(秒)
private static final int TIME = 86400;
// JobDetail 定义要执行的 job
@Bean
public JobDetail netSuiteSyncJobDetail(){
return JobBuilder.newJob(NetSuiteSyncJob.class)
.withIdentity("netSuiteSyncJob")
.storeDurably().build();
}
// 触发 Bean 的 Trigger
@Bean
public Trigger netSuiteSyncJobTrigger(){
SimpleScheduleBuilder simpleScheduleBuilder = SimpleScheduleBuilder.simpleSchedule()
.withIntervalInSeconds(TIME).repeatForever();
return TriggerBuilder.newTrigger().forJob(netSuiteSyncJobDetail())
.withIdentity("netSuiteSyncTrigger")
.withSchedule(simpleScheduleBuilder).build();
}
}
package com.blt.other.common.util;
import java.io.InputStream;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Map;
import oauth.signpost.OAuthConsumer;
import oauth.signpost.basic.HttpURLConnectionResponseAdapter;
import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer;
import oauth.signpost.commonshttp.CommonsHttpOAuthProvider;
import oauth.signpost.exception.OAuthCommunicationException;
import oauth.signpost.exception.OAuthExpectationFailedException;
import oauth.signpost.exception.OAuthMessageSignerException;
import oauth.signpost.http.HttpParameters;
import oauth.signpost.signature.AuthorizationHeaderSigningStrategy;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.springframework.util.ObjectUtils;
/**
* @Author: li.yanlin
* @Description:
* @Date: Created in 14:29 2021-04-24
* @Modified by:
*/
public class OAuth1Util {
private static OAuthConsumer oAuthConsumer;
/**
* 设置认证参数
* @param consumerKey
* @param consumerSecret
* @param accessToken
* @param accessTokenSecret
* @param realmID
*/
public static void setupContext(String consumerKey, String consumerSecret, String accessToken, String accessTokenSecret, String realmID) {
HttpParameters httpParameters = new HttpParameters();
httpParameters.put("realm", realmID);
oAuthConsumer = new CommonsHttpOAuthConsumer(consumerKey, consumerSecret);
oAuthConsumer.setTokenWithSecret(accessToken, accessTokenSecret);
oAuthConsumer.setAdditionalParameters(httpParameters);
oAuthConsumer.setSigningStrategy(new AuthorizationHeaderSigningStrategy());
}
/**
* 认证
* @param httpRequest
* @throws Exception
*/
private static void authorize(HttpRequestBase httpRequest) throws Exception {
try {
oAuthConsumer.sign(httpRequest);
} catch (OAuthMessageSignerException e) {
throw new Exception(e);
} catch (OAuthExpectationFailedException e) {
throw new Exception(e);
} catch (OAuthCommunicationException e) {
throw new Exception(e);
}
}
/**
* 请求,Json Post
* @param customURIString
* @param header 请求头,已经默认了application/json
* @param parameter Json数据
* @return
* @throws Exception
*/
public static HttpResponse executePost(String customURIString, Map<String, String> header, String parameter) throws Exception {
CloseableHttpClient client = HttpClients.createDefault();
URIBuilder builder = null;
builder = new URIBuilder(customURIString);
HttpPost request = new HttpPost(builder.build());
HttpEntity httpEntity = new StringEntity(parameter, "application/json", "utf-8");
request.setEntity(httpEntity);
if (!ObjectUtils.isEmpty(header)) {
for (Map.Entry<String, String> entry : header.entrySet()) {
request.addHeader(entry.getKey(), entry.getValue());
}
}
authorize(request);
HttpClient httpClient = new DefaultHttpClient();
return httpClient.execute(request);
}
}
package com.blt.other.module.cost.vo;
import lombok.Data;
import java.util.List;
/**
* @Author: li.yanlin
* @Description: 发送到NS的数据主体
* @Date: Created in 17:19 2021-04-24
* @Modified by:
*/
@Data
public class NetSuiteCostVo {
/**
* 费用单号
*/
private String tranid;
/**
* 币种
*/
private String currency;
/**
* 付款主体
*/
private String subsidiary;
/**
* //费用单关联的出纳单日期格式为YYYY/MM/DD
*/
private String trandate;
/**
* 费用类型
*/
private String custbody_iofeetype;
/**
* 付款单
*/
private List<NetSuitePaymentVo> payment;
/**
* 收款单
*/
private List<NetSuitePaymentVo> receipt;
}
package com.blt.other.module.cost.vo;
import lombok.Data;
/**
* @Author: li.yanlin
* @Description: 发送到NS的数据金额VO
* @Date: Created in 17:24 2021-04-24
* @Modified by:
*/
@Data
public class NetSuitePaymentVo {
/**
* 付款银行
*/
private String account;
/**
* 金额
*/
private String amount;
/**
* 付款理由
*/
private String memo;
}
......@@ -86,3 +86,12 @@ server:
port: 8082
servlet:
context-path: /purchase/other
# NetSuite Api Base
net-suite:
base-url: https://6916374-sb1.restlets.api.netsuite.com/app/site/hosting/restlet.nl?script={}&deploy=1
consumer-key: 4e1786849ffd6cb866f2f22b43cce56793e446d56f1cf60fe45674aa02424646
consumer-secret: d66b2d4c6d6a9bf59b7923e3d32535b1e3685babf4109581884230fdb00ec330
token: 042c17360b2f3c831c9531365c8b98ef6aeeaf40ce4e7c46144ee70aad2e7667
token-secret: d974ba497472b258a06175cc62a2edba6104f94c131afba38143e8c3a2f0e125
realm: 6916374_SB1
......@@ -53,7 +53,6 @@ pagehelper:
logging:
file:
name: other/log/other.log
#项目参数配置
cost:
url:
......@@ -84,3 +83,13 @@ server:
port: 8082
servlet:
context-path: /purchase/other
# NetSuite Api Base
net-suite:
base-url: https://6916374-sb1.restlets.api.netsuite.com/app/site/hosting/restlet.nl?script={}&deploy=1
consumer-key: 4e1786849ffd6cb866f2f22b43cce56793e446d56f1cf60fe45674aa02424646
consumer-secret: d66b2d4c6d6a9bf59b7923e3d32535b1e3685babf4109581884230fdb00ec330
token: 042c17360b2f3c831c9531365c8b98ef6aeeaf40ce4e7c46144ee70aad2e7667
token-secret: d974ba497472b258a06175cc62a2edba6104f94c131afba38143e8c3a2f0e125
realm: 6916374_SB1
......@@ -82,3 +82,12 @@ server:
port: 8082
servlet:
context-path: /purchase/other
# NetSuite Api Base
net-suite:
base-url: https://6916374-sb1.restlets.api.netsuite.com/app/site/hosting/restlet.nl?script={}&deploy=1
consumer-key: 4e1786849ffd6cb866f2f22b43cce56793e446d56f1cf60fe45674aa02424646
consumer-secret: d66b2d4c6d6a9bf59b7923e3d32535b1e3685babf4109581884230fdb00ec330
token: 042c17360b2f3c831c9531365c8b98ef6aeeaf40ce4e7c46144ee70aad2e7667
token-secret: d974ba497472b258a06175cc62a2edba6104f94c131afba38143e8c3a2f0e125
realm: 6916374_SB1
......@@ -12,6 +12,7 @@ import com.bailuntec.common.SpringContextUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.blt.other.common.interceptor.mail.MailService;
import com.blt.other.common.util.OAuth1Util;
import com.blt.other.database.model.CostCompanyDomain;
import com.blt.other.database.model.CostTypeDomain;
import com.blt.other.module.auth.dao.OaDepartmentMapper;
......@@ -38,12 +39,17 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
......@@ -429,9 +435,30 @@ public class OtherApplicationTests {
@Resource
CostTypeDao costTypeDao;
@Test
public void test(){
OAuth1Util.setupContext("4e1786849ffd6cb866f2f22b43cce56793e446d56f1cf60fe45674aa02424646",
"d66b2d4c6d6a9bf59b7923e3d32535b1e3685babf4109581884230fdb00ec330",
"042c17360b2f3c831c9531365c8b98ef6aeeaf40ce4e7c46144ee70aad2e7667",
"d974ba497472b258a06175cc62a2edba6104f94c131afba38143e8c3a2f0e125",
"6916374_SB1");
try {
OAuth1Util.executePost("https://6916374-sb1.restlets.api.netsuite.com/app/site/hosting/restlet.nl?script=78&deploy=1",null,"[{\"tranid\":\"test_F20210312\",\"currency\":\"CNY\",\"subsidiary\":\"gzbl\",\"trandate\":\"2021/03/12\",\"custbody_iofeetype\":\"1\",\"payment\":[{\"account\":\"220201\",\"amount\":\"500\",\"memo\":\"测试\"},{\"account\":\"630108\",\"amount\":\"500\",\"memo\":\"测试\"}],\"receipt\":[{\"account\":\"630108\",\"amount\":\"500\",\"memo\":\"测试\"},{\"account\":\"220201\",\"amount\":\"500\",\"memo\":\"测试\"}]}]");
} catch (URISyntaxException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
@Rollback(value = false)
@Test
public void testType() {
costTypeDao.selectTestType()
.forEach(costTypeDomain -> {
CostTypeDomain item = new CostTypeDomain();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment