Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

非spring boot项目整合SSO #66

Open
zhaojieip opened this issue Mar 22, 2021 · 0 comments
Open

非spring boot项目整合SSO #66

zhaojieip opened this issue Mar 22, 2021 · 0 comments

Comments

@zhaojieip
Copy link

zhaojieip commented Mar 22, 2021

1.将XxlSsoWebFilter.java 放入项目中
`package com.xxl.sso.core.filter;

import com.xxl.sso.core.conf.Conf;
import com.xxl.sso.core.login.SsoWebLoginHelper;
import com.xxl.sso.core.path.impl.AntPathMatcher;
import com.xxl.sso.core.user.XxlSsoUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.*;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**

  • web sso filter

  • @author xuxueli 2018-04-03
    */
    public class XxlSsoWebFilter extends HttpServlet implements Filter {
    private static Logger logger = LoggerFactory.getLogger(XxlSsoWebFilter.class);

    private static final AntPathMatcher antPathMatcher = new AntPathMatcher();

    private String ssoServer;
    private String logoutPath;
    private String excludedPaths;

    @OverRide
    public void init(FilterConfig filterConfig) throws ServletException {

     ssoServer = filterConfig.getInitParameter(Conf.SSO_SERVER);
     logoutPath = filterConfig.getInitParameter(Conf.SSO_LOGOUT_PATH);
     excludedPaths = filterConfig.getInitParameter(Conf.SSO_EXCLUDED_PATHS);
    
     logger.info("XxlSsoWebFilter init.");
    

    }

    @OverRide
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    HttpServletRequest req = (HttpServletRequest) request;
    HttpServletResponse res = (HttpServletResponse) response;

     // make url
     String servletPath = req.getServletPath();
    
     // excluded path check
     if (excludedPaths!=null && excludedPaths.trim().length()>0) {
         for (String excludedPath:excludedPaths.split(",")) {
             String uriPattern = excludedPath.trim();
    
             // 支持ANT表达式
             if (antPathMatcher.match(uriPattern, servletPath)) {
                 // excluded path, allow
                 chain.doFilter(request, response);
                 return;
             }
    
         }
     }
    
     // logout path check
     if (logoutPath!=null
             && logoutPath.trim().length()>0
             && logoutPath.equals(servletPath)) {
    
         // remove cookie
         SsoWebLoginHelper.removeSessionIdByCookie(req, res);
    
         // redirect logout
         String logoutPageUrl = ssoServer.concat(Conf.SSO_LOGOUT);
         res.sendRedirect(logoutPageUrl);
    
         return;
     }
    
     // valid login user, cookie + redirect
     XxlSsoUser xxlUser = SsoWebLoginHelper.loginCheck(req, res);
    
     // valid login fail
     if (xxlUser == null) {
    
         String header = req.getHeader("content-type");
         boolean isJson=  header!=null && header.contains("json");
         if (isJson) {
    
             // json msg
             res.setContentType("application/json;charset=utf-8");
             res.getWriter().println("{\"code\":"+Conf.SSO_LOGIN_FAIL_RESULT.getCode()+", \"msg\":\""+ Conf.SSO_LOGIN_FAIL_RESULT.getMsg() +"\"}");
             return;
         } else {
    
             // total link
             String link = req.getRequestURL().toString();
    
             // redirect logout
             String loginPageUrl = ssoServer.concat(Conf.SSO_LOGIN)
                     + "?" + Conf.REDIRECT_URL + "=" + link;
    
             res.sendRedirect(loginPageUrl);
             return;
         }
    
     }
    
     // ser sso user
     request.setAttribute(Conf.SSO_USER, xxlUser);
    
    
     // already login, allow
     chain.doFilter(request, response);
     return;
    

    }

}
`
2.web.xml加入配置

XxlSsoWebFilter
com.it.config.XxlSsoWebFilter

sso_server
http://你的IP:端口/xxl-sso-server


SSO_LOGOUT_PATH
/logout


SSO_EXCLUDED_PATHS




XxlSsoWebFilter
/*

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant