|
@@ -1,23 +1,83 @@
|
|
|
package cn.org.bjca.trust.java.imserver.controller.user.v1;
|
|
|
|
|
|
import cn.org.bjca.trust.java.imserver.HttpResult;
|
|
|
+import cn.org.bjca.trust.java.imserver.entitys.UserInfo;
|
|
|
+import cn.org.bjca.trust.java.imserver.repository.UserRepository;
|
|
|
import jakarta.servlet.http.HttpServletRequest;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.http.HttpHeaders;
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
+import java.io.ByteArrayOutputStream;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.io.OutputStream;
|
|
|
+import java.net.HttpURLConnection;
|
|
|
+import java.net.URL;
|
|
|
+import java.util.UUID;
|
|
|
+
|
|
|
@RestController
|
|
|
@RequestMapping("user/v1")
|
|
|
public class UserV1Controller {
|
|
|
|
|
|
@Autowired
|
|
|
private HttpServletRequest request;
|
|
|
+ @Autowired
|
|
|
+ private UserRepository userRepository;
|
|
|
|
|
|
@PostMapping("/login")
|
|
|
public HttpResult<LoginData> getById(@RequestBody LoginBean user, @RequestHeader HttpHeaders headers) throws Exception {
|
|
|
- System.out.println(headers.toString());
|
|
|
- System.out.println(headers.getFirst("appid"));
|
|
|
+ String appid = headers.getFirst("appid");
|
|
|
+ String userid = headers.getFirst("userid");
|
|
|
+ String ostype = headers.getFirst("ostype");
|
|
|
+ requestByGetAndParams("http://114.115.203.60:15672/api/vhosts/"+appid, "");
|
|
|
+ if (null != appid && null != userid && null != ostype) {
|
|
|
+ UserInfo userInfo = userRepository.findUserInfoByUserIdAndOsTypeAndAppId(userid, ostype, appid);
|
|
|
+ if (null == userInfo) {
|
|
|
+ System.out.println("-----------------用户不存在------------------");
|
|
|
+ userInfo = new UserInfo();
|
|
|
+ userInfo.setUserId(userid);
|
|
|
+ userInfo.setUserName(UUID.randomUUID().toString());
|
|
|
+ userInfo.setAppId(appid);
|
|
|
+ userInfo.setOsType(ostype);
|
|
|
+ userInfo.setPassword(UUID.randomUUID().toString());
|
|
|
+ userRepository.save(userInfo);
|
|
|
+
|
|
|
+ requestByGetAndParams("http://114.115.203.60:15672/api/users/" + userInfo.getUserName(), "{\"username\":\"" + userInfo.getUserName() + "\",\"password\":\"" + userInfo.getPassword() + "\",\"tags\":\"\"}");
|
|
|
+ requestByGetAndParams("http://114.115.203.60:15672/api/permissions/%2F/" + userInfo.getUserName(), "{\"username\":\"" + userInfo.getUserName() + "\",\"vhost\":\"" + appid + "\",\"configure\":\".*\",\"write\":\".*\",\"read\":\".*\"}");
|
|
|
+ requestByGetAndParams("http://114.115.203.60:15672/api/permissions/" + appid + "/" + userInfo.getUserName(), "{\"username\":\"" + userInfo.getUserName() + "\",\"vhost\":\"" + appid + "\",\"configure\":\".*\",\"write\":\".*\",\"read\":\".*\"}");
|
|
|
+ requestByGetAndParams("http://114.115.203.60:15672/api/topic-permissions/" + appid + "/" + userInfo.getUserName(), "{\"username\":\"" + userInfo.getUserName() + "\",\"vhost\":\"" + appid + "\",\"exchange\":\"\",\"write\":\".*\",\"read\":\".*\"}");
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ return new HttpResult<>(200, "成功", new LoginData("114.115.203.60", "18883", userInfo.getUserName(), userInfo.getPassword()));
|
|
|
+ } else {
|
|
|
+ return new HttpResult<>(201, "参数错误", new LoginData());
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
|
|
|
- return new HttpResult<>(200, "成功", new LoginData("114.115.203.60", "18883", user.getUserId(), user.getUserSig()));
|
|
|
+ private void requestByGetAndParams(String requestUrl, String param) throws Exception {
|
|
|
+ System.out.println(requestUrl + ":\n" + param);
|
|
|
+ URL url = new URL(requestUrl);
|
|
|
+ HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
|
|
|
+ //设置请求方式,请求参数类型
|
|
|
+ httpURLConnection.setRequestMethod("PUT");
|
|
|
+ httpURLConnection.setRequestProperty("content-type", "application/json;charset=UTF-8");
|
|
|
+ httpURLConnection.setRequestProperty("Authorization", "Basic YWRtaW46YWRtaW4=");
|
|
|
+ httpURLConnection.setDoOutput(true);
|
|
|
+ OutputStream outputStream = httpURLConnection.getOutputStream();
|
|
|
+ //将参数写入输出流,param必须是JSON格式
|
|
|
+ outputStream.write(param.getBytes());
|
|
|
+ outputStream.flush();
|
|
|
+ InputStream inputStream = httpURLConnection.getInputStream();
|
|
|
+ ByteArrayOutputStream bout = new ByteArrayOutputStream();
|
|
|
+ byte[] bytes = new byte[1024];
|
|
|
+ int len = 0;
|
|
|
+ while ((len = inputStream.read(bytes)) >= 0) {
|
|
|
+ bout.write(bytes, 0, len);
|
|
|
+ }
|
|
|
+ inputStream.close();
|
|
|
+ bout.close();
|
|
|
+ bout.toByteArray();
|
|
|
}
|
|
|
}
|