| | |
| | | import cn.hutool.http.HttpResponse; |
| | | import cn.hutool.http.HttpUtil; |
| | | import com.alibaba.fastjson2.JSON; |
| | | import com.alibaba.fastjson2.JSONObject; |
| | | import com.xiaopeng.account.service.impl.SysLoginService; |
| | | import com.xiaopeng.account.vo.OIDCAppUserInfoVo; |
| | | import com.xiaopeng.account.vo.OIDCUserInfoVo; |
| | | import com.xiaopeng.account.vo.SSOToken; |
| | | import com.xiaopeng.api.model.account.SysRole; |
| | |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.web.bind.annotation.GetMapping; |
| | | import org.springframework.web.bind.annotation.RequestBody; |
| | | import org.springframework.web.bind.annotation.RequestMapping; |
| | | import org.springframework.web.bind.annotation.RestController; |
| | | |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import java.text.ParseException; |
| | | import java.time.Instant; |
| | | import java.util.Date; |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | |
| | | @Slf4j |
| | | @RestController |
| | | @RequestMapping("/ssoAuthSdk") |
| | | @RequestMapping("") |
| | | public class XtigerSSOController { |
| | | |
| | | @Value("${xiaopeng.xtiger.sso.issuer}") |
| | |
| | | * @param code |
| | | * @param state |
| | | */ |
| | | @GetMapping("/callback") |
| | | public R<?> callback(String code, String state, HttpServletRequest request) { |
| | | @GetMapping("/base/v1/ssoAuthSdk/sysLogin") |
| | | public R<?> sysLogin(String code, String state, HttpServletRequest request) { |
| | | log.info("翼虎登录重定向接口, code: {}, state: {}", code, state); |
| | | HttpRequest post = HttpUtil.createPost(issuer + "/oidc/token/v2"); |
| | | post.form("client_id", appId); |
| | | post.form("code", code); |
| | | post.form("grant_type", "authorization_code"); |
| | | HttpResponse execute = post.execute(); |
| | | if (!execute.isOk()) { |
| | | if(!execute.isOk()){ |
| | | throw new ServiceException("登录失败,请稍后重试。"); |
| | | } |
| | | String body = execute.body(); |
| | |
| | | |
| | | |
| | | /** |
| | | * 翼虎app登录换取本系统token |
| | | * |
| | | *翼虎app登录换取本系统token |
| | | * @param request |
| | | * @return |
| | | */ |
| | | @GetMapping("/appLogin") |
| | | public R<?> appLogin(Integer deviceType, HttpServletRequest request) { |
| | | @GetMapping("/maintenance-app/ssoAuthSdk/appLogin") |
| | | public R<?> appLogin(Integer deviceType, HttpServletRequest request){ |
| | | String accessTokenHeader = request.getHeader("Authorization"); |
| | | log.info("运维APP登录, accessTokenHeader: {}", accessTokenHeader); |
| | | String accessToken = StringUtils.isEmpty(accessTokenHeader) ? null : accessTokenHeader.replace("Bearer ", ""); |
| | | if (StringUtils.isEmpty(accessToken)) { |
| | | if(StringUtils.isEmpty(accessToken)){ |
| | | throw new ServiceException("登录失败,请稍后重试。"); |
| | | } |
| | | OIDCUserInfoVo userInfo = getUserInfo(accessToken); |
| | | OIDCAppUserInfoVo userInfo = getAppUserInfo(request); |
| | | return appLogin(userInfo, deviceType, request); |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | public OIDCUserInfoVo getUserInfo(String accessToken) { |
| | |
| | | HttpRequest post = HttpUtil.createGet(issuer + "/oidc/userinfo"); |
| | | post.header("Authorization", "Bearer " + accessToken); |
| | | HttpResponse execute = post.execute(); |
| | | if (!execute.isOk()) { |
| | | if(!execute.isOk()){ |
| | | throw new ServiceException("登录失败,请稍后重试。"); |
| | | } |
| | | String body = execute.body(); |
| | | log.info("翼虎登录使用token获取用户信息, 返回结果: {}", body); |
| | | OIDCUserInfoVo ssoToken = JSON.parseObject(body, OIDCUserInfoVo.class); |
| | | return ssoToken; |
| | | } |
| | | |
| | | |
| | | public OIDCAppUserInfoVo getAppUserInfo(HttpServletRequest request) { |
| | | log.info("翼虎登录使用token获取用户信息"); |
| | | HttpRequest post = HttpUtil.createGet(issuer + "/xtiger/app/userinfo"); |
| | | post.header("Authorization", "Bearer " + request.getHeader("Authorization")); |
| | | post.header("Content-Type", "application/json"); |
| | | post.header("XT-Client-Type", request.getHeader("XT-Client-Type")); |
| | | post.header("XT-DeviceId", request.getHeader("XT-DeviceId")); |
| | | post.header("XT-AppId", request.getHeader("XT-AppId")); |
| | | post.header("XT-Signature", request.getHeader("XT-Signature")); |
| | | post.header("XT-Nonce", request.getHeader("XT-Nonce")); |
| | | log.info("请求参数:{}", post.headers()); |
| | | HttpResponse execute = post.execute(); |
| | | if(!execute.isOk()){ |
| | | throw new ServiceException("登录失败,请稍后重试。"); |
| | | } |
| | | String body = execute.body(); |
| | | log.info("翼虎登录使用token获取用户信息, 返回结果: {}", body); |
| | | JSONObject jsonObject = JSON.parseObject(body); |
| | | Integer code = jsonObject.getInteger("code"); |
| | | if(null != code && 200 != code){ |
| | | throw new ServiceException(jsonObject.getString("msg")); |
| | | } |
| | | OIDCAppUserInfoVo ssoToken = jsonObject.getObject("data", OIDCAppUserInfoVo.class); |
| | | return ssoToken; |
| | | } |
| | | |
| | |
| | | // throw new ServiceException("请关联角色!", I18nKeyConstant.LOGIN_ROLE_NOT_BIND); |
| | | // } |
| | | // map.put("roleName",roles.get(0).getRoleName()); |
| | | map.put("info", userInfo); |
| | | map.put("info", loginUser); |
| | | // 修改用户最后登录时间 |
| | | return R.ok(map); |
| | | } |
| | | |
| | | |
| | | public R<?> appLogin(OIDCUserInfoVo userInfo, Integer deviceType, HttpServletRequest request) { |
| | | LoginOperationUser loginUser = sysLoginService.verificationCodeLogin(userInfo.getPhone_number(), deviceType, request); |
| | | |
| | | public R<?> appLogin(OIDCAppUserInfoVo userInfo, Integer deviceType, HttpServletRequest request){ |
| | | LoginOperationUser loginUser = sysLoginService.verificationCodeLogin(userInfo.getPhone(), deviceType, request); |
| | | HashMap<String, Object> map = new HashMap<>(); |
| | | map.put("token", tokenService.createOperationUserToken(loginUser)); |
| | | // List<SysRole> roles = userInfo.getXpOperationsUser().getRoles(); |
| | | // if (CollectionUtils.isEmpty(roles)) { |
| | | // return R.fail("请关联角色!"); |
| | | // } |
| | | // map.put("roleName", roles.get(0).getRoleName()); |
| | | map.put("info", loginUser); |
| | | return R.ok(map); |
| | | } |