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

com.digitalchina.platform.security.auth.CustomCasAuthenticationEntryPoint Maven / Gradle / Ivy

The newest version!
package com.digitalchina.platform.security.auth;


import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.digitalchina.platform.security.constants.SecurityConstants;
import com.digitalchina.platform.security.properties.CasProperties;
import com.google.common.collect.Maps;
import org.jasig.cas.client.util.CommonUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.cas.ServiceProperties;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.util.Assert;

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

public class CustomCasAuthenticationEntryPoint implements AuthenticationEntryPoint, InitializingBean {
    private ServiceProperties serviceProperties;
    private String loginUrl;
    private boolean encodeServiceUrlWithSessionId = true;
    private boolean authPomptType;
    private String ssoUrl;
    private static final SerializeConfig cfg = new SerializeConfig();

    @Autowired
    private CasProperties casProperties;

    public CustomCasAuthenticationEntryPoint() {
    }

    public void afterPropertiesSet() throws Exception {
        Assert.hasLength(this.loginUrl, "loginUrl must be specified");
        Assert.notNull(this.serviceProperties, "serviceProperties must be specified");
        Assert.notNull(this.serviceProperties.getService(), "serviceProperties.getService() cannot be null.");
    }

    public final void commence(HttpServletRequest servletRequest, HttpServletResponse response, AuthenticationException authenticationException) throws IOException, ServletException {
        if (authPomptType) {
            Map map = Maps.newHashMap();
            map.put("status", "ERROR");
            map.put("message", "unLogin error!");
            map.put("code", SecurityConstants.RESPONSE_CODE_UNLOGIN);
            /*if (ssoUrl.endsWith("/")) {
                ssoUrl = ssoUrl.substring(0, ssoUrl.length());
            }*/

            String url = String.format("%s?service=%s", ssoUrl, getLoginSuccessUrl());
            map.put("result", url);
            response.setContentType("application/json;charset=UTF-8");
            PrintWriter pw = response.getWriter();
            pw.print(JSONObject.toJSONString(map, cfg, SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullListAsEmpty));
            pw.flush();
            return;
        }
        String urlEncodedService = this.createServiceUrl(servletRequest, response);
        String redirectUrl = this.createRedirectUrl(urlEncodedService);
        this.preCommence(servletRequest, response);
        response.sendRedirect(redirectUrl);
    }

    private String getLoginSuccessUrl(){
        return String.format("%s%s", casProperties.getLocalAppUrl(), casProperties.getFilterProcessesUrl());
    }

    protected String createServiceUrl(HttpServletRequest request, HttpServletResponse response) {
        return CommonUtils.constructServiceUrl((HttpServletRequest) null, response, this.serviceProperties.getService(), (String) null, this.serviceProperties.getArtifactParameter(), this.encodeServiceUrlWithSessionId);
    }

    protected String createRedirectUrl(String serviceUrl) {
        return CommonUtils.constructRedirectUrl(this.loginUrl, this.serviceProperties.getServiceParameter(), serviceUrl, this.serviceProperties.isSendRenew(), false);
    }

    protected void preCommence(HttpServletRequest request, HttpServletResponse response) {
    }

    public final String getLoginUrl() {
        return this.loginUrl;
    }

    public final ServiceProperties getServiceProperties() {
        return this.serviceProperties;
    }

    public final void setLoginUrl(String loginUrl) {
        this.loginUrl = loginUrl;
    }

    public final void setServiceProperties(ServiceProperties serviceProperties) {
        this.serviceProperties = serviceProperties;
    }

    public final void setEncodeServiceUrlWithSessionId(boolean encodeServiceUrlWithSessionId) {
        this.encodeServiceUrlWithSessionId = encodeServiceUrlWithSessionId;
    }

    protected boolean getEncodeServiceUrlWithSessionId() {
        return this.encodeServiceUrlWithSessionId;
    }

    public void setSsoUrl(String ssoUrl) {
        this.ssoUrl = ssoUrl;
    }

    public void setAuthPomptType(boolean authPomptType) {
        this.authPomptType = authPomptType;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy