Commit c8b9c054 authored by 杨翌文's avatar 杨翌文

feature: 支持单点登录

parent 08403e1a
...@@ -2,6 +2,7 @@ package cn.breeze.elleai.controller.front; ...@@ -2,6 +2,7 @@ package cn.breeze.elleai.controller.front;
import cn.breeze.elleai.application.dto.ApiResponse; import cn.breeze.elleai.application.dto.ApiResponse;
import cn.breeze.elleai.util.UserPrincipal; import cn.breeze.elleai.util.UserPrincipal;
import cn.hutool.core.exceptions.ExceptionUtil;
import cn.hutool.core.util.ArrayUtil; import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
...@@ -11,39 +12,48 @@ import jakarta.servlet.http.Cookie; ...@@ -11,39 +12,48 @@ import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse; import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j;
import org.apache.hadoop.yarn.webapp.hamlet2.Hamlet;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.ResponseBody;
@RestController import java.io.IOException;
@Controller
@RequestMapping(value = "/front/user") @RequestMapping(value = "/front/user")
@Tag(name = "移动端-用户&授权") @Tag(name = "移动端-用户&授权")
@RequiredArgsConstructor @RequiredArgsConstructor
@Slf4j
public class UserMobileController { public class UserMobileController {
@Value("${sso.user.mock:true}") @Value("${sso.user.mock:true}")
private String enableMock; private String enableMock;
@SneakyThrows
@Operation(summary = "单点登录") @Operation(summary = "单点登录")
@GetMapping("/sso") @GetMapping("/sso")
public void userSSOLogin(@RequestParam("jwt") String token, HttpServletResponse response) { public void userSSOLogin(@RequestParam("jwt") String token, HttpServletResponse response) {
if (StrUtil.isNotBlank(token)) { if (StrUtil.isNotBlank(token)) {
log.info("单点登录:{}", token);
try {
Cookie jwt = new Cookie("jwt", token); Cookie jwt = new Cookie("jwt", token);
jwt.setPath("/"); jwt.setPath("/");
jwt.setHttpOnly(true);
response.addCookie(jwt); response.addCookie(jwt);
response.sendRedirect("/elle-coaching/1.0.0/index.html"); response.sendRedirect("/elle-coaching/1.0.0/index.html");
} catch (IOException e) {
log.error("单点登录异常{}", ExceptionUtil.getMessage(e));
}
} }
} }
@Operation(summary = "获取token") @Operation(summary = "获取token")
@GetMapping("/get_token") @GetMapping("/get_token")
public ApiResponse<String> getToken(HttpServletRequest request, HttpServletResponse response) { public @ResponseBody ApiResponse<String> getToken(HttpServletRequest request, HttpServletResponse response) {
Cookie[] cookies = request.getCookies(); Cookie[] cookies = request.getCookies();
if (ArrayUtil.isNotEmpty(cookies)) { if (ArrayUtil.isNotEmpty(cookies)) {
Cookie cookie = ArrayUtil.firstMatch(c -> StrUtil.equals(c.getName(), "jwt"), cookies); Cookie cookie = ArrayUtil.firstMatch(c -> StrUtil.equals(c.getName(), "jwt"), cookies);
...@@ -62,7 +72,7 @@ public class UserMobileController { ...@@ -62,7 +72,7 @@ public class UserMobileController {
@Operation(summary = "获取用户信息") @Operation(summary = "获取用户信息")
@GetMapping("/info") @GetMapping("/info")
public ApiResponse<UserPrincipal> getUserInfo(UserPrincipal userPrincipal) { public @ResponseBody ApiResponse<UserPrincipal> getUserInfo(UserPrincipal userPrincipal) {
return ApiResponse.ok(userPrincipal); return ApiResponse.ok(userPrincipal);
} }
......
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