com.healthy.security.browser.BrowserSecurityController Maven / Gradle / Ivy
package com.healthy.security.browser;
import cn.hutool.core.util.StrUtil;
import com.healthy.common.core.support.Message;
import com.healthy.common.security.properties.SecurityConstants;
import com.healthy.common.security.properties.SecurityProperties;
import com.healthy.common.security.social.SocialController;
import com.healthy.common.security.social.support.SocialUserInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.security.web.DefaultRedirectStrategy;
import org.springframework.security.web.RedirectStrategy;
import org.springframework.security.web.savedrequest.HttpSessionRequestCache;
import org.springframework.security.web.savedrequest.RequestCache;
import org.springframework.security.web.savedrequest.SavedRequest;
import org.springframework.social.connect.Connection;
import org.springframework.social.connect.web.ProviderSignInUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.ServletWebRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* BrowserSecurityController
*
* @author xiaomingzhang
*/
@Slf4j
@RestController
public class BrowserSecurityController extends SocialController {
private final RequestCache requestCache = new HttpSessionRequestCache();
private final RedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
@Autowired
private SecurityProperties securityProperties;
@Autowired
private ProviderSignInUtils providerSignInUtils;
/**
* When identity authentication is required, jump to here
*/
@RequestMapping(SecurityConstants.DEFAULT_UNAUTHENTICATION_URL)
@ResponseStatus(code = HttpStatus.UNAUTHORIZED)
public Message requireAuthentication(HttpServletRequest request, HttpServletResponse response) throws IOException {
SavedRequest savedRequest = requestCache.getRequest(request, response);
if (savedRequest != null) {
String targetUrl = savedRequest.getRedirectUrl();
log.info("引发跳转的请求是: {}", targetUrl);
if (StrUtil.endWithIgnoreCase(targetUrl, ".html")) {
redirectStrategy.sendRedirect(request, response, securityProperties.getBrowser().getSignInPage());
}
}
return Message.failed("访问的服务需要身份认证,请引导用户到登录页");
}
/**
* When the user logs in for the first time, guide the user to register or bind
* This service is used to obtain social networking site user information on the registration or binding page
*
* @param request the current request attributes
* @return SocialUserInfo
*/
@GetMapping(SecurityConstants.DEFAULT_SOCIAL_USER_INFO_URL)
public SocialUserInfo getSocialUserInfo(HttpServletRequest request) {
Connection> connection = providerSignInUtils.getConnectionFromSession(new ServletWebRequest(request));
return buildSocialUserInfo(connection);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy