All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.shawven.security.browser.authentication.BrowserAuthenticationSuccessHandler Maven / Gradle / Ivy

There is a newer version: 2.1.0
Show newest version

package com.github.shawven.security.browser.authentication;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.shawven.security.authorization.ResponseData;
import com.github.shawven.security.authorization.Responses;
import com.github.shawven.security.browser.ResponseType;
import com.github.shawven.security.browser.config.BrowserConfiguration;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
import org.springframework.security.web.savedrequest.HttpSessionRequestCache;
import org.springframework.security.web.savedrequest.RequestCache;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 浏览器环境下登录成功的处理器
 *
 * @author Shoven
 * @since 2019-05-08 21:55
 */
public class BrowserAuthenticationSuccessHandler extends SavedRequestAwareAuthenticationSuccessHandler {

	private ObjectMapper objectMapper;

	private BrowserConfiguration browserConfiguration;

	private RequestCache requestCache;

    private BrowserLoginSuccessHandler loginSuccessHandler;

    public BrowserAuthenticationSuccessHandler(BrowserConfiguration browserConfiguration,
                                               BrowserLoginSuccessHandler loginSuccessHandler) {
        this.objectMapper = new ObjectMapper();
        this.requestCache = new HttpSessionRequestCache();
        this.browserConfiguration = browserConfiguration;
        this.loginSuccessHandler = loginSuccessHandler;
    }


	@Override
	public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
			Authentication authentication) throws IOException, ServletException {
        if (loginSuccessHandler != null) {
            loginSuccessHandler.onAuthenticationSuccess(request, response, authentication);
        }

		if (ResponseType.JSON.equals(browserConfiguration.getResponseType())) {
            ResponseData result = Responses.loginSuccess();
			response.setContentType("application/json;charset=UTF-8");
            response.getWriter().write(objectMapper.writeValueAsString(result));
		} else {
			// 如果设置了security.browser.singInSuccessUrl,总是跳到设置的地址上
			// 如果没设置,则尝试跳转到登录之前访问的地址上,如果登录前访问地址为空,则跳到网站根路径上
			if (StringUtils.isNotBlank(browserConfiguration.getSingInSuccessUrl())) {
				requestCache.removeRequest(request, response);
				setAlwaysUseDefaultTargetUrl(true);
				setDefaultTargetUrl(browserConfiguration.getSingInSuccessUrl());
			}
			super.onAuthenticationSuccess(request, response, authentication);
		}

	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy