Skip to content

Commit

Permalink
忽略ssl证书错误
Browse files Browse the repository at this point in the history
增加多应用选项
  • Loading branch information
luoye663 committed Dec 17, 2020
1 parent 97c2677 commit 62060f8
Show file tree
Hide file tree
Showing 14 changed files with 163 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void onAuthenticationSuccess(HttpServletRequest httpServletRequest, HttpS
public void onAuthenticationFailure(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException {
httpServletResponse.setContentType("application/json;charset=utf-8");
PrintWriter writer = httpServletResponse.getWriter();
writer.write(gson.toJson(ResultUtil.error(-1, "failed!")));
writer.write(gson.toJson(ResultUtil.error(-1, e.getMessage())));
writer.flush();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ public Authentication authenticate(Authentication authentication) throws Authent
// 根据用户Token中的用户名查找用户信息,如果有该用户信息,则验证用户密码是否正确
String code = authenticationToken.getCode();
String state = authenticationToken.getState();
logger.info("Github 认证: code:{} state:{} Token:", code, state);
logger.info("认证: code:{} state:{} Token:{}", code, state, authenticationToken.getToken());
Map<String, Object> userInfo_redis = new HashMap<>();

if (!redisUtil.hasKey(states + state)) {
throw new UsernameNotFoundException("status不存在");
throw new UsernameNotFoundException("STATUS不存在");
// return ResultUtil.error(ResultEnum.STATE_HAS_EXPIRED);
}
redisUtil.del(states + state);
Expand Down
15 changes: 13 additions & 2 deletions src/main/java/io/qyi/e5/controller/admin/AdminController.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
Expand Down Expand Up @@ -53,19 +54,28 @@ public class AdminController {
@Value("${user.token.expire}")
private int tokenExpire;



/**
* 测试队列
*
* @Author: 落叶随风
* @Date: 2020/9/7 14:44
* @Return: * @return: void
*/
@GetMapping("/send")
public void send() {
Task.sendTaskOutlookMQ(Integer.valueOf(adminGithubId) );
public void send(@RequestParam int githubId, @RequestParam int outlookId) {
Task.sendTaskOutlookMQ(githubId, outlookId);
}

@GetMapping("/execute")
public void execute(@RequestParam int githubId, @RequestParam int outlookId) {
Task.executeE5(githubId, outlookId);
}

/**
* 对所有队列重新添加
*
* @Author: 落叶随风
* @Date: 2020/9/7 14:43
* @Return: * @return: java.lang.String
Expand All @@ -78,6 +88,7 @@ public String sendAll() {

/**
* 清空redis
*
* @Author: 落叶随风
* @Date: 2020/9/7 14:41
* @Return: * @return: java.lang.String
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/io/qyi/e5/outlook/bean/OutlookMq.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package io.qyi.e5.outlook.bean;

import lombok.Data;

/**
* @program: e5
* @description:
* @author: 落叶随风
* @create: 2020-12-17 22:20
**/
@Data
public class OutlookMq {
private int githubId;
private int outlookId;

public OutlookMq(int githubId, int outlookId) {
this.githubId = githubId;
this.outlookId = outlookId;
}
}
37 changes: 30 additions & 7 deletions src/main/java/io/qyi/e5/outlook/controller/AuthController.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,37 +39,53 @@ public class AuthController {
@Autowired
IOutlookService outlookService;

@Value("${isdebug}")
boolean isDebug;

@Value("${redis.auth2.outlook}")
String states;

@Value("${outlook.replyUrl}")
String replyUrl;

@Value("${outlook.replyUrlDebug}")
String replyUrlDebug;

@Value("${outlook.authorize.url}")
String authorizeUrl;

@Autowired
ITask Task;


@RequestMapping("/receive")
public Result Receive(String code, String state, String session_state) throws Exception {
if (!redisUtil.hasKey(states + state)) {
throw new APIException("state已过期,请到用户中心重新授权!");
}
/*这里不应该查询,在进行授权时因该把基础数据丢到redis*/
QueryWrapper<Outlook> outlookQueryWrapper = new QueryWrapper<>();
outlookQueryWrapper.eq("github_id", redisUtil.get(states + state));
int outlookId = (int) redisUtil.get(states + state);
outlookQueryWrapper.eq("id", outlookId);
Outlook outlook = outlookService.getOne(outlookQueryWrapper);
/*删除redis中的此键*/
redisUtil.del(states + state);
if (outlook == null) {
throw new APIException("没有查询到此用户,请检查是否在系统中注册!");
throw new APIException("没有查询到此记录,请检查是否在系统中注册!");
}
System.out.println(outlook);
boolean authorization_code = outlookService.getTokenAndSave(code, outlook.getClientId(), outlook.getClientSecret(), "https://e5.qyi.io/outlook/auth2/receive"
String reUrl = "";
if (isDebug) {
reUrl = replyUrlDebug;
} else {
reUrl = replyUrl;
}
boolean authorization_code = outlookService.getTokenAndSave(code, outlook.getClientId(), outlook.getClientSecret(), reUrl
, "authorization_code");
if (!authorization_code) {
throw new APIException("clientId 或 clientSecret 填写错误!授权失败!");
}
/*添加此用户进消息队列*/
Task.sendTaskOutlookMQ(outlook.getGithubId());
Task.sendTaskOutlookMQ(outlook.getGithubId(),outlookId);
return ResultUtil.success();
}

Expand All @@ -88,8 +104,15 @@ public Result getAuthorizeUrl(int id) {
}
// 生成随机uuid标识用户
String state = EncryptUtil.getInstance().SHA1Hex(UUID.randomUUID().toString());
redisUtil.set(states + state, outlook.getGithubId(), 600);
String url = String.format(authorizeUrl, outlook.getClientId(), "https://e5.qyi.io/outlook/auth2/receive", state);
redisUtil.set(states + state, id, 600);
String reUrl = "";
if (isDebug) {
reUrl = replyUrlDebug;
} else {
reUrl = replyUrl;
}

String url = String.format(authorizeUrl, outlook.getClientId(), reUrl, state);
return ResultUtil.success(url);
} else {
throw new APIException("没有此记录");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ public Result getOutlookList() {
List<OutlookListVo> vo = new ArrayList<>();
outlooklist.forEach(outlook -> {
OutlookListVo v = new OutlookListVo();
log.info(outlook.toString());
BeanUtils.copyProperties(outlook, v);
vo.add(v);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public boolean getTokenAndSave(String code, String client_id, String client_secr
logger.info("请求access_token返回数据:" + s);
if (jsonObject.get("error") != null) {
logger.error("错授权误!");
return false;
throw new APIException(jsonObject.get("error_description").toString());
} else {
int expires_in = jsonObject.getIntValue("expires_in");
String access_token = jsonObject.getString("access_token");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
* @since 2020-03-03
*/
public interface IOutlookLogService extends IService<OutlookLog> {
void addLog(int githubId, String msg,int result,String original_msg);
void addLog(int githubId,int outlookId, String msg,int result,String original_msg);
int deleteInfo(int github_id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@
@Service
public class OutlookLogServiceImpl extends ServiceImpl<OutlookLogMapper, OutlookLog> implements IOutlookLogService {
@Override
public void addLog(int githubId, String msg, int result,String original_msg) {
public void addLog(int githubId,int outlookId, String msg, int result,String original_msg) {
OutlookLog outlookLog = new OutlookLog();
outlookLog.setGithubId(githubId)
.setOutlookId(outlookId)
.setResult(result)
.setCallTime((int) (System.currentTimeMillis() / 1000))
.setMsg(msg)
Expand Down
11 changes: 7 additions & 4 deletions src/main/java/io/qyi/e5/service/rabbitMQ/impl/ListenerImpl.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package io.qyi.e5.service.rabbitMQ.impl;

import com.google.gson.Gson;
import com.rabbitmq.client.Channel;
import io.qyi.e5.outlook.bean.OutlookMq;
import io.qyi.e5.outlook.service.IOutlookService;
import io.qyi.e5.service.task.ITask;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -29,17 +31,18 @@ public class ListenerImpl {
@Autowired
ITask Task;

private static final Gson gson = new Gson();

@RabbitHandler
@RabbitListener(queues = "delay_queue1", containerFactory = "rabbitListenerContainerFactory")
public void listen(Message message, Channel channel) throws IOException {
log.info("消费者1开始处理消息: {},时间戳:{}" ,message,System.currentTimeMillis());
System.out.println("消费者1开始处理消息:"+System.currentTimeMillis());
int github_id = Integer.valueOf(new String(message.getBody()));
boolean b = Task.executeE5(github_id);
OutlookMq mq = gson.fromJson(new String(message.getBody()), OutlookMq.class);
boolean b = Task.executeE5(mq.getGithubId(),mq.getOutlookId());
channel.basicAck(message.getMessageProperties().getDeliveryTag(), true);
/*再次进行添加任务*/
if (b) {
Task.sendTaskOutlookMQ(github_id);
Task.sendTaskOutlookMQ(mq.getGithubId(),mq.getOutlookId());
}
}
}
4 changes: 2 additions & 2 deletions src/main/java/io/qyi/e5/service/task/ITask.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
* @create: 2020-04-16 16:51
**/
public interface ITask {
void sendTaskOutlookMQ(int github_id);
void sendTaskOutlookMQ(int github_id, int outlookId);

void sendTaskOutlookMQALL();

boolean executeE5(int github_id);
boolean executeE5(int github_id,int outlookId);

}
33 changes: 19 additions & 14 deletions src/main/java/io/qyi/e5/service/task/impl/TaskImpl.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.qyi.e5.service.task.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.qyi.e5.outlook.bean.OutlookMq;
import io.qyi.e5.outlook.entity.Outlook;
import io.qyi.e5.outlook.service.IOutlookService;
import io.qyi.e5.outlook_log.service.IOutlookLogService;
Expand Down Expand Up @@ -47,18 +48,22 @@ public class TaskImpl implements ITask {

@Override
@Async
public void sendTaskOutlookMQ(int github_id) {
Outlook Outlook = outlookService.getOne(new QueryWrapper<Outlook>().eq("github_id", github_id));
public void sendTaskOutlookMQ(int github_id, int outlookId) {
Outlook Outlook = outlookService.getOne(new QueryWrapper<Outlook>().eq("github_id", github_id).eq("id", outlookId));
if (Outlook == null) {
logger.warn("未找到此用户,github_id: {}", github_id);
return;
}
/*根据用户设置生成随机数*/
int Expiration = getRandom(Outlook.getCronTimeRandomStart(), Outlook.getCronTimeRandomEnd());
/*将此用户信息加入redis,如果存在则代表在队列中,同时提前10秒过期*/
if (!redisUtil.hasKey("user.mq:" + github_id)) {
redisUtil.set("user.mq:" + github_id, 0, Expiration - 10);
send(github_id, Expiration* 1000);
String rsKey = "user.mq:" + github_id + ".outlookId:" + outlookId;
if (!redisUtil.hasKey(rsKey)) {
redisUtil.set(rsKey, 0, Expiration - 10);
OutlookMq mq = new OutlookMq(github_id, outlookId);
send(mq, Expiration * 1000);
} else {
logger.info("Key 存在,不执行{}",rsKey);
}
}

Expand All @@ -80,17 +85,17 @@ public void sendTaskOutlookMQALL() {
}

@Override
public boolean executeE5(int github_id) {
Outlook Outlook = outlookService.getOne(new QueryWrapper<Outlook>().eq("github_id", github_id));
public boolean executeE5(int github_id,int outlookId) {
Outlook Outlook = outlookService.getOne(new QueryWrapper<Outlook>().eq("github_id", github_id).eq("id",outlookId));
if (Outlook == null) {
logger.warn("未找到此用户,github_id: {}", github_id);
return false;
}
boolean isExecuteE5 ;
String errorKey = "user.mq:" + github_id + ":error";
boolean isExecuteE5;
String errorKey = "user.mq:" + github_id + ":outlook.id:" + outlookId + ":error";
try {
int mail_count = outlookService.getMailList(Outlook);
outlookLogService.addLog(github_id, "ok", 1, "读取邮件数量:" + mail_count);
outlookLogService.addLog(github_id,outlookId, "ok", 1, "读取邮件数量:" + mail_count);
if (redisUtil.hasKey(errorKey)) {
redisUtil.del(errorKey);
}
Expand All @@ -102,13 +107,14 @@ public boolean executeE5(int github_id) {
redisUtil.set(errorKey, 1);
isExecuteE5 = true;
} else {
int error_count = (int)redisUtil.get(errorKey);
int error_count = (int) redisUtil.get(errorKey);
if (error_count >= errorCountMax) {
outlookLogService.addLog(github_id, "error", 0, e.getMessage());
outlookLogService.addLog(github_id, "error", 0, "检测到3次连续错误,下次将不再自动调用,请修正错误后再授权开启续订。");
outlookLogService.addLog(github_id, outlookId,"error", 0, e.getMessage());
outlookLogService.addLog(github_id, outlookId,"error", 0, "检测到3次连续错误,下次将不再自动调用,请修正错误后再授权开启续订。");
isExecuteE5 = false;
} else {
redisUtil.incr(errorKey, 1);
outlookLogService.addLog(github_id, outlookId,"error", 0, e.getMessage());
isExecuteE5 = true;
}
}
Expand All @@ -133,7 +139,6 @@ public void send(Object msg, int Expiration) {
MessageProperties messageProperties = message.getMessageProperties();
// 设置这条消息的过期时间
// messageProperties.setExpiration(Expiration);

messageProperties.setHeader("x-delay", Expiration);
return message;
}, correlationData);
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/io/qyi/e5/util/netRequest/OkHttpClientUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import okhttp3.*;

import javax.net.ssl.X509TrustManager;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.TimeUnit;
Expand All @@ -13,11 +14,14 @@ public class OkHttpClientUtil {
public static OkHttpClient client = null;

static {
X509TrustManager manager = SSLSocketClientUtil.getX509TrustManager();
client = new OkHttpClient.Builder()
.connectTimeout(connTimeOut, TimeUnit.SECONDS)
.readTimeout(readTimeOut, TimeUnit.SECONDS)
.writeTimeout(writeTimeOut, TimeUnit.SECONDS)
.retryOnConnectionFailure(true)
.sslSocketFactory(SSLSocketClientUtil.getSocketFactory(manager), manager)// 忽略校验
.hostnameVerifier(SSLSocketClientUtil.getHostnameVerifier())//忽略校验
.build();
}

Expand Down
Loading

0 comments on commit 62060f8

Please sign in to comment.