SocialSignOnEndpoint.java 6.7 KB
Newer Older
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * Copyright [2020] [MaxKey of copyright http://www.maxkey.top]
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 

MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
18 19 20 21 22 23 24
/**
 * 
 */
package org.maxkey.authn.support.socialsignon;

import javax.servlet.http.HttpServletRequest;

MaxKey单点登录官方's avatar
fix  
MaxKey单点登录官方 已提交
25
import org.maxkey.authn.support.socialsignon.service.SocialsAssociate;
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
26
import org.maxkey.constants.ConstantsLoginType;
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
27 28 29 30 31 32 33 34 35 36 37
import org.maxkey.web.WebContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.web.WebAttributes;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

38 39
import me.zhyd.oauth.utils.AuthStateUtils;

MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
40 41 42 43 44 45 46 47 48 49 50
/**
 * @author Crystal.Sea
 *
 */
@Controller
@RequestMapping(value = "/logon/oauth20")
public class SocialSignOnEndpoint  extends AbstractSocialSignOnEndpoint{
	final static Logger _logger = LoggerFactory.getLogger(SocialSignOnEndpoint.class);
	
    public  ModelAndView socialSignOnAuthorize(String provider){
    	_logger.debug("SocialSignOn provider : "+provider);
51
    	String authorizationUrl=buildAuthRequest(provider).authorize(AuthStateUtils.createState());
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
		_logger.debug("authorize SocialSignOn : "+authorizationUrl);
		return WebContext.redirect(authorizationUrl);
    }
    
	@RequestMapping(value={"/authorize/{provider}"}, method = RequestMethod.GET)
	public ModelAndView authorize(@PathVariable String provider) {
		WebContext.setAttribute(SOCIALSIGNON_TYPE_SESSION, SOCIALSIGNON_TYPE.SOCIALSIGNON_TYPE_LOGON);
		return socialSignOnAuthorize(provider);
	}
	
	@RequestMapping(value={"/bind/{provider}"}, method = RequestMethod.GET)
	public ModelAndView bind(HttpServletRequest request,
				@PathVariable String provider) {
		WebContext.setAttribute(SOCIALSIGNON_SESSION_REDIRECT_URI, request.getParameter(SOCIALSIGNON_REDIRECT_URI));
		WebContext.setAttribute(SOCIALSIGNON_TYPE_SESSION, SOCIALSIGNON_TYPE.SOCIALSIGNON_TYPE_BIND);
		return socialSignOnAuthorize(provider);
	}
	
	@RequestMapping(value={"/unbind/{provider}"}, method = RequestMethod.GET)
	public ModelAndView unbind(HttpServletRequest request,
				@PathVariable String provider) {
		WebContext.setAttribute(SOCIALSIGNON_SESSION_REDIRECT_URI, request.getParameter(SOCIALSIGNON_REDIRECT_URI));
MaxKey单点登录官方's avatar
fix  
MaxKey单点登录官方 已提交
74
		SocialsAssociate socialSignOnUser =new SocialsAssociate();
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
75 76 77 78 79
		socialSignOnUser.setProvider(provider);
		socialSignOnUser.setUid(WebContext.getUserInfo().getId());
		socialSignOnUser.setUsername(WebContext.getUserInfo().getUsername());
		_logger.debug("Social Sign On unbind "+provider+" from user "+WebContext.getUserInfo().getUsername());
		
MaxKey单点登录官方's avatar
fix  
MaxKey单点登录官方 已提交
80
		socialsAssociateService.delete(socialSignOnUser);
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
		
		if(WebContext.getAttribute(SOCIALSIGNON_SESSION_REDIRECT_URI)!=null){
			return WebContext.redirect(WebContext.getAttribute(SOCIALSIGNON_SESSION_REDIRECT_URI).toString());
		}else{
			return WebContext.forward("/socialsignon/list");
		}
		
	}
	
	@RequestMapping(value={"/authorize/{provider}/{appid}"}, method = RequestMethod.GET)
	public ModelAndView authorize2AppId(@PathVariable("provider") String provider,
			@PathVariable("appid") String appid) {
		WebContext.setAttribute(SOCIALSIGNON_SESSION_REDIRECT_URI, "/authorize/"+appid);
		return authorize(provider);
	}
	
	
	@RequestMapping(value={"/callback/{provider}"}, method = RequestMethod.GET)
	public ModelAndView callback(@PathVariable String provider
			) {
		this.provider=provider;
102
		this.authCallback();
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
103
		_logger.debug(this.accountId);
MaxKey单点登录官方's avatar
fix  
MaxKey单点登录官方 已提交
104
		SocialsAssociate socialSignOnUserToken =new SocialsAssociate();
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
		socialSignOnUserToken.setProvider(provider);
		socialSignOnUserToken.setSocialuid(this.accountId);
		
		//for login
		String socialSignOnType= "";
		if(WebContext.getAttribute(SOCIALSIGNON_TYPE_SESSION)!=null){
			socialSignOnType=WebContext.getAttribute(SOCIALSIGNON_TYPE_SESSION).toString();
		}
		
		if(socialSignOnType.equals(SOCIALSIGNON_TYPE.SOCIALSIGNON_TYPE_LOGON)||socialSignOnType.equals("")){
			socialSignOn(socialSignOnUserToken);
			return WebContext.redirect("/index");
		}else{
			socialBind(socialSignOnUserToken);
		}
		
		if(WebContext.getAttribute(SOCIALSIGNON_SESSION_REDIRECT_URI)!=null){
			return WebContext.redirect(WebContext.getAttribute(SOCIALSIGNON_SESSION_REDIRECT_URI).toString());
		}else{
			return WebContext.forward("/socialsignon/list");
		}
		
	}
	
MaxKey单点登录官方's avatar
fix  
MaxKey单点登录官方 已提交
129
	public boolean socialBind(SocialsAssociate socialSignOnUserToken){
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
130 131 132
		socialSignOnUserToken.setSocialUserInfo(accountJsonString);
		socialSignOnUserToken.setUid(WebContext.getUserInfo().getId());
		socialSignOnUserToken.setUsername(WebContext.getUserInfo().getUsername());
133 134
		//socialSignOnUserToken.setAccessToken(JsonUtils.object2Json(accessToken));
		//socialSignOnUserToken.setExAttribute(JsonUtils.object2Json(accessToken.getResponseObject()));
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
135
		_logger.debug("Social Bind : "+socialSignOnUserToken);
MaxKey单点登录官方's avatar
fix  
MaxKey单点登录官方 已提交
136 137
		this.socialsAssociateService.delete(socialSignOnUserToken);
		this.socialsAssociateService.insert(socialSignOnUserToken);
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
138 139 140
		return true;
	}
	
MaxKey单点登录官方's avatar
fix  
MaxKey单点登录官方 已提交
141
	public boolean socialSignOn(SocialsAssociate socialSignOnUserToken){
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
142
		
MaxKey单点登录官方's avatar
fix  
MaxKey单点登录官方 已提交
143
		socialSignOnUserToken=this.socialsAssociateService.get(socialSignOnUserToken);
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
144 145 146 147 148 149
		
		_logger.debug("callback SocialSignOn User Token : "+socialSignOnUserToken);
		if(null !=socialSignOnUserToken){

			_logger.debug("Social Sign On from "+socialSignOnUserToken.getProvider()+" mapping to user "+socialSignOnUserToken.getUsername());
			
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
150 151 152 153 154 155 156
			authenticationProvider.trustAuthentication(socialSignOnUserToken.getUsername(), ConstantsLoginType.SOCIALSIGNON,this.socialSignOnProvider.getProviderName(),"xe00000004","success");
			//socialSignOnUserToken.setAccessToken(JsonUtils.object2Json(this.accessToken));
			socialSignOnUserToken.setSocialUserInfo(accountJsonString);
			//socialSignOnUserToken.setExAttribute(JsonUtils.object2Json(accessToken.getResponseObject()));
			
			this.socialsAssociateService.update(socialSignOnUserToken);
			
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
157 158 159 160 161 162 163
			
		}else{
			WebContext.getRequest().getSession().setAttribute(WebAttributes.AUTHENTICATION_EXCEPTION, new BadCredentialsException(WebContext.getI18nValue("login.error.social")));
		}
		return true;
	}
}