Commit 9ede9b41 authored by 李洪明's avatar 李洪明

添加登录

parent 30a927e6
Pipeline #1272 canceled with stages
package com.bme.access.upload.common;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
@Component
public class ApplicationContextHelper implements ApplicationContextAware {
private static ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
ApplicationContextHelper.applicationContext = applicationContext;
if(ApplicationContextHelper.applicationContext == null) {
ApplicationContextHelper.applicationContext = applicationContext;
}
}
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
public static <T> T getBean(Class<T> clasz) {
return applicationContext != null ? applicationContext.getBean(clasz) : null;
}
public static Object getBean(String name){
return getApplicationContext().getBean(name);
}
}
\ No newline at end of file
package com.bme.access.upload.common;
import java.util.HashMap;
import java.util.Map;
public class CommonResult {
public CommonResult() {
}
public CommonResult(boolean success, String msg, Object data) {
super();
this.success = success;
this.msg = msg;
this.data = data;
}
public static CommonResult success() {
return CommonResult.success(null, null);
}
public static CommonResult success(String msg) {
return CommonResult.success(msg, null);
}
public static CommonResult success(String msg, Object data) {
CommonResult result = new CommonResult(true, msg, data);
result.setType(0);
return result;
}
public static CommonResult fail() {
return CommonResult.fail(null, null);
}
public static CommonResult fail(String msg) {
return CommonResult.fail(msg, null);
}
public static CommonResult fail(String msg, Object data) {
CommonResult result = new CommonResult(false, msg, data);
result.setType(0);
return result;
}
@SuppressWarnings("unchecked")
public CommonResult data(Object... objects) {
if (objects != null) {
if (objects.length == 1) {
this.data = objects[0];
} else {
Map<String, Object> dataMap = new HashMap<String, Object>();
if (data instanceof Map) {
dataMap = (Map<String, Object>) this.data;
}
for (int i = 0; i < objects.length / 2; i++) {
int index = i * 2;
dataMap.put(String.valueOf(objects[index]), objects[index + 1]);
}
this.data = dataMap;
}
}
return this;
}
private boolean success;
private Integer type;
private String msg;
private Object data;
public boolean isSuccess() {
return success;
}
public void setSuccess(boolean success) {
this.success = success;
}
public Integer getType() {
return type;
}
public void setType(Integer type) {
this.type = type;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
public static CommonResult result(boolean checkResult) {
CommonResult result = new CommonResult();
result.setSuccess(checkResult);
return result;
}
}
\ No newline at end of file
This diff is collapsed.
package com.bme.access.upload.config;
import com.alibaba.fastjson.JSONObject;
import com.bme.access.upload.common.*;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.context.annotation.Configuration;
import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;
@Configuration
public class LoginConfig {
//账号
public final static String USERNAME = "13905902008";
//密码
public final static String PASSWORD = "uR4}eP4!";
public final static String AUTH = "111";
public final static String LOGIN_URL = "https://dctapi.soszyzg.com/dct/login";
public static String token = "";
@PostConstruct
public void getLogin() throws Exception {
Map<String, Object> body = new HashMap<>();
body.put("username", USERNAME);
body.put("password", PASSWORD);
Map<String, String> headers = new HashMap<>();
headers.put("Content-Type", "application/json");
headers.put("X-Access-Token", AUTH);
HttpResponse response = HttpUtils.sendRequest(LOGIN_URL, null, headers, null, body);
String str = EntityUtils.toString(response.getEntity());
JSONObject jsonObject = JSONObject.parseObject(str);
Integer code = jsonObject.getInteger("code");
if (code.equals(200)){
String result = jsonObject.getString("result");
JSONObject resultObject = JSONObject.parseObject(result);
token = resultObject.getString("token");
}
}
}
package com.bme.access.upload.module.service;
import org.springframework.stereotype.Service;
@Service
public class LoginService {
}
package com.bme.access.upload.module.web;
import com.bme.access.upload.common.CommonResult;
import com.bme.access.upload.common.HttpUtils;
import com.bme.access.upload.config.LoginConfig;
import com.bme.access.upload.module.service.LoginService;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
@RestController
@Slf4j
@RequestMapping("/auth")
public class AuthController {
@Autowired
private LoginService loginService;
@Autowired
private LoginConfig loginConfig;
@GetMapping("")
public CommonResult getAuthToken() throws Exception {
loginConfig.getLogin();
return CommonResult.success("成功");
}
}
package com.bme.access.upload.module.web;
public class LoginController {
}
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