RuoYi-Vue-Plus/ruoyi-modules/ruoyi-sso-server/src/main/java/com/pj/controller/SsoServerController.java
2024-11-03 23:24:44 +08:00

87 lines
3.4 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.pj.controller;
import cn.dev33.satoken.context.SaHolder;
import cn.dev33.satoken.context.model.SaRequest;
import cn.dev33.satoken.sso.config.SaSsoServerConfig;
import cn.dev33.satoken.sso.processor.SaSsoServerProcessor;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import cn.hutool.json.JSONUtil;
import com.pj.model.vo.LoginVo;
import com.pj.model.vo.SysClientVo;
import com.pj.service.IAuthStrategy;
import org.dromara.common.core.domain.model.PasswordLoginBody;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
/**
* Sa-Token-SSO Server端 Controller
*
* @author click33
*/
@RestController
public class SsoServerController {
/**
* SSO-Server端处理所有SSO相关请求
* http://{host}:{port}/sso/auth -- 单点登录授权地址接受参数redirect=授权重定向地址
* http://{host}:{port}/sso/doLogin -- 账号密码登录接口接受参数name、pwd
* http://{host}:{port}/sso/checkTicket -- Ticket校验接口isHttp=true时打开接受参数ticket=ticket码、ssoLogoutCall=单点注销回调地址 [可选]
* http://{host}:{port}/sso/signout -- 单点注销地址isSlo=true时打开接受参数loginId=账号id、sign=参数签名
*/
@RequestMapping("/sso/*")
public Object ssoRequest() {
// 如果登录时没有提供redirect参数则进入平台中心首页 /home而不是重定向到 client 端
SaRequest req = SaHolder.getRequest();
if (req.isPath("/sso/auth") && !req.hasParam("redirect") && StpUtil.isLogin()) {
return new ModelAndView("index.html");
}
return SaSsoServerProcessor.instance.dister();
}
// 配置SSO相关参数
@Autowired
private void configSso(SaSsoServerConfig ssoServer) {
// 配置未登录时返回的View
ssoServer.notLoginView = () -> {
return new ModelAndView("sa-login.html");
};
// 配置:登录处理函数
ssoServer.doLoginHandle = (name, pwd) -> {
// 此处仅做模拟登录,真实环境应该查询数据进行登录
if ("sa".equals(name) && "123456".equals(pwd)) {
PasswordLoginBody body = new PasswordLoginBody();
body.setUsername("sa");
body.setPassword("123456");
body.setClientId("ClientId");
body.setGrantType("password");
body.setTenantId("1");
body.setCode("code1");
body.setUuid("qweqw");
SysClientVo client = new SysClientVo();
client.setId(1L);
client.setClientId("ClientId1");
client.setClientKey("setClientKey");
client.setClientSecret("setClientSecret");
client.setGrantType("password");
client.setDeviceType("pc");
client.setActiveTimeout(1800L);
client.setTimeout(300L);
client.setStatus("0");
LoginVo loginVo = IAuthStrategy.login(JSONUtil.toJsonStr(body), client, body.getGrantType());
return SaResult.ok("登录成功!").setData(StpUtil.getTokenValue());
}
return SaResult.error("登录失败!");
};
}
}