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("登录失败!"); }; } }