登录返回信息补充
parent
5683cf183e
commit
4768358395
|
@ -17,13 +17,12 @@ import lombok.SneakyThrows;
|
|||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.cglib.beans.BeanMap;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
import org.springframework.web.servlet.view.RedirectView;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
@Slf4j
|
||||
@RestController
|
||||
|
@ -38,14 +37,9 @@ public class AuthClientController {
|
|||
@Autowired
|
||||
private UserInfoCache userInfoCache;
|
||||
|
||||
// 进入首页
|
||||
@RequestMapping("/")
|
||||
public Object index(HttpServletRequest request) {
|
||||
request.setAttribute("uid", StpUtil.getLoginIdDefaultNull());
|
||||
return new ModelAndView("index.html");
|
||||
}
|
||||
|
||||
// 根据Code码进行登录,获取 Access-Token 和 openid
|
||||
/**
|
||||
* 根据Code码进行登录,获取 Access-Token 和 用户信息
|
||||
*/
|
||||
@RequestMapping("/codeLogin")
|
||||
public SaResult codeLogin(String code, String clientId) {
|
||||
OauthClient oauthClient = oauthClientCache.getClient(clientId);
|
||||
|
@ -71,19 +65,20 @@ public class AuthClientController {
|
|||
}
|
||||
|
||||
// 根据openid获取其对应的userId
|
||||
SoMap data = new SoMap();
|
||||
String uid = getUserIdByOpenid(so.getString("openid"));
|
||||
String access_token = so.getString("access_token");
|
||||
UserInfo userInfo = userInfoCache.getUserInfo(uid);
|
||||
data.put("name", userInfo.getNickName());
|
||||
data.put("uid", uid);
|
||||
data.put("access_token", access_token);
|
||||
UserInfoVo userVo = getUserInfo(uid);
|
||||
BeanMap beanMap = BeanMap.create(userVo);
|
||||
beanMap.put("access_token", access_token);
|
||||
|
||||
// 返回相关参数
|
||||
StpUtil.login(uid, SaLoginConfig.setToken(access_token));
|
||||
return SaResult.data(data);
|
||||
return SaResult.data(beanMap);
|
||||
}
|
||||
|
||||
// 注销登录
|
||||
/**
|
||||
* 注销登录
|
||||
*/
|
||||
@RequestMapping("/logout")
|
||||
public RedirectView logout(String accessToken, String redirect_uri) {
|
||||
//先注销client中cookie的token
|
||||
|
@ -94,13 +89,14 @@ public class AuthClientController {
|
|||
return new RedirectView(redirect_uri);
|
||||
}
|
||||
|
||||
/**
|
||||
* 登录验证
|
||||
*/
|
||||
@GetMapping("/checkLogin")
|
||||
public SaResult checkLogin() {
|
||||
try {
|
||||
String uid = StpUtil.getLoginId().toString();
|
||||
UserInfo userInfo = userInfoCache.getUserInfo(uid);
|
||||
UserInfoVo userVo = new UserInfoVo();
|
||||
ReflectUtil.copyNoNulls(userInfo, userVo);
|
||||
UserInfoVo userVo = getUserInfo(uid);
|
||||
return SaResult.ok().setData(userVo);
|
||||
} catch (Throwable e) {
|
||||
return SaResult.error("no login");
|
||||
|
@ -113,4 +109,11 @@ public class AuthClientController {
|
|||
return clientIdLoginId.split(":")[1];
|
||||
}
|
||||
|
||||
private UserInfoVo getUserInfo(String uid) {
|
||||
UserInfo userInfo = userInfoCache.getUserInfo(uid);
|
||||
UserInfoVo userVo = new UserInfoVo();
|
||||
ReflectUtil.copyNoNulls(userInfo, userVo);
|
||||
return userVo;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -5,9 +5,7 @@ import cc.iotkit.dao.UserInfoRepository;
|
|||
import cc.iotkit.model.UserInfo;
|
||||
import cc.iotkit.oauth.service.TokenRequestHandler;
|
||||
import cc.iotkit.utils.AuthUtil;
|
||||
import cn.dev33.satoken.context.SaHolder;
|
||||
import cn.dev33.satoken.oauth2.config.SaOAuth2Config;
|
||||
import cn.dev33.satoken.oauth2.logic.SaOAuth2Util;
|
||||
import cn.dev33.satoken.stp.StpUtil;
|
||||
import cn.dev33.satoken.util.SaResult;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
@ -19,7 +17,6 @@ import org.springframework.web.servlet.ModelAndView;
|
|||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@Slf4j
|
||||
|
@ -29,7 +26,9 @@ public class AuthServerController {
|
|||
@Autowired
|
||||
private UserInfoRepository userInfoRepository;
|
||||
|
||||
// 处理所有OAuth相关请求
|
||||
/**
|
||||
* 处理所有OAuth相关请求
|
||||
*/
|
||||
@RequestMapping("/oauth2/*")
|
||||
public Object request(HttpServletRequest request) {
|
||||
Object result = TokenRequestHandler.serverRequest();
|
||||
|
@ -37,7 +36,9 @@ public class AuthServerController {
|
|||
return result;
|
||||
}
|
||||
|
||||
// Sa-OAuth2 定制化配置
|
||||
/**
|
||||
* Sa-OAuth2 自定义配置
|
||||
*/
|
||||
@Autowired
|
||||
public void setSaOAuth2Config(SaOAuth2Config cfg) {
|
||||
cfg.
|
||||
|
@ -67,6 +68,11 @@ public class AuthServerController {
|
|||
return new ModelAndView("confirm.html", map);
|
||||
})
|
||||
;
|
||||
|
||||
//开启密码授权、刷新token和client授权模式
|
||||
cfg.setIsPassword(true);
|
||||
cfg.setIsNewRefresh(true);
|
||||
cfg.setIsClient(true);
|
||||
}
|
||||
|
||||
// 全局异常拦截
|
||||
|
@ -76,28 +82,9 @@ public class AuthServerController {
|
|||
return SaResult.error(e.getMessage());
|
||||
}
|
||||
|
||||
// ---------- 开放相关资源接口: Client端根据 Access-Token ,置换相关资源 ------------
|
||||
|
||||
// 获取Userinfo信息:昵称、头像、性别等等
|
||||
@RequestMapping("/oauth2/userinfo")
|
||||
public SaResult userinfo() {
|
||||
// 获取 Access-Token 对应的账号id
|
||||
String accessToken = SaHolder.getRequest().getParamNotNull("access_token");
|
||||
Object loginId = SaOAuth2Util.getLoginIdByAccessToken(accessToken);
|
||||
System.out.println("-------- 此Access-Token对应的账号id: " + loginId);
|
||||
|
||||
// 校验 Access-Token 是否具有权限: userinfo
|
||||
SaOAuth2Util.checkScope(accessToken, "userinfo");
|
||||
|
||||
// 模拟账号信息 (真实环境需要查询数据库获取信息)
|
||||
Map<String, Object> map = new LinkedHashMap<>();
|
||||
map.put("nickname", "shengzhang_");
|
||||
map.put("avatar", "http://xxx.com/1.jpg");
|
||||
map.put("age", "18");
|
||||
map.put("sex", "男");
|
||||
map.put("address", "山东省 青岛市 城阳区");
|
||||
return SaResult.data(map);
|
||||
return SaResult.ok();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -40,6 +40,7 @@ public class AuthVerticle extends AbstractVerticle {
|
|||
//第二步 配置Router解析url
|
||||
backendRouter.route(HttpMethod.POST, "/mqtt/auth").handler(rc -> {
|
||||
String json = rc.getBodyAsString();
|
||||
log.info("mqtt auth:{}", json);
|
||||
try {
|
||||
Map<String, Object> head = new HashMap<>();
|
||||
head.put("topic", "/mqtt/auth");
|
||||
|
@ -54,6 +55,7 @@ public class AuthVerticle extends AbstractVerticle {
|
|||
});
|
||||
backendRouter.route(HttpMethod.POST, "/mqtt/acl").handler(rc -> {
|
||||
String json = rc.getBodyAsString();
|
||||
log.info("mqtt acl:{}", json);
|
||||
try {
|
||||
Map<String, Object> head = new HashMap<>();
|
||||
head.put("topic", "/mqtt/acl");
|
||||
|
|
Loading…
Reference in New Issue