| | |
| | | import com.xiaopeng.api.model.account.SysRole; |
| | | import com.xiaopeng.api.vo.account.LoginOperationUser; |
| | | import com.xiaopeng.api.vo.account.LoginUser; |
| | | import com.xiaopeng.common.core.constant.CacheConstants; |
| | | import com.xiaopeng.common.core.domain.R; |
| | | import com.xiaopeng.common.core.exception.ServiceException; |
| | | import com.xiaopeng.common.core.utils.JwtUtils; |
| | | import com.xiaopeng.common.redis.service.RedisService; |
| | | import com.xiaopeng.common.security.service.TokenService; |
| | | import io.jsonwebtoken.Claims; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.commons.lang3.StringUtils; |
| | | 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 org.springframework.web.bind.annotation.*; |
| | | |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import java.text.ParseException; |
| | |
| | | import java.util.Date; |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | |
| | | @Slf4j |
| | | @RestController |
| | |
| | | @Value("${xiaopeng.xtiger.sso.appId}") |
| | | private String appId; |
| | | |
| | | @Value("${ali.push.key:335666352}") |
| | | private String aliPushKey; |
| | | |
| | | @Value("${ali.push.secret:0252ebde2dc84f63aebd41f757f213d7}") |
| | | private String aliPushSecret; |
| | | |
| | | @Autowired |
| | | private TokenService tokenService; |
| | | |
| | |
| | | |
| | | @Autowired |
| | | private ISysGroupService sysGroupService; |
| | | |
| | | @Autowired |
| | | private RedisService redisService; |
| | | |
| | | /** |
| | | * 翼虎web登录重定向接口 |
| | |
| | | */ |
| | | @GetMapping("/maintenance-app/ssoAuthSdk/appLogin") |
| | | public R<?> appLogin(Integer deviceType, HttpServletRequest request){ |
| | | String accessTokenHeader = request.getHeader("Authorization"); |
| | | String accessTokenHeader = request.getHeader("XP-TOKEN"); |
| | | log.info("运维APP登录, accessTokenHeader: {}", accessTokenHeader); |
| | | String accessToken = StringUtils.isEmpty(accessTokenHeader) ? null : accessTokenHeader.replace("Bearer ", ""); |
| | | if(StringUtils.isEmpty(accessToken)){ |
| | | throw new ServiceException("登录失败,请稍后重试。"); |
| | | } |
| | | OIDCAppUserInfoVo userInfo = getAppUserInfo(request); |
| | | OIDCAppUserInfoVo userInfo = getAppUserInfo(request, accessToken); |
| | | return appLogin(userInfo, deviceType, request); |
| | | } |
| | | |
| | | |
| | | /** |
| | | *翼虎app登录换取本系统token |
| | | * @return |
| | | */ |
| | | @PostMapping("/maintenance-app/ssoAuthSdk/appLoginOut") |
| | | public R<?> appLoginOut(){ |
| | | LoginOperationUser loginOperationUser = tokenService.getLoginOperationUser(); |
| | | if(null == loginOperationUser){ |
| | | throw new ServiceException("操作失败,请稍后重试。"); |
| | | } |
| | | String token = loginOperationUser.getToken(); |
| | | redisService.deleteObject(CacheConstants.LOGIN_TOKEN_KEY + token); |
| | | return R.ok(); |
| | | } |
| | | |
| | | |
| | | |
| | |
| | | } |
| | | |
| | | |
| | | public OIDCAppUserInfoVo getAppUserInfo(HttpServletRequest request) { |
| | | log.info("翼虎登录使用token获取用户信息"); |
| | | public OIDCAppUserInfoVo getAppUserInfo(HttpServletRequest request, String accessToken) { |
| | | log.info("APP翼虎登录使用token获取用户信息"); |
| | | HttpRequest post = HttpUtil.createGet(issuer + "/xtiger/app/userinfo"); |
| | | post.header("Authorization", "Bearer " + request.getHeader("Authorization")); |
| | | post.header("Authorization", "Bearer " + accessToken); |
| | | 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")); |
| | | post.header("Client-Type", "PC"); |
| | | log.info("请求参数:{}", post.headers()); |
| | | HttpResponse execute = post.execute(); |
| | | if(!execute.isOk()){ |
| | | throw new ServiceException("登录失败,请稍后重试。"); |
| | | } |
| | | String body = execute.body(); |
| | | log.info("翼虎登录使用token获取用户信息, 返回结果: {}", body); |
| | | log.info("APP翼虎登录使用token获取用户信息, 返回结果: {}", body); |
| | | JSONObject jsonObject = JSON.parseObject(body); |
| | | Integer code = jsonObject.getInteger("code"); |
| | | if(null != code && 200 != code){ |
| | |
| | | loginUser.setPhoneDesensitizationExempt(phoneDesensitization); |
| | | HashMap<String, Object> map = new HashMap<>(); |
| | | map.put("token", tokenService.createToken(loginUser)); |
| | | List<SysRole> roles = loginUser.getSysUser().getRoles(); |
| | | // if (CollectionUtils.isEmpty(roles)) { |
| | | // throw new ServiceException("请关联角色!", I18nKeyConstant.LOGIN_ROLE_NOT_BIND); |
| | | // } |
| | | // map.put("roleName",roles.get(0).getRoleName()); |
| | | map.put("info", loginUser); |
| | | // 修改用户最后登录时间 |
| | | return R.ok(map); |
| | |
| | | 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); |
| | | Map<String, String> thirdInfo = new HashMap<>(); |
| | | thirdInfo.put("pushKey", aliPushKey); |
| | | thirdInfo.put("pushSecret", aliPushSecret); |
| | | map.put("thirdInfo", thirdInfo); |
| | | return R.ok(map); |
| | | } |
| | | } |