Commit 42a69e3d by 宋祥

Merge branch 'dev-sx' of http://git.pseer.com/platform/hp-smart into dev

parents 461e726c 6a1981bb
package com.baosight.xservices.xs.service;
import com.baosight.iplat4j.core.FrameworkInfo;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.log.Logger;
import com.baosight.iplat4j.core.log.LoggerFactory;
import com.baosight.iplat4j.core.security.base.SecurityFactory;
import com.baosight.iplat4j.core.security.user.IUserManager;
import com.baosight.iplat4j.core.security.user.User;
import com.baosight.iplat4j.core.service.impl.ServiceBase;
import com.baosight.iplat4j.core.util.StringUtils;
import com.baosight.iplat4j.core.web.threadlocal.UserSession;
import com.baosight.xservices.xs.jwt.JwtTokenValidator;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
/**
* 重写
*
* @author:songx
* @date:2024/4/11,15:28
*/
public class ServiceXSUserJwt extends ServiceBase {
Logger logger = LoggerFactory.getLogger(ServiceXSUserJwt.class);
@Autowired
JwtTokenValidator jwtTokenValidator;
public ServiceXSUserJwt() {
}
public EiInfo generateJwt(EiInfo inInfo) {
String username = inInfo.getString("loginName");
String password = inInfo.getString("password");
String audience = inInfo.getString("audience");
EiInfo outInfo;
try {
if (!StringUtils.isNotEmpty(audience)) {
audience = FrameworkInfo.getProjectEname().toUpperCase();
}
IUserManager um = SecurityFactory.getUserManager();
User user = um.getUser(username);
if (user == null) {
throw new UsernameNotFoundException("Can't find user!");
}
outInfo = um.checkIdentity(user.getLoginName(), password);
if (outInfo.getStatus() > 0) {
Map tokenMap = new HashMap();
tokenMap.put("sub", user.getLoginName());
tokenMap.put("uid", user.getUserId());
Object userCname = user.get("userName");
if (userCname != null) {
tokenMap.put("ucn", userCname.toString());
}
if (!StringUtils.isNotEmpty(audience)) {
audience = FrameworkInfo.getProjectEname().toUpperCase();
}
String token = this.jwtTokenValidator.generateToken(tokenMap, audience);
outInfo.set("token", token);
outInfo.set("x-token", token);
outInfo.getAttr().put("x-token", token);
}
} catch (Exception var11) {
outInfo = new EiInfo();
this.logger.error(var11.getMessage(), var11);
outInfo.setStatus(-1);
outInfo.setMsg(var11.getMessage());
}
return outInfo;
}
public EiInfo validateTest(EiInfo inInfo) {
new HashMap();
inInfo.set("userSessionData", new HashMap(UserSession.getData()));
inInfo.set("loginName", UserSession.getLoginName());
return inInfo;
}
public EiInfo validateTokenTest(EiInfo inInfo) {
String token = inInfo.getString("token");
Map result = this.jwtTokenValidator.validateToken(token);
inInfo.set("result", result);
return inInfo;
}
}
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