Created
June 19, 2020 13:30
-
-
Save greenyleaf/bb29f22d14522aac41985af8082292f9 to your computer and use it in GitHub Desktop.
a Spring interceptor for Weixin(wechat) oauth2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package top.sdrkyj.custom.config; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import org.springframework.stereotype.Component; | |
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; | |
import org.springframework.web.util.UriComponentsBuilder; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletResponse; | |
@Component | |
public class WxAuthInterceptor extends HandlerInterceptorAdapter { | |
public static final String REDIRECT_URL_KEY = "top.sdrkyj.custom.config.WxAuthInterceptor_REDIRECT_URL_KEY"; | |
public static final String STATE_KEY = "top.sdrkyj.custom.config.WxAuthInterceptor_STATE_KEY"; | |
public static final String WX_USER_KEY = "top.sdrkyj.custom.config.WxAuthInterceptor_WX_USER_KEY"; | |
private static final Logger logger = LoggerFactory.getLogger(WxAuthInterceptor.class); | |
@Override | |
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { | |
logger.info("preHandle entered"); | |
logger.info("request.getSession().getAttribute(WX_USER_KEY), {}", request.getSession().getAttribute(WX_USER_KEY)); | |
if (request.getSession().getAttribute(WX_USER_KEY) == null) { | |
request.setAttribute("redirect", UriComponentsBuilder.fromHttpUrl(String.valueOf(request.getRequestURL())).query(request.getQueryString()).build().toUriString()); | |
request.getRequestDispatcher("/wx-authorize").forward(request, response); | |
return false; | |
} | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment