mirror of
https://gitee.com/dromara/MaxKey.git
synced 2026-05-18 14:28:08 +08:00
Authentication Forward Fix
Authentication Forward Fix
This commit is contained in:
@@ -1,5 +1,10 @@
|
||||
package org.maxkey.web.endpoint;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import org.maxkey.config.ApplicationConfig;
|
||||
import org.maxkey.constants.ConstantsPasswordSetType;
|
||||
import org.maxkey.domain.UserInfo;
|
||||
@@ -25,9 +30,10 @@ public class IndexEndpoint {
|
||||
@Autowired
|
||||
@Qualifier("applicationConfig")
|
||||
ApplicationConfig applicationConfig;
|
||||
|
||||
@RequestMapping(value={"/forwardindex"})
|
||||
public ModelAndView forwardindex() {
|
||||
public ModelAndView forwardindex(HttpServletRequest request,
|
||||
HttpServletResponse response) throws ServletException, IOException {
|
||||
|
||||
_logger.debug("IndexEndpoint /forwardindex.");
|
||||
ModelAndView modelAndView=new ModelAndView();
|
||||
Integer passwordSetType=(Integer)WebContext.getSession().getAttribute(WebConstants.CURRENT_LOGIN_USER_PASSWORD_SET_TYPE);
|
||||
@@ -54,11 +60,14 @@ public class IndexEndpoint {
|
||||
return modelAndView;
|
||||
}
|
||||
|
||||
|
||||
|
||||
return new ModelAndView("index");
|
||||
}
|
||||
|
||||
@RequestMapping(value={"/index"})
|
||||
public ModelAndView home() {
|
||||
public ModelAndView home(HttpServletRequest request,
|
||||
HttpServletResponse response) throws ServletException, IOException {
|
||||
_logger.debug("IndexEndpoint /index.");
|
||||
|
||||
if(applicationConfig.getLoginConfig().getDefaultUri()!=null&&
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
package org.maxkey.web.endpoint;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
@@ -22,9 +24,6 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.security.web.savedrequest.HttpSessionRequestCache;
|
||||
import org.springframework.security.web.savedrequest.RequestCache;
|
||||
import org.springframework.security.web.savedrequest.SavedRequest;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.CookieValue;
|
||||
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||
@@ -148,18 +147,7 @@ public class LoginEndpoint {
|
||||
modelAndView.addObject("ssopList", socialSignOnProviderService.getSocialSignOnProviders());
|
||||
}
|
||||
}
|
||||
//save first protected url
|
||||
SavedRequest firstSavedRequest = (SavedRequest)WebContext.getAttribute(WebConstants.FIRST_SAVED_REQUEST_PARAMETER);
|
||||
if(firstSavedRequest==null){
|
||||
RequestCache requestCache = new HttpSessionRequestCache();
|
||||
SavedRequest savedRequest =requestCache.getRequest(request, response);
|
||||
if(savedRequest!=null){
|
||||
_logger.debug("first request parameter "+savedRequest.getRedirectUrl());
|
||||
WebContext.setAttribute(WebConstants.FIRST_SAVED_REQUEST_PARAMETER, savedRequest);
|
||||
}
|
||||
}else {
|
||||
WebContext.setAttribute(WebConstants.SPRING_PROCESS_SAVED_REQUEST, firstSavedRequest);
|
||||
}
|
||||
|
||||
|
||||
if(isAuthenticated){
|
||||
return WebContext.redirect("/forwardindex");
|
||||
@@ -169,12 +157,15 @@ public class LoginEndpoint {
|
||||
}
|
||||
|
||||
@RequestMapping(value={"/logon.do"})
|
||||
public ModelAndView logon(@ModelAttribute("authentication") BasicAuthentication authentication) {
|
||||
public ModelAndView logon(
|
||||
HttpServletRequest request,
|
||||
HttpServletResponse response,
|
||||
@ModelAttribute("authentication") BasicAuthentication authentication) throws ServletException, IOException {
|
||||
|
||||
authenticationProvider.authenticate(authentication);
|
||||
|
||||
if(WebContext.isAuthenticated()){
|
||||
return WebContext.redirect("/forwardindex");
|
||||
return WebContext.redirect("/forwardindex");
|
||||
}else{
|
||||
return WebContext.redirect("/login");
|
||||
}
|
||||
|
||||
@@ -4,12 +4,18 @@ import java.util.concurrent.ConcurrentHashMap;
|
||||
import javax.servlet.RequestDispatcher;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.maxkey.authn.SavedRequestAwareAuthenticationSuccessHandler;
|
||||
import org.maxkey.config.ApplicationConfig;
|
||||
import org.maxkey.web.WebConstants;
|
||||
import org.maxkey.web.WebContext;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.security.web.savedrequest.HttpSessionRequestCache;
|
||||
import org.springframework.security.web.savedrequest.RequestCache;
|
||||
import org.springframework.security.web.savedrequest.SavedRequest;
|
||||
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
|
||||
|
||||
/**
|
||||
@@ -26,6 +32,11 @@ public class PermissionAdapter extends HandlerInterceptorAdapter {
|
||||
@Qualifier("applicationConfig")
|
||||
private ApplicationConfig applicationConfig;
|
||||
|
||||
|
||||
@Autowired
|
||||
@Qualifier("savedRequestSuccessHandler")
|
||||
SavedRequestAwareAuthenticationSuccessHandler savedRequestSuccessHandler;
|
||||
|
||||
static ConcurrentHashMap<String, String> navigationsMap = null;
|
||||
|
||||
/*
|
||||
@@ -41,14 +52,34 @@ public class PermissionAdapter extends HandlerInterceptorAdapter {
|
||||
HttpServletResponse response, Object handler)
|
||||
throws Exception {
|
||||
_logger.trace("PermissionAdapter preHandle");
|
||||
//save first protected url
|
||||
SavedRequest firstSavedRequest = (SavedRequest)WebContext.getAttribute(WebConstants.FIRST_SAVED_REQUEST_PARAMETER);
|
||||
// 判断用户是否登录, 判断用户和角色,判断用户是否登录用户
|
||||
if (WebContext.getAuthentication() == null
|
||||
|| WebContext.getAuthentication().getAuthorities() == null) {
|
||||
//保存未认证的请求信息
|
||||
if(firstSavedRequest==null){
|
||||
RequestCache requestCache = new HttpSessionRequestCache();
|
||||
requestCache.saveRequest(request, response);
|
||||
SavedRequest savedRequest =requestCache.getRequest(request, response);
|
||||
if(savedRequest!=null){
|
||||
_logger.debug("first request parameter savedRequest "+savedRequest.getRedirectUrl());
|
||||
WebContext.setAttribute(WebConstants.FIRST_SAVED_REQUEST_PARAMETER, savedRequest);
|
||||
savedRequestSuccessHandler.setRequestCache(requestCache);
|
||||
}
|
||||
}
|
||||
|
||||
_logger.trace("No Authentication ... forward to /login");
|
||||
RequestDispatcher dispatcher = request.getRequestDispatcher("/login");
|
||||
dispatcher.forward(request, response);
|
||||
return false;
|
||||
}
|
||||
|
||||
//认证完成,跳转到未认证请求
|
||||
if(firstSavedRequest!=null) {
|
||||
savedRequestSuccessHandler.onAuthenticationSuccess(request, response, WebContext.getAuthentication());
|
||||
WebContext.removeAttribute(WebConstants.FIRST_SAVED_REQUEST_PARAMETER);
|
||||
}
|
||||
|
||||
boolean hasAccess = true;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user