Commit e8ba17e4 authored by sam.x.wang's avatar sam.x.wang

sso问题

parent 752e03b8
package pwc.taxtech.atms.common;
import com.alibaba.fastjson.annotation.JSONField;
/**
* @Auther: Gary J Li
* @Date: 05/03/2019 18:25
* @Description:
*/
public class DDUserInfo {
private int uid;
private String phone;
@JSONField(name = "username_zh")
private String username_zh;
private String email;
private String username;
public int getUid() {
return uid;
}
public void setUid(int uid) {
this.uid = uid;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getUsername_zh() {
return username_zh;
}
public void setUsername_zh(String username_zh) {
this.username_zh = username_zh;
}
}
package pwc.taxtech.atms.common;
import com.alibaba.fastjson.annotation.JSONField;
/**
* @Auther: Gary J Li
* @Date: 05/03/2019 18:25
* @Description:
*/
public class DDUserInfoRes {
private int errno;
@JSONField(name = "data")
private DDUserInfo ddUserInfo;
private String errmsg;
public int getErrno() {
return errno;
}
public void setErrno(int errno) {
this.errno = errno;
}
public DDUserInfo getDdUserInfo() {
return ddUserInfo;
}
public void setDdUserInfo(DDUserInfo ddUserInfo) {
this.ddUserInfo = ddUserInfo;
}
public String getErrmsg() {
return errmsg;
}
public void setErrmsg(String errmsg) {
this.errmsg = errmsg;
}
}
......@@ -130,7 +130,7 @@ public class IndexController {
}catch (Exception e){
logger.error("ddSSOCallback error",e);
}
return "redirect:"+tempContextUrl+"Account/LogOn";
return "redirect:"+tempContextUrl;
}
@RequestMapping(value = {"/admin", "/admin.html"}, method = RequestMethod.GET)
......
package pwc.taxtech.atms.web.service;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpResponse;
......@@ -8,11 +9,20 @@ import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import pwc.taxtech.atms.common.DDUserInfoRes;
import pwc.taxtech.atms.common.HttpUtil;
import pwc.taxtech.atms.web.OrangeHeapConfig;
import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
@Service
......@@ -21,7 +31,23 @@ public class OrangeHeapService {
@Resource
private OrangeHeapConfig systemConfig;
protected final Logger logger = LoggerFactory.getLogger(this.getClass());
private static Logger logger = LoggerFactory.getLogger(OrangeHeapService.class);
@Autowired
HttpServletRequest request;
@Autowired
HttpServletResponse response;
@Value("${get_user_info_url}")
private String getUserInfoUrl;
@Value("${app_id}")
private String appId;
@Value("${app_key}")
private String appKey;
public Optional<String> getTicket(String username) {
CloseableHttpClient httpClient = null;
......@@ -55,78 +81,159 @@ public class OrangeHeapService {
public Optional<String> getTableauTaxCategoryUnreturnedTax() {
Optional<String> optional = Optional.of("admin");
Optional<String> optional = Optional.of(getDDUserName());
return optional.map(s -> String.format(systemConfig.getTableauTaxCategoryUnreturnedTax(), getTicket(s).orElse(StringUtils.EMPTY)));
}
public Optional<String> getTableauDistrictUnreturnedTax() {
Optional<String> optional = Optional.of("admin");
//TODO
Optional<String> optional = Optional.of(getDDUserName());
return optional.map(s -> String.format(systemConfig.getTableauDistrictUnreturnedTax(), getTicket(s).orElse(StringUtils.EMPTY)));
}
public Optional<String> getTableauUnreturnedAndReturnedTax() {
Optional<String> optional = Optional.of("admin");
Optional<String> optional = Optional.of(getDDUserName());
return optional.map(s -> String.format(systemConfig.getTableauUnreturnedAndReturnedTax(), getTicket(s).orElse(StringUtils.EMPTY)));
}
public Optional<String> getTableauCostAnalysis() {
Optional<String> optional = Optional.of("admin");
Optional<String> optional = Optional.of(getDDUserName());
return optional.map(s -> String.format(systemConfig.getTableauCostAnalysis(), getTicket(s).orElse(StringUtils.EMPTY)));
}
public Optional<String> getTableauDistrictProfitAndLoss() {
Optional<String> optional = Optional.of("admin");
Optional<String> optional = Optional.of(getDDUserName());
return optional.map(s -> String.format(systemConfig.getTableauDistrictProfitAndLoss(), getTicket(s).orElse(StringUtils.EMPTY)));
}
public Optional<String> getTableauCompanyProfitAndLoss() {
Optional<String> optional = Optional.of("admin");
Optional<String> optional = Optional.of(getDDUserName());
return optional.map(s -> String.format(systemConfig.getTableauCompanyProfitAndLoss(), getTicket(s).orElse(StringUtils.EMPTY)));
}
public Optional<String> getTableauDriverAndEmployee() {
Optional<String> optional = Optional.of("admin");
Optional<String> optional = Optional.of(getDDUserName());
return optional.map(s -> String.format(systemConfig.getTableauDriverAndEmployee(), getTicket(s).orElse(StringUtils.EMPTY)));
}
public Optional<String> getTableauGmvAndSubsidy() {
Optional<String> optional = Optional.of("admin");
Optional<String> optional = Optional.of(getDDUserName());
return optional.map(s -> String.format(systemConfig.getTableauGmvAndSubsidy(), getTicket(s).orElse(StringUtils.EMPTY)));
}
public Optional<String> getTableauFileArrangement() {
Optional<String> optional = Optional.of("admin");
Optional<String> optional = Optional.of(getDDUserName());
return optional.map(s -> String.format(systemConfig.getTableauFileArrangement(), getTicket(s).orElse(StringUtils.EMPTY)));
}
public Optional<String> getTableauGlobalOverview() {
Optional<String> optional = Optional.of("admin");
Optional<String> optional = Optional.of(getDDUserName());
return optional.map(s -> String.format(systemConfig.getTableauGlobalOverview(), getTicket(s).orElse(StringUtils.EMPTY)));
}
public Optional<String> getTableauGlobalBusiness() {
Optional<String> optional = Optional.of("admin");
Optional<String> optional = Optional.of(getDDUserName());
return optional.map(s -> String.format(systemConfig.getTableauGlobalBusiness(), getTicket(s).orElse(StringUtils.EMPTY)));
}
public Optional<String> getTableauMexicanTax() {
Optional<String> optional = Optional.of("admin");
Optional<String> optional = Optional.of(getDDUserName());
return optional.map(s -> String.format(systemConfig.getTableauMexicanTax(), getTicket(s).orElse(StringUtils.EMPTY)));
}
public Optional<String> getTableauAustralianTax() {
Optional<String> optional = Optional.of("admin");
Optional<String> optional = Optional.of(getDDUserName());
return optional.map(s -> String.format(systemConfig.getTableauAustralianTax(), getTicket(s).orElse(StringUtils.EMPTY)));
}
public Optional<String> getTableauBrazilianTax() {
Optional<String> optional = Optional.of("admin");
Optional<String> optional = Optional.of(getDDUserName());
return optional.map(s -> String.format(systemConfig.getTableauBrazilianTax(), getTicket(s).orElse(StringUtils.EMPTY)));
}
public Optional<String> getTableauOtherTaxAnalysis() {
Optional<String> optional = Optional.of("admin");
Optional<String> optional = Optional.of(getDDUserName());
return optional.map(s -> String.format(systemConfig.getTableauOtherTaxAnalysis(), getTicket(s).orElse(StringUtils.EMPTY)));
}
public String getDDUserName() {
logger.info("进入 获取DD user的方法~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
String ticket = getDDTicketByCookie();
logger.info("ticket=~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"+ticket);
return findUsernameByDDTicket(ticket);
}
//通過cookie 取出ddTicket
public String getDDTicketByCookie() {
Map<String,Cookie> cookieMap = ReadCookieMap();
//TODO 修改token的名字到配置文件,判断ddTicket是否过期
if(cookieMap.containsKey("ddTicket")){
return Optional.ofNullable(cookieMap.get("ddTicket")).map(s->s.getValue()).orElse("");
}else{
logger.info("code=~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"+Optional.ofNullable(cookieMap.get("code")).map(z->z.getValue()).orElse(""));
return getDDTicket(Optional.ofNullable(cookieMap.get("code")).map(z->z.getValue()).orElse(""));
}
}
private String findUsernameByDDTicket(String ticket) {
Assert.hasText(ticket, "empty ticket");
DDUserInfoRes ddUserInfoRes = new DDUserInfoRes();
try {
String response = HttpUtil.post(getUserInfoUrl + "get_user_by_ticket", "ticket=" + ticket + "&app_id=" + appId, "application/x-www-form-urlencoded", "UTF-8", 10000, 10000);
logger.debug(String.format("DD-get_user_by_ticket返回:[%s]", response));
logger.info(String.format("DD-get_user_by_ticket返回:[%s]", response));
ddUserInfoRes = JSONObject.parseObject(response, DDUserInfoRes.class);
} catch (Exception e) {
logger.info(String.format("调用DDSSO获取用户信息失败:[%s]", e.getMessage()), e);
logger.error(String.format("调用DDSSO获取用户信息失败:[%s]", e.getMessage()), e);
}
String username = Optional.ofNullable(ddUserInfoRes)
.map(s -> s.getDdUserInfo())
.map(n -> n.getUsername())
.orElse("");
return username;
}
/**
* 将cookie封装到Map里面
* @return
*/
private Map<String,Cookie> ReadCookieMap(){
Map<String,Cookie> cookieMap = new HashMap<String,Cookie>();
Cookie[] cookies = request.getCookies();
if(null!=cookies){
for(Cookie cookie : cookies){
cookieMap.put(cookie.getName(), cookie);
}
}
return cookieMap;
}
//copy過來的 沒對象
public String getDDTicket(String code) {
logger.info("code=~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"+code);
try{
JSONObject object;
String ddResponse = HttpUtil.post(getUserInfoUrl + "check_code", "code=" + code + "&app_key=" + appKey+ "&app_id=" + appId, "application/x-www-form-urlencoded", "UTF-8", 10000, 10000);
object = JSONObject.parseObject(ddResponse);
Map<String, Object> res = object.getInnerMap();
int errno = (int) res.get("errno");
if (errno != 0) {
logger.warn(String.format("DDTicket get Failed:[%s]", object.toJSONString()));
return null;
}else{
Map<String, String> dataMap = (Map)res.get("data");
Cookie cookie=new Cookie("ddTicket",dataMap.get("ticket"));
response.addCookie(cookie);
return dataMap.get("ticket");
}
}catch (Exception e){
logger.error(String.format("通过code:[%s]获取Ticket失败",code));
}
return null;
}
}
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