|
|
@@ -1,6 +1,7 @@
|
|
|
package cn.reghao.devops.manager.account.security.handler;
|
|
|
|
|
|
import cn.reghao.jutil.jdk.result.WebResult;
|
|
|
+import cn.reghao.jutil.web.ServletUtil;
|
|
|
import org.springframework.security.core.Authentication;
|
|
|
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
|
|
|
import org.springframework.security.web.savedrequest.SavedRequest;
|
|
|
@@ -22,34 +23,33 @@ public class AuthSuccessHandlerImpl implements AuthenticationSuccessHandler {
|
|
|
@Override
|
|
|
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication auth)
|
|
|
throws IOException {
|
|
|
- sendResponse(response);
|
|
|
- //redirect(request, response);
|
|
|
- }
|
|
|
-
|
|
|
- private void sendResponse(HttpServletResponse response) throws IOException {
|
|
|
- response.setContentType("text/html;charset=utf-8");
|
|
|
- String result = WebResult.success();
|
|
|
- PrintWriter pt = response.getWriter();
|
|
|
- pt.println(result);
|
|
|
+ String redirectPath = getRedirectPath();
|
|
|
+ String body = WebResult.success(redirectPath);
|
|
|
+ writeResponse(response, body);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * TODO 前端无法处理重定向
|
|
|
+ * 认证成功后, 重定向到登录前的地址, 需要 session 的支持
|
|
|
*
|
|
|
* @param
|
|
|
* @return
|
|
|
- * @date 2021-07-27 下午3:54
|
|
|
+ * @date 2023-08-16 10:58:41
|
|
|
*/
|
|
|
- private void redirect(HttpServletRequest request, HttpServletResponse response) throws IOException {
|
|
|
+ private String getRedirectPath() {
|
|
|
+ String redirectPath = "/";
|
|
|
+ // 获取 spring security 在 session 中存放的变量
|
|
|
SavedRequest savedRequest =
|
|
|
- (SavedRequest) request.getSession().getAttribute("SPRING_SECURITY_SAVED_REQUEST");
|
|
|
+ (SavedRequest) ServletUtil.getSession().getAttribute("SPRING_SECURITY_SAVED_REQUEST");
|
|
|
if (savedRequest != null) {
|
|
|
- String redirectUrl = savedRequest.getRedirectUrl();
|
|
|
- // 跳转到登录前的地址
|
|
|
- response.sendRedirect(redirectUrl);
|
|
|
- } else {
|
|
|
- // 跳转到首页
|
|
|
- response.sendRedirect("/");
|
|
|
+ redirectPath = savedRequest.getRedirectUrl();
|
|
|
}
|
|
|
+
|
|
|
+ return redirectPath;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void writeResponse(HttpServletResponse response, String body) throws IOException {
|
|
|
+ response.setContentType("application/json; charset=utf-8");
|
|
|
+ PrintWriter printWriter = response.getWriter();
|
|
|
+ printWriter.write(body);
|
|
|
}
|
|
|
}
|