|
@@ -1,83 +1,46 @@
|
|
|
package com.uas.sso.sso.backend.config;
|
|
package com.uas.sso.sso.backend.config;
|
|
|
|
|
|
|
|
-import com.uas.sso.SSOConfig;
|
|
|
|
|
-import com.uas.sso.SSOHelper;
|
|
|
|
|
-import com.uas.sso.SSOToken;
|
|
|
|
|
-import com.uas.sso.entity.UserAccount;
|
|
|
|
|
-import com.uas.sso.support.SystemSession;
|
|
|
|
|
-import com.uas.sso.util.FastjsonUtils;
|
|
|
|
|
-import com.uas.sso.web.spring.AbstractSSOInterceptor;
|
|
|
|
|
-import java.io.IOException;
|
|
|
|
|
|
|
+import com.uas.sso.sso.backend.entity.Admin;
|
|
|
|
|
+import com.uas.sso.sso.backend.support.AccountSession;
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
|
+import javax.servlet.http.HttpSession;
|
|
|
|
|
+import org.springframework.http.HttpStatus;
|
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.stereotype.Component;
|
|
|
-import org.springframework.util.StringUtils;
|
|
|
|
|
|
|
+import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 登录拦截
|
|
* 登录拦截
|
|
|
*
|
|
*
|
|
|
- * @author wangmh
|
|
|
|
|
- * @date 2018/1/9.
|
|
|
|
|
|
|
+ * @author huxz
|
|
|
*/
|
|
*/
|
|
|
@Component
|
|
@Component
|
|
|
-public class SecurityInterceptor extends AbstractSSOInterceptor {
|
|
|
|
|
|
|
+public class SecurityInterceptor extends HandlerInterceptorAdapter {
|
|
|
|
|
|
|
|
- @Override
|
|
|
|
|
- protected boolean onAuthenticateFailed(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
|
|
|
|
|
- return false;
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
|
|
|
|
|
+ HttpSession session = request.getSession();
|
|
|
|
|
|
|
|
- @Override
|
|
|
|
|
- protected void onAuthenticateSuccess(HttpServletRequest request, HttpServletResponse response) {
|
|
|
|
|
- SSOToken token = SSOHelper.attrToken(request);
|
|
|
|
|
- UserAccount userAccount = getUserByToken(token);
|
|
|
|
|
|
|
+ boolean flag = false;
|
|
|
|
|
+ if (session != null && session.getAttribute("account") != null) {
|
|
|
|
|
+ Admin account = (Admin) session.getAttribute("account");
|
|
|
|
|
|
|
|
- if (userAccount != null) {
|
|
|
|
|
- request.getSession().setAttribute("userAccount", userAccount);
|
|
|
|
|
- SystemSession.setUserAccount(userAccount);
|
|
|
|
|
- } else {
|
|
|
|
|
- try {
|
|
|
|
|
- sendRedirect(request, response);
|
|
|
|
|
- } catch (IOException e) {
|
|
|
|
|
- e.printStackTrace();
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ if (account != null) {
|
|
|
|
|
+ flag = true;
|
|
|
|
|
+ AccountSession.setUserAccount(account);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- @Override
|
|
|
|
|
- public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
|
|
|
|
|
- throws Exception {
|
|
|
|
|
- super.afterCompletion(request, response, handler, ex);
|
|
|
|
|
- SystemSession.clear();
|
|
|
|
|
|
|
+ if (!flag) {
|
|
|
|
|
+ response.sendRedirect("/login");
|
|
|
|
|
+ // response.sendError(HttpStatus.UNAUTHORIZED.value(), "请您进行登录认证");
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- @Override
|
|
|
|
|
- protected void sendRedirect(HttpServletRequest request, HttpServletResponse response) throws IOException {
|
|
|
|
|
- System.out.println(request.getRequestURL().toString());
|
|
|
|
|
- boolean cross = SSOHelper.isCrossDomain(request);
|
|
|
|
|
- if (cross) {
|
|
|
|
|
- request.getSession().setAttribute(SSOConfig.SSOReferer, request.getRequestURL());
|
|
|
|
|
-
|
|
|
|
|
- String returnUrl = request.getRequestURL().toString();
|
|
|
|
|
- String baseUrl = returnUrl.replace(request.getRequestURI(), "");
|
|
|
|
|
- if (StringUtils.isEmpty(returnUrl)) {
|
|
|
|
|
- returnUrl = SSOHelper.getRedirectRefererLoginUrl(request);
|
|
|
|
|
- } else {
|
|
|
|
|
- returnUrl = SSOHelper.getRedirectLoginUrl(request, returnUrl);
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ return flag;
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- baseUrl = baseUrl + "/login/other";
|
|
|
|
|
- response.sendRedirect(returnUrl + "&baseUrl=" + baseUrl);
|
|
|
|
|
- } else {
|
|
|
|
|
- SSOHelper.clearRedirectLogin(request, response);
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- private UserAccount getUserByToken(SSOToken token) {
|
|
|
|
|
- UserAccount userAccount = null;
|
|
|
|
|
- if (token != null && !StringUtils.isEmpty(token.getData())) {
|
|
|
|
|
- userAccount = FastjsonUtils.fromJson(token.getData(), UserAccount.class);
|
|
|
|
|
- }
|
|
|
|
|
- return userAccount;
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
|
|
|
|
|
+ AccountSession.clear();
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|