星空对接接口开发
This commit is contained in:
parent
14dae22176
commit
004bea1a5e
|
@ -0,0 +1,93 @@
|
|||
package com.ruoyi.webApi;
|
||||
|
||||
import com.ruoyi.common.constant.Constants;
|
||||
import com.ruoyi.common.core.domain.AjaxResult;
|
||||
import com.ruoyi.common.core.domain.model.LoginBody;
|
||||
import com.ruoyi.common.core.domain.model.LoginUser;
|
||||
import com.ruoyi.common.exception.ServiceException;
|
||||
import com.ruoyi.common.exception.user.UserPasswordNotMatchException;
|
||||
import com.ruoyi.common.utils.MessageUtils;
|
||||
import com.ruoyi.framework.manager.AsyncManager;
|
||||
import com.ruoyi.framework.manager.factory.AsyncFactory;
|
||||
import com.ruoyi.framework.security.context.AuthenticationContextHolder;
|
||||
import com.ruoyi.framework.web.service.SysLoginService;
|
||||
import com.ruoyi.framework.web.service.TokenService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.security.authentication.AuthenticationManager;
|
||||
import org.springframework.security.authentication.BadCredentialsException;
|
||||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* @author 16358
|
||||
* @date 2025/6/3
|
||||
*/
|
||||
@RequestMapping("/openApi")
|
||||
@RestController
|
||||
public class ApiController {
|
||||
|
||||
@Autowired
|
||||
private SysLoginService sysLoginService;
|
||||
|
||||
@Autowired
|
||||
private TokenService tokenService;
|
||||
|
||||
@Resource
|
||||
private AuthenticationManager authenticationManager;
|
||||
|
||||
//星空 -> 计量平台 : 过磅申请(接收星空 发货通知单,收料通知单)
|
||||
@PostMapping("/poundapplication")
|
||||
public AjaxResult poundapplication(@RequestBody ApiRequestBody data) throws IOException {
|
||||
Object json = data.getData();
|
||||
|
||||
AjaxResult success = AjaxResult.success();
|
||||
return success;
|
||||
}
|
||||
|
||||
//对外提供登录接口
|
||||
@PostMapping("/getToken")
|
||||
public AjaxResult getToken(@RequestBody LoginBody loginBody) throws IOException {
|
||||
String username = loginBody.getUsername();
|
||||
String password = loginBody.getPassword();
|
||||
AjaxResult ajax = AjaxResult.success();
|
||||
//登录前校验
|
||||
sysLoginService.loginPreCheck(username, password);
|
||||
// 用户验证
|
||||
Authentication authentication = null;
|
||||
try
|
||||
{
|
||||
UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(username, password);
|
||||
AuthenticationContextHolder.setContext(authenticationToken);
|
||||
// 该方法会去调用UserDetailsServiceImpl.loadUserByUsername
|
||||
authentication = authenticationManager.authenticate(authenticationToken);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
if (e instanceof BadCredentialsException)
|
||||
{
|
||||
AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.password.not.match")));
|
||||
throw new UserPasswordNotMatchException();
|
||||
}
|
||||
else
|
||||
{
|
||||
AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, e.getMessage()));
|
||||
throw new ServiceException(e.getMessage());
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
AuthenticationContextHolder.clearContext();
|
||||
}
|
||||
AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success")));
|
||||
LoginUser loginUser = (LoginUser) authentication.getPrincipal();
|
||||
sysLoginService.recordLoginInfo(loginUser.getUserId());
|
||||
// 生成token
|
||||
String token = tokenService.createToken(loginUser);
|
||||
ajax.put(Constants.TOKEN, token);
|
||||
return ajax;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
package com.ruoyi.webApi;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author 16358
|
||||
* @date 2025/6/5
|
||||
* 接口接取参数body
|
||||
*/
|
||||
public class ApiRequestBody {
|
||||
|
||||
@JsonProperty("data")
|
||||
private Map<String, Object> data;
|
||||
|
||||
public Map<String, Object> getData() {
|
||||
return data;
|
||||
}
|
||||
|
||||
public void setData(Map<String, Object> data) {
|
||||
this.data = data;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,104 @@
|
|||
package com.ruoyi.webApi;
|
||||
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.ruoyi.system.service.ISysConfigService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStream;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
|
||||
/**
|
||||
* @author 16358
|
||||
* @date 2025/6/5
|
||||
*/
|
||||
@Component("apiTask")
|
||||
public class ApiTask {
|
||||
@Autowired
|
||||
private ISysConfigService sysConfigService;
|
||||
|
||||
//登录星空接口
|
||||
public String OAlogin(){
|
||||
// 目标URL
|
||||
String url = sysConfigService.selectConfigByKey("OA_Url")+"/k3cloud/Kingdee.BOS.WebApi.ServicesStub.AuthService.ValidateUser.common.kdsvc";
|
||||
// 请求体,JSON格式
|
||||
String jsonInputString = sysConfigService.selectConfigByKey("login_JSON");
|
||||
|
||||
try {
|
||||
// 创建URL对象
|
||||
URL apiUrl = new URL(url);
|
||||
// 打开连接
|
||||
HttpURLConnection connection = (HttpURLConnection) apiUrl.openConnection();
|
||||
|
||||
// 设置请求方法为POST
|
||||
connection.setRequestMethod("POST");
|
||||
|
||||
// 设置请求头
|
||||
// Content-Type 指定发送的数据格式是JSON,并且字符集为UTF-8
|
||||
connection.setRequestProperty("Content-Type", "application/json; utf-8");
|
||||
// Accept 指定期望接收的数据格式是JSON
|
||||
connection.setRequestProperty("Accept", "application/json");
|
||||
|
||||
// 允许写入请求体
|
||||
connection.setDoOutput(true);
|
||||
|
||||
// 获取输出流,发送请求体数据
|
||||
try (OutputStream os = connection.getOutputStream()) {
|
||||
byte[] input = jsonInputString.getBytes("utf-8");
|
||||
os.write(input, 0, input.length);
|
||||
}
|
||||
|
||||
// 获取响应码
|
||||
int responseCode = connection.getResponseCode();
|
||||
|
||||
// 读取响应内容
|
||||
StringBuilder response = new StringBuilder();
|
||||
try (BufferedReader br = new BufferedReader(
|
||||
new InputStreamReader(connection.getInputStream(), "utf-8"))) {
|
||||
String responseLine = null;
|
||||
while ((responseLine = br.readLine()) != null) {
|
||||
response.append(responseLine.trim());
|
||||
}
|
||||
return response.toString();
|
||||
}catch (Exception e) {
|
||||
throw new RuntimeException("因未知原因导致登录OA系统失败!");
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
//判断是否登录成功并返回token
|
||||
public String getToken(){
|
||||
//触发登录接口
|
||||
String response = OAlogin();
|
||||
|
||||
// 解析响应体为 JSONObject
|
||||
JSONObject jsonResponse = JSONObject.parseObject(response.toString());
|
||||
// 提取 "Message" 字段
|
||||
if (jsonResponse != null && !jsonResponse.containsKey("")) {
|
||||
String loginResultType = jsonResponse.getString("LoginResultType");
|
||||
if("1".equals(loginResultType)){
|
||||
return jsonResponse.getString("KDSVCSessionId");
|
||||
}else {
|
||||
throw new RuntimeException(jsonResponse.getString("Message"));
|
||||
}
|
||||
} else {
|
||||
throw new RuntimeException("返回参数解析错误!");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
//测试任务
|
||||
public void testTAS(){
|
||||
String token = getToken();
|
||||
System.out.println(token);
|
||||
}
|
||||
}
|
|
@ -163,7 +163,7 @@ public class Constants
|
|||
/**
|
||||
* 定时任务白名单配置(仅允许访问的包名,如其他需要可以自行添加)
|
||||
*/
|
||||
public static final String[] JOB_WHITELIST_STR = { "com.ruoyi.quartz.task" };
|
||||
public static final String[] JOB_WHITELIST_STR = { "com.ruoyi.quartz.task","com.ruoyi.webApi" };
|
||||
|
||||
/**
|
||||
* 定时任务违规的字符
|
||||
|
|
|
@ -111,7 +111,7 @@ public class SecurityConfig
|
|||
.authorizeHttpRequests((requests) -> {
|
||||
permitAllUrl.getUrls().forEach(url -> requests.antMatchers(url).permitAll());
|
||||
// 对于登录login 注册register 验证码captchaImage 允许匿名访问
|
||||
requests.antMatchers("/login", "/register", "/captchaImage").permitAll()
|
||||
requests.antMatchers("/login", "/register", "/captchaImage", "/openApi/getToken").permitAll()
|
||||
// 静态资源,可匿名访问
|
||||
.antMatchers(HttpMethod.GET, "/", "/*.html", "/**/*.html", "/**/*.css", "/**/*.js", "/profile/**").permitAll()
|
||||
.antMatchers("/swagger-ui.html", "/swagger-resources/**", "/webjars/**", "/*/api-docs", "/druid/**").permitAll()
|
||||
|
|
Loading…
Reference in New Issue