修改存储逻辑
This commit is contained in:
parent
bb20aa672c
commit
2d60b51c7c
@ -1,5 +1,7 @@
|
||||
package com.dnslin.onemanager.logic;
|
||||
|
||||
import com.dnslin.onemanager.pojo.Onedriveconfig;
|
||||
|
||||
/**
|
||||
* AuthToken
|
||||
* @author dnslin
|
||||
@ -7,27 +9,25 @@ package com.dnslin.onemanager.logic;
|
||||
* @ProjectName: Onemanager-java
|
||||
* @Version 1.0
|
||||
*/
|
||||
|
||||
public interface AuthToken {
|
||||
/**
|
||||
* 获取AccessToken
|
||||
* @author dnslin
|
||||
* @date 10/25
|
||||
* @param clientId
|
||||
* @param redirectUri
|
||||
* @param clientSecret
|
||||
* @return
|
||||
*/
|
||||
void getAccessToken(String clientId, String redirectUri,String clientSecret);
|
||||
/**
|
||||
*
|
||||
* @Description: 获取AccessToken
|
||||
* @param: config
|
||||
* @return void
|
||||
* @author DnsLin
|
||||
* @date 2021/10/31 18:40
|
||||
*/
|
||||
void getAccessToken(Onedriveconfig config);
|
||||
|
||||
/**
|
||||
* 刷新Refresh_token和AccessToken
|
||||
* @Description:
|
||||
* @param: clientId
|
||||
* @param: redirectUri
|
||||
* @param: clientSecret
|
||||
* @return java.lang.String
|
||||
*
|
||||
* @Description: 刷新Refresh_token和AccessToken
|
||||
* @param: config
|
||||
* @return void
|
||||
* @author DnsLin
|
||||
* @date 2021/10/29 23:38
|
||||
*/
|
||||
void getRefreshToken(String clientId, String redirectUri,String clientSecret);
|
||||
* @date 2021/10/31 18:40
|
||||
*/
|
||||
void getRefreshToken(Onedriveconfig config);
|
||||
}
|
||||
|
@ -4,8 +4,11 @@ import cn.hutool.core.lang.Console;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.dnslin.onemanager.exception.AppException;
|
||||
import com.dnslin.onemanager.logic.AuthToken;
|
||||
import com.dnslin.onemanager.pojo.Onedriveconfig;
|
||||
import com.dnslin.onemanager.result.ResponseEnum;
|
||||
import com.dnslin.onemanager.service.SaveConfig;
|
||||
import com.dnslin.onemanager.utils.HttpClientUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.servlet.ServletContext;
|
||||
@ -27,37 +30,39 @@ import java.util.Map;
|
||||
public class AuthTokenImpl extends HttpServlet implements AuthToken {
|
||||
private final ServletContext context = this.getServletContext();
|
||||
|
||||
@Autowired
|
||||
private SaveConfig saveConfig;
|
||||
@Override
|
||||
public void getAccessToken(String clientId, String redirectUri, String clientSecret) {
|
||||
public void getAccessToken(Onedriveconfig config) {
|
||||
String code = (String) context.getAttribute("code");
|
||||
if (code == null || code.isEmpty()) {
|
||||
throw new AppException(ResponseEnum.AUTH_CODE_ISNULL);
|
||||
}
|
||||
Map<String, String> param = new HashMap<String, String>();
|
||||
param.put("client_id", clientId);
|
||||
param.put("client_id", config.getClientid());
|
||||
param.put("scope", "files.readwrite.all files.readwrite offline_access");
|
||||
param.put("code", code);
|
||||
param.put("redirect_uri", redirectUri);
|
||||
param.put("redirect_uri", config.getRedirecturl());
|
||||
param.put("grant_type", "authorization_code");
|
||||
param.put("client_secret", clientSecret);
|
||||
extracted(HttpClientUtils.doPost("https://login.microsoftonline.com/common/oauth2/v2.0/token", param).getContent());
|
||||
param.put("client_secret", config.getRedirecturl());
|
||||
extracted(HttpClientUtils.doPost("https://login.microsoftonline.com/common/oauth2/v2.0/token", param).getContent(),null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void getRefreshToken(String clientId, String redirectUri, String clientSecret) {
|
||||
public void getRefreshToken(Onedriveconfig config) {
|
||||
if ((context.getAttribute("refresh_token") == null && context.getAttribute("refresh_token").toString().isEmpty()) || (context.getAttribute("access_token") == null && context.getAttribute("access_token").toString().isEmpty())) {
|
||||
Console.log("Refresh_token:==>" + context.getAttribute("refresh_token"));
|
||||
Console.log("Access_token:==>" + context.getAttribute("access_token"));
|
||||
throw new AppException(ResponseEnum.Token_invalid);
|
||||
}
|
||||
Map<String, String> param = new HashMap<String, String>();
|
||||
param.put("client_id", clientId);
|
||||
param.put("client_id", config.getClientid());
|
||||
param.put("scope", "files.readwrite.all files.readwrite offline_access");
|
||||
param.put("refresh_token", context.getAttribute("refresh_token").toString());
|
||||
param.put("redirect_uri", redirectUri);
|
||||
param.put("redirect_uri", config.getRedirecturl());
|
||||
param.put("grant_type", "refresh_token");
|
||||
param.put("client_secret", clientSecret);
|
||||
extracted(HttpClientUtils.doPost("https://login.microsoftonline.com/common/oauth2/v2.0/token", param).getContent());
|
||||
param.put("client_secret", config.getRedirecturl());
|
||||
extracted(HttpClientUtils.doPost("https://login.microsoftonline.com/common/oauth2/v2.0/token", param).getContent(),config);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -69,7 +74,7 @@ public class AuthTokenImpl extends HttpServlet implements AuthToken {
|
||||
* @author DnsLin
|
||||
* @date 2021/10/29 23:55
|
||||
*/
|
||||
private void extracted(String accessJson) {
|
||||
private void extracted(String accessJson,Onedriveconfig config) {
|
||||
if (accessJson == null || accessJson.isEmpty()) {
|
||||
throw new AppException(ResponseEnum.THE_RESULT_SET_IS_EMPTY);
|
||||
}
|
||||
@ -78,7 +83,11 @@ public class AuthTokenImpl extends HttpServlet implements AuthToken {
|
||||
String refresh_token = JSONObject.parseObject(accessJson).getString("refresh_token");
|
||||
Console.log("Access_token:==>" + access_token);
|
||||
Console.log("Refresh_token:==>" + refresh_token);
|
||||
config.setAccesstoken(access_token);
|
||||
config.setRefreshtoken(refresh_token);
|
||||
context.setAttribute("access_token", access_token);
|
||||
context.setAttribute("refresh_token", refresh_token);
|
||||
|
||||
saveConfig.saveOnedriveConfig(config);
|
||||
}
|
||||
}
|
||||
|
28
src/main/java/com/dnslin/onemanager/service/SaveConfig.java
Normal file
28
src/main/java/com/dnslin/onemanager/service/SaveConfig.java
Normal file
@ -0,0 +1,28 @@
|
||||
package com.dnslin.onemanager.service;/**
|
||||
* @author: DnsLin
|
||||
* @Title: SaveConfig
|
||||
* @ProjectName: Onemanager-java
|
||||
* @Description:
|
||||
* @date: 2021/10/31 18:26
|
||||
*/
|
||||
|
||||
import com.dnslin.onemanager.pojo.Onedriveconfig;
|
||||
|
||||
/**
|
||||
* @author: DnsLin
|
||||
* @Title: SaveConfig
|
||||
* @ProjectName: Onemanager-java
|
||||
* @Description:
|
||||
* @date: 2021/10/31 18:26
|
||||
*/
|
||||
public interface SaveConfig {
|
||||
/**
|
||||
*
|
||||
* @Description: 保存配置到数据库
|
||||
* @param: config
|
||||
* @return int
|
||||
* @author DnsLin
|
||||
* @date 2021/10/31 18:27
|
||||
*/
|
||||
void saveOnedriveConfig(Onedriveconfig config);
|
||||
}
|
@ -0,0 +1,42 @@
|
||||
package com.dnslin.onemanager.service.impl;/**
|
||||
* @author: DnsLin
|
||||
* @Title: SaveConfigImpl
|
||||
* @ProjectName: Onemanager-java
|
||||
* @Description:
|
||||
* @date: 2021/10/31 18:28
|
||||
*/
|
||||
|
||||
import com.dnslin.onemanager.exception.AppException;
|
||||
import com.dnslin.onemanager.mapper.OnedriveconfigMapper;
|
||||
import com.dnslin.onemanager.pojo.Onedriveconfig;
|
||||
import com.dnslin.onemanager.result.ResponseEnum;
|
||||
import com.dnslin.onemanager.service.SaveConfig;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
|
||||
@Service
|
||||
public class SaveConfigImpl implements SaveConfig {
|
||||
@Autowired
|
||||
private OnedriveconfigMapper onedriveconfigMapper;
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public void saveOnedriveConfig(Onedriveconfig config) {
|
||||
if (config == null) {
|
||||
throw new AppException(ResponseEnum.OBJECT_IS_EMPTY);
|
||||
}
|
||||
// 当clientId存在时候 为更新数据 不存在就插入
|
||||
Onedriveconfig onedriveconfig = onedriveconfigMapper.selectByPrimaryKey(config.getClientid());
|
||||
if (onedriveconfig == null) {
|
||||
int state = onedriveconfigMapper.insertSelective(config);
|
||||
if (state < 1) {
|
||||
throw new AppException(ResponseEnum.INSERT_THE_FAILURE);
|
||||
}
|
||||
}else {
|
||||
onedriveconfigMapper.updateByPrimaryKey(config);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user