Commit f05b7b0a authored by wangsong's avatar wangsong

change data

parent 023eac58
...@@ -7,7 +7,7 @@ import org.springframework.boot.web.support.SpringBootServletInitializer; ...@@ -7,7 +7,7 @@ import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.ComponentScan;
@AlphaFrameworkApplication @AlphaFrameworkApplication
@ComponentScan(basePackages = {"com.chineseall.eden.authcenter.agent","com.chineseall.eden.authcenter.log"}) @ComponentScan(basePackages = {"com.chineseall.eden.authcenter.agent","com.chineseall.eden.authcenter.log","com.chineseall.eden.authcenter.config"})
public class EdenAuthcenterAgentApplication extends SpringBootServletInitializer { public class EdenAuthcenterAgentApplication extends SpringBootServletInitializer {
@Override @Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
......
...@@ -10,6 +10,6 @@ public class ClientDataInfo { ...@@ -10,6 +10,6 @@ public class ClientDataInfo {
private String returnUrl; private String returnUrl;
private OauthType oauthType; private String oauthType;
} }
package com.chineseall.eden.authcenter.agent.controller; package com.chineseall.eden.authcenter.agent.controller;
import cn.sh.chineseall.framework.core.util.ArrayUtils; import cn.sh.chineseall.framework.core.util.ArrayUtils;
import cn.sh.chineseall.framework.core.util.CollectionUtils;
import cn.sh.chineseall.framework.core.util.StringUtils; import cn.sh.chineseall.framework.core.util.StringUtils;
import cn.sh.chineseall.framework.lang.calendar.DateUtils; import cn.sh.chineseall.framework.lang.calendar.DateUtils;
import com.alibaba.fastjson.JSON; import com.chineseall.eden.authcenter.agent.client.AuthUserInfo;
import com.chineseall.eden.authcenter.agent.client.*; import com.chineseall.eden.authcenter.agent.client.ClientDataInfo;
import com.chineseall.eden.authcenter.agent.oauth.OauthConfig; import com.chineseall.eden.authcenter.agent.client.UserType;
import com.chineseall.eden.authcenter.agent.oauth.OauthConfigItem;
import com.chineseall.eden.authcenter.agent.oauth.OauthType;
import com.chineseall.eden.authcenter.agent.utils.ConvertMessageMapToBean; import com.chineseall.eden.authcenter.agent.utils.ConvertMessageMapToBean;
import com.chineseall.eden.authcenter.agent.utils.EncodeUtil; import com.chineseall.eden.authcenter.agent.utils.EncodeUtil;
import com.chineseall.eden.authcenter.config.entity.AuthClient;
import com.chineseall.eden.authcenter.config.entity.AuthSource;
import com.chineseall.eden.authcenter.config.service.AuthClientService;
import com.chineseall.eden.authcenter.config.service.AuthSourceService;
import com.chineseall.eden.authcenter.log.enums.LogType; import com.chineseall.eden.authcenter.log.enums.LogType;
import com.chineseall.eden.authcenter.log.model.AuthLog; import com.chineseall.eden.authcenter.log.model.AuthLog;
import com.chineseall.eden.authcenter.log.model.LogUserInfo; import com.chineseall.eden.authcenter.log.model.LogUserInfo;
...@@ -28,10 +31,7 @@ import javax.servlet.http.HttpServletRequest; ...@@ -28,10 +31,7 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.IOException; import java.io.IOException;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.util.Date; import java.util.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function; import java.util.function.Function;
import java.util.stream.Collectors; import java.util.stream.Collectors;
...@@ -41,11 +41,16 @@ public class AuthController { ...@@ -41,11 +41,16 @@ public class AuthController {
Logger logger = Logger.getLogger(AuthController.class); Logger logger = Logger.getLogger(AuthController.class);
@Autowired // @Autowired
private OauthClient oauthClient; // private OauthClient oauthClient;
//
// @Autowired
// private OauthConfig oauthConfig;
@Autowired @Autowired
private OauthConfig oauthConfig; private AuthClientService authClientService;
@Autowired
private AuthSourceService authSourceService;
@Resource @Resource
private AuthLogService authLogService; private AuthLogService authLogService;
...@@ -56,7 +61,10 @@ public class AuthController { ...@@ -56,7 +61,10 @@ public class AuthController {
public ModelAndView index(HttpServletRequest request, HttpServletResponse response) { public ModelAndView index(HttpServletRequest request, HttpServletResponse response) {
ModelAndView modelAndView = new ModelAndView(); ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("ds"); modelAndView.setViewName("ds");
Map<String, ClientItem> clientItemMap = oauthClient.getClients().stream().collect(Collectors.toMap(ClientItem::getClientId, Function.identity())); List<AuthClient> authClientList = authClientService.listAll();
List<AuthSource> authSourceList = authSourceService.listAll();
Map<String, AuthClient> clientMap = authClientList.stream().collect(Collectors.toMap(AuthClient::getKey, Function.identity(), (o1, o2) -> o1));
// Map<String, ClientItem> clientItemMap = oauthClient.getClients().stream().collect(Collectors.toMap(ClientItem::getClientId, Function.identity()));
try { try {
String clientId = request.getParameter("client_id"); String clientId = request.getParameter("client_id");
...@@ -67,16 +75,16 @@ public class AuthController { ...@@ -67,16 +75,16 @@ public class AuthController {
authLog.setLogType(LogType.login); authLog.setLogType(LogType.login);
if (StringUtils.isNotEmpty(clientId)){ if (StringUtils.isNotEmpty(clientId)){
ClientItem clientItem = clientItemMap.get(clientId); AuthClient clientItem = clientMap.get(clientId);
if (null == clientItem) { if (null == clientItem) {
modelAndView.setViewName("error"); modelAndView.setViewName("error");
modelAndView.addObject("message", "client_id为空或者不正确"); modelAndView.addObject("message", "client_id为空或者不正确");
return modelAndView; return modelAndView;
} }
authLog.setClientId(clientId); authLog.setClientId(clientId);
authLog.setAuthSource(clientItem.getClientName()); authLog.setAuthSource(clientItem.getName());
String sign = request.getParameter("sign"); String sign = request.getParameter("sign");
String signOrigin = clientItem.getClientId() + "$$" + clientItem.getClientSecret(); String signOrigin = clientItem.getKey() + "$$" + clientItem.getSecret();
String md5Hex = EncodeUtil.md5(signOrigin); String md5Hex = EncodeUtil.md5(signOrigin);
if (!Objects.equals(sign, md5Hex)) { if (!Objects.equals(sign, md5Hex)) {
...@@ -93,22 +101,21 @@ public class AuthController { ...@@ -93,22 +101,21 @@ public class AuthController {
if(StringUtils.isNotEmpty(loginType)) { if(StringUtils.isNotEmpty(loginType)) {
if (StringUtils.isNotEmpty(returnUrl) && (returnUrl.contains("readinglab") || returnUrl.contains("read.etextbook.cn"))){ if (StringUtils.isNotEmpty(returnUrl) && (returnUrl.contains("readinglab") || returnUrl.contains("read.etextbook.cn"))){
// 阅览室日志分离 // 阅览室日志分离
ClientItem item = clientItemMap.get("readingroomClientId"); AuthClient item = clientMap.get("readingroomClientId");
authLog.setClientId("readingroomClientId"); authLog.setClientId("readingroomClientId");
authLog.setAuthSource(item.getClientName()); authLog.setAuthSource(item.getName());
}else if ("cloudcourse".equals(business_client) || "cloudcourse-test".equals(business_client)){ }else if ("cloudcourse".equals(business_client) || "cloudcourse-test".equals(business_client)){
ClientItem item = clientItemMap.get("cloudcourse"); AuthClient item = clientMap.get("cloudcourse");
authLog.setClientId("cloudcourse"); authLog.setClientId("cloudcourse");
authLog.setAuthSource(item.getClientName()); authLog.setAuthSource(item.getName());
}else if ("adaptive-learning".equals(business_client)){ }else if ("adaptive-learning".equals(business_client)){
ClientItem item = clientItemMap.get("adaptive-learning"); AuthClient item = clientMap.get("adaptive-learning");
authLog.setClientId("adaptive-learning"); authLog.setClientId("adaptive-learning");
authLog.setAuthSource(item.getClientName()); authLog.setAuthSource(item.getName());
} }
OauthType oauthType = OauthType.getValue(loginType); if (authSourceList.stream().anyMatch(p -> Objects.equals(p.getKey(), loginType))){
if (oauthType != null){
modelAndView.addObject("loginType",loginType); modelAndView.addObject("loginType",loginType);
}else { }else {
modelAndView.addObject("loginType",""); modelAndView.addObject("loginType","");
...@@ -119,21 +126,29 @@ public class AuthController { ...@@ -119,21 +126,29 @@ public class AuthController {
} }
authLogService.save(authLog); authLogService.save(authLog);
String logId = authLog.getId();
String dianjiaoguanLoinUrl = generateLoginUrl(returnUrl, OauthType.dianjiaoguan.getCode(), authLog.getId()); if(CollectionUtils.isNotEmpty(authSourceList)){
modelAndView.addObject("dianjiaoguanLoinUrl", dianjiaoguanLoinUrl); authSourceList.forEach(p -> {
String edenoperationLoginUrl = generateLoginUrl(returnUrl, OauthType.edenoperation.getCode(), authLog.getId()); modelAndView.addObject(p.getKey()+"LoginUrl", generateLoginUrl(returnUrl, p, logId));
modelAndView.addObject("edenoperationLoginUrl", edenoperationLoginUrl); });
OauthType[] oauthTypes = OauthType.values();
for (OauthType oauthType : oauthTypes) {
if ("idp".equals(oauthType.getType())){
String idpAuthTypeUrl = generateSpLoginUrl(returnUrl, oauthType.getCode(), authLog.getId());
modelAndView.addObject(oauthType.getCode()+"LoginUrl", idpAuthTypeUrl);
}
} }
// String dianjiaoguanLoinUrl = generateLoginUrl(returnUrl, OauthType.dianjiaoguan.getCode(), authLog.getId());
// modelAndView.addObject("dianjiaoguanLoinUrl", dianjiaoguanLoinUrl);
// String edenoperationLoginUrl = generateLoginUrl(returnUrl, OauthType.edenoperation.getCode(), authLog.getId());
// modelAndView.addObject("edenoperationLoginUrl", edenoperationLoginUrl);
//
// OauthType[] oauthTypes = OauthType.values();
// for (OauthType oauthType : oauthTypes) {
// if ("idp".equals(oauthType.getType())){
//
// String idpAuthTypeUrl = generateSpLoginUrl(returnUrl, oauthType.getCode(), authLog.getId());
// modelAndView.addObject(oauthType.getCode()+"LoginUrl", idpAuthTypeUrl);
// }
// }
modelAndView.addObject("logId", authLog.getId()); modelAndView.addObject("logId", authLog.getId());
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
...@@ -155,8 +170,8 @@ public class AuthController { ...@@ -155,8 +170,8 @@ public class AuthController {
String os = request.getParameter("os"); String os = request.getParameter("os");
try { try {
AuthLog authLog = authLogService.getById(logId); AuthLog authLog = authLogService.getById(logId);
OauthConfigItem oauthConfigItem = oauthConfig.getItems().get(oauthType); AuthSource authSource = authSourceService.getAuthSourceByKey(oauthType);
authLog.setFowardUrl(oauthConfigItem.getOauthUrl()); authLog.setFowardUrl(authSource.getOauthUrl());
authLog.setOauthType(oauthType); authLog.setOauthType(oauthType);
authLog.setBrowser(browser); authLog.setBrowser(browser);
authLog.setVersion(version); authLog.setVersion(version);
...@@ -184,17 +199,19 @@ public class AuthController { ...@@ -184,17 +199,19 @@ public class AuthController {
public ModelAndView logout(HttpServletRequest request, HttpServletResponse response) { public ModelAndView logout(HttpServletRequest request, HttpServletResponse response) {
ModelAndView modelAndView = new ModelAndView(); ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("logout"); modelAndView.setViewName("logout");
Map<String, ClientItem> clientItemMap = oauthClient.getClients().stream().collect(Collectors.toMap(ClientItem::getClientId, Function.identity())); List<AuthClient> authClientList = authClientService.listAll();
Map<String, AuthClient> authClientMap = authClientList.stream().collect(Collectors.toMap(AuthClient::getKey, Function.identity(), (o1, o2) -> o1));
// Map<String, ClientItem> clientItemMap = oauthClient.getClients().stream().collect(Collectors.toMap(ClientItem::getClientId, Function.identity()));
try { try {
String clientId = request.getParameter("client_id"); String clientId = request.getParameter("client_id");
ClientItem clientItem = clientItemMap.get(clientId); AuthClient clientItem = authClientMap.get(clientId);
if (clientItem == null) { if (clientItem == null) {
modelAndView.setViewName("error"); modelAndView.setViewName("error");
modelAndView.addObject("message", "client_id为空或者不正确"); modelAndView.addObject("message", "client_id为空或者不正确");
return modelAndView; return modelAndView;
} }
String sign = request.getParameter("sign"); String sign = request.getParameter("sign");
String signOrigin = clientItem.getClientId() + "$$" + clientItem.getClientSecret(); String signOrigin = clientItem.getKey() + "$$" + clientItem.getSecret();
String md5Hex = EncodeUtil.md5(signOrigin); String md5Hex = EncodeUtil.md5(signOrigin);
if (!Objects.equals(sign, md5Hex)) { if (!Objects.equals(sign, md5Hex)) {
...@@ -214,8 +231,10 @@ public class AuthController { ...@@ -214,8 +231,10 @@ public class AuthController {
modelAndView.addObject("message", "登出方式为空"); modelAndView.addObject("message", "登出方式为空");
return modelAndView; return modelAndView;
} }
OauthType type = OauthType.valueOf(oauthType);
if(type == null){ List<AuthSource> authSourceList = authSourceService.listAll();
AuthSource authSource = authSourceList.stream().filter(p -> Objects.equals(p.getKey(), oauthType)).findFirst().orElse(null);
if(authSource == null){
modelAndView.setViewName("error"); modelAndView.setViewName("error");
modelAndView.addObject("message", "登出方式错误"); modelAndView.addObject("message", "登出方式错误");
return modelAndView; return modelAndView;
...@@ -224,19 +243,22 @@ public class AuthController { ...@@ -224,19 +243,22 @@ public class AuthController {
authLog.setOauthType(oauthType); authLog.setOauthType(oauthType);
authLog.setReturnUrl(service); authLog.setReturnUrl(service);
authLog.setClientId(clientId); authLog.setClientId(clientId);
authLog.setAuthSource(clientItem.getClientName()); authLog.setAuthSource(clientItem.getName());
authLog.setLogType(LogType.logout); authLog.setLogType(LogType.logout);
authLogService.save(authLog); authLogService.save(authLog);
if(StringUtils.isNotEmpty(service)) { if(StringUtils.isNotEmpty(service)) {
if (!OauthType.dianjiaoguan.equals(type) && !OauthType.edenoperation.equals(type)){ if(Objects.equals(authSource.getAuthType(), 1) || Objects.equals(authSource.getKey(), "qpjy")){
if (OauthType.qpjy.equals(type)){ response.sendRedirect(generateLogoutUrl(service, authSource));
response.sendRedirect(generateSpLogoutUrl(service,type.name()));
}
}else{
response.sendRedirect(generateLogoutUrl(service, type.name()));
} }
// if (!OauthType.dianjiaoguan.equals(type) && !OauthType.edenoperation.equals(type)){
// if (OauthType.qpjy.equals(type)){
// response.sendRedirect(generateSpLogoutUrl(service,type.name()));
//
// }
//
// }else{
// response.sendRedirect(generateLogoutUrl(service, type.name()));
// }
} }
return null; return null;
} catch (Exception e) { } catch (Exception e) {
...@@ -288,9 +310,9 @@ public class AuthController { ...@@ -288,9 +310,9 @@ public class AuthController {
Map<String, Object> userInfoMap = MyRestTemplate.exchange(HttpMethod.GET, userInfoUrl, null, null, HashMap.class); Map<String, Object> userInfoMap = MyRestTemplate.exchange(HttpMethod.GET, userInfoUrl, null, null, HashMap.class);
// 转换用户信息 // 转换用户信息
AuthUserInfo userInfo = null; AuthUserInfo userInfo = null;
if(oauthType.equals(OauthType.dianjiaoguan.toString())) if(oauthType.equals("dianjiaoguan"))
userInfo = ConvertMessageMapToBean.converDianjiaoguanData(userInfoMap); userInfo = ConvertMessageMapToBean.converDianjiaoguanData(userInfoMap);
else if(oauthType.equals(OauthType.edenoperation.toString())) else if(oauthType.equals("edenoperation"))
userInfo = ConvertMessageMapToBean.converEdenoperationData(userInfoMap); userInfo = ConvertMessageMapToBean.converEdenoperationData(userInfoMap);
// 封装下游数据 // 封装下游数据
ClientDataInfo clientDataInfo = new ClientDataInfo(); ClientDataInfo clientDataInfo = new ClientDataInfo();
...@@ -322,7 +344,7 @@ public class AuthController { ...@@ -322,7 +344,7 @@ public class AuthController {
authLog.setLogUserInfo(logUserInfo); authLog.setLogUserInfo(logUserInfo);
} }
clientDataInfo.setAuthUserInfo(userInfo); clientDataInfo.setAuthUserInfo(userInfo);
clientDataInfo.setOauthType(OauthType.valueOf(oauthType)); clientDataInfo.setOauthType(oauthType);
modelAndView.addObject("clientDataInfo", clientDataInfo); modelAndView.addObject("clientDataInfo", clientDataInfo);
modelAndView.addObject("successPostUrl", returnUrl); modelAndView.addObject("successPostUrl", returnUrl);
...@@ -394,7 +416,7 @@ public class AuthController { ...@@ -394,7 +416,7 @@ public class AuthController {
authLog.setLogUserInfo(logUserInfo); authLog.setLogUserInfo(logUserInfo);
} }
clientDataInfo.setAuthUserInfo(userInfo); clientDataInfo.setAuthUserInfo(userInfo);
clientDataInfo.setOauthType(OauthType.valueOf(oauthType)); clientDataInfo.setOauthType(oauthType);
modelAndView.addObject("clientDataInfo", clientDataInfo); modelAndView.addObject("clientDataInfo", clientDataInfo);
modelAndView.addObject("successPostUrl", returnUrl); modelAndView.addObject("successPostUrl", returnUrl);
...@@ -423,108 +445,168 @@ public class AuthController { ...@@ -423,108 +445,168 @@ public class AuthController {
return null; return null;
} }
private String generateLoginUrl(String returnUrl, String oauthType, String logId) {
//oauthConfig + "/authorize?client_id=testClentId&redirect_uri=http%3a%2f%2f192.168.17.129%3a7774%2fauth%2floginsuccess.do&state=state" private String generateLoginUrl(String returnUrl, AuthSource authSource, String logId){
StringBuilder loginUrlBuilder = new StringBuilder(); StringBuilder loginUrlBuilder = new StringBuilder();
OauthConfigItem oauthConfigItem = oauthConfig.getItems().get(oauthType); loginUrlBuilder.append(authSource.getOauthUrl());
loginUrlBuilder.append(oauthConfigItem.getOauthUrl()); if (!authSource.getOauthUrl().endsWith("/")) {
if (!oauthConfigItem.getOauthUrl().endsWith("/")) {
loginUrlBuilder.append("/"); loginUrlBuilder.append("/");
} }
loginUrlBuilder.append("oauth2.0/authorize?client_id=").append(oauthConfigItem.getClientId()); if(Objects.equals(authSource.getAuthType(), 1)){
String loginSuccessUrl = oauthConfigItem.getLoginSuccessUrl(); // oauth 认证
loginSuccessUrl = loginSuccessUrl + "?oauth_type=" + oauthType + "&log_id=" + logId; loginUrlBuilder.append("oauth2.0/authorize?client_id=").append(authSource.getClientId());
if (StringUtils.isNotEmpty(returnUrl)) { loginUrlBuilder.append("&state=state&response_type=code");
Map<String, String> param = new HashMap<>(); }else if(Objects.equals(authSource.getAuthType(), 2)){
param.put("return_url", URLEncoder.encode(returnUrl)); // idp 认证
loginSuccessUrl = appendUrl(loginSuccessUrl, param); loginUrlBuilder.append("login?client_id=").append(authSource.getClientId());
String sign = EncodeUtil.md5( authSource.getClientId()+ "$$" + authSource.getClientSecret());
loginUrlBuilder.append("&sign=").append(sign);
} }
loginUrlBuilder.append("&redirect_uri=").append(URLEncoder.encode(loginSuccessUrl));
loginUrlBuilder.append("&state=state&response_type=code"); String loginSuccessUrl = authSource.getLoginSuccessUrl();
return loginUrlBuilder.toString(); loginSuccessUrl = loginSuccessUrl + "?oauth_type=" + authSource.getKey() + "&log_id=" + logId;
}
private String generateSpLoginUrl(String returnUrl, String oauthType, String logId) {
//oauthConfig + "/authorize?client_id=testClentId&redirect_uri=http%3a%2f%2f192.168.17.129%3a7774%2fauth%2floginsuccess.do&state=state"
StringBuilder loginUrlBuilder = new StringBuilder();
OauthConfigItem oauthConfigItem = oauthConfig.getItems().get(oauthType);
loginUrlBuilder.append(oauthConfigItem.getOauthUrl());
if (!oauthConfigItem.getOauthUrl().endsWith("/")) {
loginUrlBuilder.append("/");
}
loginUrlBuilder.append("login?client_id=").append(oauthConfigItem.getClientId());
String loginSuccessUrl = oauthConfigItem.getLoginSuccessUrl();
loginSuccessUrl = loginSuccessUrl + "?log_id=" + logId+"&oauth_type=" + oauthType ;
if (StringUtils.isNotEmpty(returnUrl)) { if (StringUtils.isNotEmpty(returnUrl)) {
Map<String, String> param = new HashMap<>(); Map<String, String> param = new HashMap<>();
param.put("return_url", URLEncoder.encode(returnUrl)); param.put("return_url", URLEncoder.encode(returnUrl));
loginSuccessUrl = appendUrl(loginSuccessUrl, param); loginSuccessUrl = appendUrl(loginSuccessUrl, param);
} }
loginUrlBuilder.append("&redirect_uri=").append(URLEncoder.encode(loginSuccessUrl)); loginUrlBuilder.append("&redirect_uri=").append(URLEncoder.encode(loginSuccessUrl));
String sign = EncodeUtil.md5( oauthConfigItem.getClientId()+ "$$" + oauthConfigItem.getClientSecret());
loginUrlBuilder.append("&sign="+sign);
return loginUrlBuilder.toString();
}
private String generateLogoutUrl(String returnUrl, String oauthType) {
//oauthConfig + "/authorize?client_id=testClentId&redirect_uri=http%3a%2f%2f192.168.17.129%3a7774%2fauth%2floginsuccess.do&state=state"
StringBuilder loginUrlBuilder = new StringBuilder();
OauthConfigItem oauthConfigItem = oauthConfig.getItems().get(oauthType);
loginUrlBuilder.append(oauthConfigItem.getOauthUrl());
if (!oauthConfigItem.getOauthUrl().endsWith("/")) {
loginUrlBuilder.append("/");
}
String service = oauthConfigItem.getLogoutSuccessUrl();
if (StringUtils.isNotEmpty(returnUrl)) {
Map<String, String> param = new HashMap<>();
param.put("return_url", URLEncoder.encode(returnUrl));
service = appendUrl(service, param);
}
loginUrlBuilder.append("logout?service=").append(URLEncoder.encode(service));
return loginUrlBuilder.toString(); return loginUrlBuilder.toString();
} }
// private String generateLoginUrl(String returnUrl, String oauthType, String logId) {
private String generateSpLogoutUrl(String returnUrl, String oauthType) { // //oauthConfig + "/authorize?client_id=testClentId&redirect_uri=http%3a%2f%2f192.168.17.129%3a7774%2fauth%2floginsuccess.do&state=state"
// StringBuilder loginUrlBuilder = new StringBuilder();
if (oauthType.equals("qpjy")){ // OauthConfigItem oauthConfigItem = oauthConfig.getItems().get(oauthType);
// loginUrlBuilder.append(oauthConfigItem.getOauthUrl());
// if (!oauthConfigItem.getOauthUrl().endsWith("/")) {
// loginUrlBuilder.append("/");
// }
// loginUrlBuilder.append("oauth2.0/authorize?client_id=").append(oauthConfigItem.getClientId());
// String loginSuccessUrl = oauthConfigItem.getLoginSuccessUrl();
// loginSuccessUrl = loginSuccessUrl + "?oauth_type=" + oauthType + "&log_id=" + logId;
// if (StringUtils.isNotEmpty(returnUrl)) {
// Map<String, String> param = new HashMap<>();
// param.put("return_url", URLEncoder.encode(returnUrl));
// loginSuccessUrl = appendUrl(loginSuccessUrl, param);
// }
// loginUrlBuilder.append("&redirect_uri=").append(URLEncoder.encode(loginSuccessUrl));
//
// loginUrlBuilder.append("&state=state&response_type=code");
// return loginUrlBuilder.toString();
// }
//
// private String generateSpLoginUrl(String returnUrl, String oauthType, String logId) {
// //oauthConfig + "/authorize?client_id=testClentId&redirect_uri=http%3a%2f%2f192.168.17.129%3a7774%2fauth%2floginsuccess.do&state=state"
// StringBuilder loginUrlBuilder = new StringBuilder();
// OauthConfigItem oauthConfigItem = oauthConfig.getItems().get(oauthType);
// loginUrlBuilder.append(oauthConfigItem.getOauthUrl());
// if (!oauthConfigItem.getOauthUrl().endsWith("/")) {
// loginUrlBuilder.append("/");
// }
// loginUrlBuilder.append("login?client_id=").append(oauthConfigItem.getClientId());
// String loginSuccessUrl = oauthConfigItem.getLoginSuccessUrl();
// loginSuccessUrl = loginSuccessUrl + "?log_id=" + logId+"&oauth_type=" + oauthType ;
// if (StringUtils.isNotEmpty(returnUrl)) {
// Map<String, String> param = new HashMap<>();
// param.put("return_url", URLEncoder.encode(returnUrl));
// loginSuccessUrl = appendUrl(loginSuccessUrl, param);
// }
// loginUrlBuilder.append("&redirect_uri=").append(URLEncoder.encode(loginSuccessUrl));
// String sign = EncodeUtil.md5( oauthConfigItem.getClientId()+ "$$" + oauthConfigItem.getClientSecret());
// loginUrlBuilder.append("&sign="+sign);
// return loginUrlBuilder.toString();
// }
private String generateLogoutUrl(String returnUrl, AuthSource authSource){
if(Objects.equals(authSource.getKey(), "qpjy")){
returnUrl = "https://idp.qpedu.cn/logout/logout.html?redirect_url=" + returnUrl; returnUrl = "https://idp.qpedu.cn/logout/logout.html?redirect_url=" + returnUrl;
returnUrl = "https://sp.etextbook.cn/Shibboleth.sso/Logout?return=" + URLEncoder.encode(returnUrl); returnUrl = "https://sp.etextbook.cn/Shibboleth.sso/Logout?return=" + URLEncoder.encode(returnUrl);
return returnUrl; return returnUrl;
} }
StringBuilder loginUrlBuilder = new StringBuilder();
OauthConfigItem oauthConfigItem = oauthConfig.getItems().get(oauthType);
loginUrlBuilder.append(oauthConfigItem.getOauthUrl()); StringBuilder loginUrlBuilder = new StringBuilder();
if (!oauthConfigItem.getOauthUrl().endsWith("/")) { loginUrlBuilder.append(authSource.getOauthUrl());
if (!authSource.getOauthUrl().endsWith("/")) {
loginUrlBuilder.append("/"); loginUrlBuilder.append("/");
} }
String service = oauthConfigItem.getLogoutSuccessUrl(); String service = authSource.getLogoutSuccessUrl();
if (StringUtils.isNotEmpty(returnUrl)) { if (StringUtils.isNotEmpty(returnUrl)) {
Map<String, String> param = new HashMap<>(); Map<String, String> param = new HashMap<>();
param.put("return_url", URLEncoder.encode(returnUrl)); param.put("return_url", URLEncoder.encode(returnUrl));
service = appendUrl(service, param); service = appendUrl(service, param);
} }
String clientId = oauthConfigItem.getClientId();
String signOrigin = oauthConfigItem.getClientId() + "$$" + oauthConfigItem.getClientSecret();
String md5Hex = EncodeUtil.md5(signOrigin);
loginUrlBuilder.append("logout?service=").append(URLEncoder.encode(service)); loginUrlBuilder.append("logout?service=").append(URLEncoder.encode(service));
loginUrlBuilder.append("&client_id="+clientId); if(Objects.equals(authSource.getAuthType(), 2)){
loginUrlBuilder.append("&sign="+md5Hex); // idp 认证
String clientId = authSource.getClientId();
String signOrigin = authSource.getClientId() + "$$" + authSource.getClientSecret();
String md5Hex = EncodeUtil.md5(signOrigin);
loginUrlBuilder.append("&client_id="+clientId);
loginUrlBuilder.append("&sign="+md5Hex);
}
return loginUrlBuilder.toString(); return loginUrlBuilder.toString();
} }
// private String generateLogoutUrl(String returnUrl, String oauthType) {
// //oauthConfig + "/authorize?client_id=testClentId&redirect_uri=http%3a%2f%2f192.168.17.129%3a7774%2fauth%2floginsuccess.do&state=state"
// StringBuilder loginUrlBuilder = new StringBuilder();
// OauthConfigItem oauthConfigItem = oauthConfig.getItems().get(oauthType);
//
// loginUrlBuilder.append(oauthConfigItem.getOauthUrl());
// if (!oauthConfigItem.getOauthUrl().endsWith("/")) {
// loginUrlBuilder.append("/");
// }
// String service = oauthConfigItem.getLogoutSuccessUrl();
// if (StringUtils.isNotEmpty(returnUrl)) {
// Map<String, String> param = new HashMap<>();
// param.put("return_url", URLEncoder.encode(returnUrl));
// service = appendUrl(service, param);
// }
// loginUrlBuilder.append("logout?service=").append(URLEncoder.encode(service));
// return loginUrlBuilder.toString();
// }
//
//
// private String generateSpLogoutUrl(String returnUrl, String oauthType) {
//
// if (oauthType.equals("qpjy")){
// returnUrl = "https://idp.qpedu.cn/logout/logout.html?redirect_url=" + returnUrl;
// returnUrl = "https://sp.etextbook.cn/Shibboleth.sso/Logout?return=" + URLEncoder.encode(returnUrl);
// return returnUrl;
// }
// StringBuilder loginUrlBuilder = new StringBuilder();
// OauthConfigItem oauthConfigItem = oauthConfig.getItems().get(oauthType);
//
// loginUrlBuilder.append(oauthConfigItem.getOauthUrl());
// if (!oauthConfigItem.getOauthUrl().endsWith("/")) {
// loginUrlBuilder.append("/");
// }
// String service = oauthConfigItem.getLogoutSuccessUrl();
// if (StringUtils.isNotEmpty(returnUrl)) {
// Map<String, String> param = new HashMap<>();
// param.put("return_url", URLEncoder.encode(returnUrl));
// service = appendUrl(service, param);
// }
//
// String clientId = oauthConfigItem.getClientId();
// String signOrigin = oauthConfigItem.getClientId() + "$$" + oauthConfigItem.getClientSecret();
// String md5Hex = EncodeUtil.md5(signOrigin);
//
// loginUrlBuilder.append("logout?service=").append(URLEncoder.encode(service));
// loginUrlBuilder.append("&client_id="+clientId);
// loginUrlBuilder.append("&sign="+md5Hex);
// return loginUrlBuilder.toString();
// }
private String generateUserInfoUrl(String accessToken, String oauthType) { private String generateUserInfoUrl(String accessToken, String oauthType) {
//String url = "http://castest.edu.sh.cn/CAS/oauth2.0/accessToken?client_id=testClentId&client_secret=testClientSecret&redirect_uri=http%3a%2f%2f192.168.17.129%3a7774%2fauth%2floginsuccess2.do&code=" + code; //String url = "http://castest.edu.sh.cn/CAS/oauth2.0/accessToken?client_id=testClentId&client_secret=testClientSecret&redirect_uri=http%3a%2f%2f192.168.17.129%3a7774%2fauth%2floginsuccess2.do&code=" + code;
StringBuilder builder = new StringBuilder(); StringBuilder builder = new StringBuilder();
OauthConfigItem oauthConfigItem = oauthConfig.getItems().get(oauthType); // OauthConfigItem oauthConfigItem = oauthConfig.getItems().get(oauthType);
builder.append(oauthConfigItem.getOauthUrl()); AuthSource authSource = authSourceService.getAuthSourceByKey(oauthType);
if (!oauthConfigItem.getOauthUrl().endsWith("/")) { builder.append(authSource.getOauthUrl());
if (!authSource.getOauthUrl().endsWith("/")) {
builder.append("/"); builder.append("/");
} }
builder.append("oauth2.0/profile?access_token=").append(accessToken); builder.append("oauth2.0/profile?access_token=").append(accessToken);
...@@ -534,15 +616,16 @@ public class AuthController { ...@@ -534,15 +616,16 @@ public class AuthController {
private String generateAccessTokenUrl(String code, String oauthType) { private String generateAccessTokenUrl(String code, String oauthType) {
//String url = "http://castest.edu.sh.cn/CAS/oauth2.0/accessToken?client_id=testClentId&client_secret=testClientSecret&redirect_uri=http%3a%2f%2f192.168.17.129%3a7774%2fauth%2floginsuccess2.do&code=" + code; //String url = "http://castest.edu.sh.cn/CAS/oauth2.0/accessToken?client_id=testClentId&client_secret=testClientSecret&redirect_uri=http%3a%2f%2f192.168.17.129%3a7774%2fauth%2floginsuccess2.do&code=" + code;
StringBuilder builder = new StringBuilder(); StringBuilder builder = new StringBuilder();
OauthConfigItem oauthConfigItem = oauthConfig.getItems().get(oauthType); // OauthConfigItem oauthConfigItem = oauthConfig.getItems().get(oauthType);
builder.append(oauthConfigItem.getOauthUrl()); AuthSource authSource = authSourceService.getAuthSourceByKey(oauthType);
if (!oauthConfigItem.getOauthUrl().endsWith("/")) { builder.append(authSource.getOauthUrl());
if (!authSource.getOauthUrl().endsWith("/")) {
builder.append("/"); builder.append("/");
} }
builder.append("oauth2.0/accessToken?client_id=").append(oauthConfigItem.getClientId()); builder.append("oauth2.0/accessToken?client_id=").append(authSource.getClientId());
builder.append("&client_secret=").append(oauthConfigItem.getClientSecret()); builder.append("&client_secret=").append(authSource.getClientSecret());
builder.append("&code=").append(code); builder.append("&code=").append(code);
builder.append("&redirect_uri=").append(oauthConfigItem.getLoginSuccessUrl()); builder.append("&redirect_uri=").append(authSource.getLoginSuccessUrl());
builder.append("&grant_type=authorization_code"); builder.append("&grant_type=authorization_code");
return builder.toString(); return builder.toString();
} }
...@@ -574,11 +657,13 @@ public class AuthController { ...@@ -574,11 +657,13 @@ public class AuthController {
public ModelAndView chineseallLogin(HttpServletRequest request, HttpServletResponse response) { public ModelAndView chineseallLogin(HttpServletRequest request, HttpServletResponse response) {
ModelAndView modelAndView = new ModelAndView(); ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("toward"); modelAndView.setViewName("toward");
Map<String, ClientItem> clientItemMap = oauthClient.getClients().stream().collect(Collectors.toMap(ClientItem::getClientId, Function.identity())); List<AuthClient> authClientList = authClientService.listAll();
Map<String, AuthClient> clientMap = authClientList.stream().collect(Collectors.toMap(AuthClient::getKey, Function.identity(), (o1, o2) -> o1));
// Map<String, ClientItem> clientItemMap = oauthClient.getClients().stream().collect(Collectors.toMap(ClientItem::getClientId, Function.identity()));
try { try {
String clientId = request.getParameter("client_id"); String clientId = request.getParameter("client_id");
String returnUrl = request.getParameter("redirect_uri"); String returnUrl = request.getParameter("redirect_uri");
ClientItem clientItem = clientItemMap.get(clientId); AuthClient clientItem = clientMap.get(clientId);
AuthLog authLog = new AuthLog(); AuthLog authLog = new AuthLog();
authLog.setLogType(LogType.login); authLog.setLogType(LogType.login);
if (null == clientItem) { if (null == clientItem) {
...@@ -587,9 +672,9 @@ public class AuthController { ...@@ -587,9 +672,9 @@ public class AuthController {
return modelAndView; return modelAndView;
} }
authLog.setClientId(clientId); authLog.setClientId(clientId);
authLog.setAuthSource(clientItem.getClientName()); authLog.setAuthSource(clientItem.getName());
String sign = request.getParameter("sign"); String sign = request.getParameter("sign");
String signOrigin = clientItem.getClientId() + "$$" + clientItem.getClientSecret(); String signOrigin = clientItem.getKey() + "$$" + clientItem.getSecret();
String md5Hex = EncodeUtil.md5(signOrigin); String md5Hex = EncodeUtil.md5(signOrigin);
if (!Objects.equals(sign, md5Hex)) { if (!Objects.equals(sign, md5Hex)) {
...@@ -602,11 +687,13 @@ public class AuthController { ...@@ -602,11 +687,13 @@ public class AuthController {
modelAndView.addObject("message", "登录成功回调地址为空"); modelAndView.addObject("message", "登录成功回调地址为空");
return modelAndView; return modelAndView;
} }
authLog.setOauthType(OauthType.dianjiaoguan.getCode()); String key = "dianjiaoguan";
OauthConfigItem oauthConfigItem = oauthConfig.getItems().get(OauthType.dianjiaoguan.getCode()); AuthSource authSource = authSourceService.getAuthSourceByKey(key);
authLog.setFowardUrl(oauthConfigItem.getOauthUrl()); authLog.setOauthType(key);
// OauthConfigItem oauthConfigItem = oauthConfig.getItems().get(OauthType.dianjiaoguan.getCode());
authLog.setFowardUrl(authSource.getOauthUrl());
authLogService.save(authLog); authLogService.save(authLog);
String url = generateLoginUrl(returnUrl, OauthType.dianjiaoguan.getCode(), authLog.getId()); String url = generateLoginUrl(returnUrl, authSource, authLog.getId());
modelAndView.addObject("loginUrl", url); modelAndView.addObject("loginUrl", url);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
...@@ -620,17 +707,19 @@ public class AuthController { ...@@ -620,17 +707,19 @@ public class AuthController {
public ModelAndView shLogout(HttpServletRequest request, HttpServletResponse response) { public ModelAndView shLogout(HttpServletRequest request, HttpServletResponse response) {
ModelAndView modelAndView = new ModelAndView(); ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("logout"); modelAndView.setViewName("logout");
Map<String, ClientItem> clientItemMap = oauthClient.getClients().stream().collect(Collectors.toMap(ClientItem::getClientId, Function.identity())); List<AuthClient> authClientList = authClientService.listAll();
Map<String, AuthClient> clientMap = authClientList.stream().collect(Collectors.toMap(AuthClient::getKey, Function.identity(), (o1, o2) -> o1));
// Map<String, ClientItem> clientItemMap = oauthClient.getClients().stream().collect(Collectors.toMap(ClientItem::getClientId, Function.identity()));
try { try {
String clientId = request.getParameter("client_id"); String clientId = request.getParameter("client_id");
ClientItem clientItem = clientItemMap.get(clientId); AuthClient clientItem = clientMap.get(clientId);
if (clientItem == null) { if (clientItem == null) {
modelAndView.setViewName("error"); modelAndView.setViewName("error");
modelAndView.addObject("message", "client_id为空或者不正确"); modelAndView.addObject("message", "client_id为空或者不正确");
return modelAndView; return modelAndView;
} }
String sign = request.getParameter("sign"); String sign = request.getParameter("sign");
String signOrigin = clientItem.getClientId() + "$$" + clientItem.getClientSecret(); String signOrigin = clientItem.getKey() + "$$" + clientItem.getSecret();
String md5Hex = EncodeUtil.md5(signOrigin); String md5Hex = EncodeUtil.md5(signOrigin);
if (!Objects.equals(sign, md5Hex)) { if (!Objects.equals(sign, md5Hex)) {
...@@ -645,15 +734,18 @@ public class AuthController { ...@@ -645,15 +734,18 @@ public class AuthController {
return modelAndView; return modelAndView;
} }
String key = "dianjiaoguan";
AuthSource authSource = authSourceService.getAuthSourceByKey(key);
AuthLog authLog = new AuthLog(); AuthLog authLog = new AuthLog();
authLog.setOauthType(OauthType.dianjiaoguan.getCode()); authLog.setOauthType(key);
authLog.setReturnUrl(service); authLog.setReturnUrl(service);
authLog.setClientId(clientId); authLog.setClientId(clientId);
authLog.setAuthSource(clientItem.getClientName()); authLog.setAuthSource(clientItem.getName());
authLog.setLogType(LogType.logout); authLog.setLogType(LogType.logout);
authLogService.save(authLog); authLogService.save(authLog);
if(StringUtils.isNotEmpty(service)) { if(StringUtils.isNotEmpty(service)) {
response.sendRedirect(generateLogoutUrl(service, OauthType.dianjiaoguan.getCode())); response.sendRedirect(generateLogoutUrl(service, authSource));
} }
return null; return null;
} catch (Exception e) { } catch (Exception e) {
......
...@@ -321,7 +321,7 @@ public class UcenterController { ...@@ -321,7 +321,7 @@ public class UcenterController {
authLog.setLogUserInfo(logUserInfo); authLog.setLogUserInfo(logUserInfo);
} }
clientDataInfo.setAuthUserInfo(userInfo); clientDataInfo.setAuthUserInfo(userInfo);
clientDataInfo.setOauthType(OauthType.valueOf(oauthType)); clientDataInfo.setOauthType(oauthType);
modelAndView.addObject("clientDataInfo", clientDataInfo); modelAndView.addObject("clientDataInfo", clientDataInfo);
modelAndView.addObject("successPostUrl", returnUrl); modelAndView.addObject("successPostUrl", returnUrl);
...@@ -393,7 +393,7 @@ public class UcenterController { ...@@ -393,7 +393,7 @@ public class UcenterController {
authLog.setLogUserInfo(logUserInfo); authLog.setLogUserInfo(logUserInfo);
} }
clientDataInfo.setAuthUserInfo(userInfo); clientDataInfo.setAuthUserInfo(userInfo);
clientDataInfo.setOauthType(OauthType.valueOf(oauthType)); clientDataInfo.setOauthType(oauthType);
modelAndView.addObject("clientDataInfo", clientDataInfo); modelAndView.addObject("clientDataInfo", clientDataInfo);
modelAndView.addObject("successPostUrl", returnUrl); modelAndView.addObject("successPostUrl", returnUrl);
......
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