소스 검색

SocialsAssociateService重名

vincent 3 년 전
부모
커밋
1dd31a3c10

+ 238 - 215
maxkey-authentications/maxkey-authentication-social/src/main/java/org/maxkey/authn/support/socialsignon/SocialSignOnEndpoint.java

@@ -1,22 +1,22 @@
 /*
  * 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.
  */
- 
+
 
 /**
- * 
+ *
  */
 package org.maxkey.authn.support.socialsignon;
 
@@ -46,217 +46,240 @@ import me.zhyd.oauth.request.AuthRequest;
  */
 @Controller
 @RequestMapping(value = "/logon/oauth20")
-public class SocialSignOnEndpoint  extends AbstractSocialSignOnEndpoint{
-	final static Logger _logger = LoggerFactory.getLogger(SocialSignOnEndpoint.class);
-	
-    public  ModelAndView socialSignOnAuthorize(HttpServletRequest request,String provider){
-    	_logger.trace("SocialSignOn provider : " + provider);
-    	String authorizationUrl=buildAuthRequest(provider).authorize(request.getSession().getId());
-		_logger.trace("authorize SocialSignOn : " + authorizationUrl);
-		return WebContext.redirect(authorizationUrl);
+public class SocialSignOnEndpoint extends AbstractSocialSignOnEndpoint
+{
+    final static Logger _logger = LoggerFactory.getLogger(SocialSignOnEndpoint.class);
+
+    public ModelAndView socialSignOnAuthorize(HttpServletRequest request, String provider)
+    {
+        _logger.trace("SocialSignOn provider : " + provider);
+        String authorizationUrl = buildAuthRequest(provider).authorize(request.getSession().getId());
+        _logger.trace("authorize SocialSignOn : " + authorizationUrl);
+        return WebContext.redirect(authorizationUrl);
+    }
+
+    @RequestMapping(value = {"/authorize/{provider}"}, method = RequestMethod.GET)
+    public ModelAndView authorize(HttpServletRequest request,
+                                  @PathVariable String provider)
+    {
+        WebContext.setAttribute(SOCIALSIGNON_TYPE_SESSION, SOCIALSIGNON_TYPE.SOCIALSIGNON_TYPE_LOGON);
+        return socialSignOnAuthorize(request, 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(request, 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));
+        SocialsAssociate socialSignOnUser = new SocialsAssociate();
+        socialSignOnUser.setProvider(provider);
+        socialSignOnUser.setUserId(WebContext.getUserInfo().getId());
+        socialSignOnUser.setUsername(WebContext.getUserInfo().getUsername());
+        _logger.debug("Social Sign On unbind {} from user {}",
+                provider,
+                WebContext.getUserInfo().getUsername()
+        );
+
+        socialsAssociateService.delete(socialSignOnUser);
+
+        Object redirect_uri = WebContext.getAttribute(SOCIALSIGNON_SESSION_REDIRECT_URI);
+        if (redirect_uri != null)
+        {
+            return WebContext.redirect(redirect_uri.toString());
+        } else
+        {
+            return WebContext.forward("/socialsignon/list");
+        }
+
+    }
+
+    @RequestMapping(value = {"/authorize/{provider}/{appid}"}, method = RequestMethod.GET)
+    public ModelAndView authorize2AppId(HttpServletRequest request,
+                                        @PathVariable("provider") String provider,
+                                        @PathVariable("appid") String appid)
+    {
+        WebContext.setAttribute(SOCIALSIGNON_SESSION_REDIRECT_URI, "/authorize/" + appid);
+        return authorize(request, provider);
+    }
+
+    @RequestMapping(value = {"/scanqrcode/{provider}"}, method = RequestMethod.GET)
+    @ResponseBody
+    public SocialsProvider scanQRCode(
+            HttpServletRequest request,
+            @PathVariable("provider") String provider)
+    {
+        AuthRequest authRequest = buildAuthRequest(provider);
+
+        if (authRequest == null)
+        {
+            _logger.error("build authRequest fail .");
+        }
+        String state = request.getSession().getId();
+        authRequest.authorize(state);
+
+        SocialsProvider socialSignOnProvider = socialSignOnProviderService.get(provider);
+        SocialsProvider scanQRCodeProvider = new SocialsProvider();
+
+        scanQRCodeProvider.setId(socialSignOnProvider.getId());
+        scanQRCodeProvider.setProvider(socialSignOnProvider.getProvider());
+        scanQRCodeProvider.setProviderName(socialSignOnProvider.getProviderName());
+        scanQRCodeProvider.setState(state);
+        scanQRCodeProvider.setClientId(socialSignOnProvider.getClientId());
+        scanQRCodeProvider.setRedirectUri(applicationConfig.getServerPrefix() +
+                "/logon/oauth20/callback/" + provider);
+        scanQRCodeProvider.setAgentId(socialSignOnProvider.getAgentId());
+
+        return scanQRCodeProvider;
     }
-    
-	@RequestMapping(value={"/authorize/{provider}"}, method = RequestMethod.GET)
-	public ModelAndView authorize(HttpServletRequest request,
-									@PathVariable String provider) {
-		WebContext.setAttribute(SOCIALSIGNON_TYPE_SESSION, SOCIALSIGNON_TYPE.SOCIALSIGNON_TYPE_LOGON);
-		return socialSignOnAuthorize(request,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(request,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));
-		SocialsAssociate socialSignOnUser =new SocialsAssociate();
-		socialSignOnUser.setProvider(provider);
-		socialSignOnUser.setUserId(WebContext.getUserInfo().getId());
-		socialSignOnUser.setUsername(WebContext.getUserInfo().getUsername());
-		_logger.debug("Social Sign On unbind {} from user {}",
-		                provider,
-		                WebContext.getUserInfo().getUsername()
-		          );
-		
-		socialsAssociateService.delete(socialSignOnUser);
-		
-		Object redirect_uri = WebContext.getAttribute(SOCIALSIGNON_SESSION_REDIRECT_URI);
-		if(redirect_uri != null){
-			return WebContext.redirect(redirect_uri.toString());
-		}else{
-			return WebContext.forward("/socialsignon/list");
-		}
-		
-	}
-	
-	@RequestMapping(value={"/authorize/{provider}/{appid}"}, method = RequestMethod.GET)
-	public ModelAndView authorize2AppId(HttpServletRequest request,
-										@PathVariable("provider") String provider,
-										@PathVariable("appid") String appid) {
-		WebContext.setAttribute(SOCIALSIGNON_SESSION_REDIRECT_URI, "/authorize/"+appid);
-		return authorize(request,provider);
-	}
-	
-	@RequestMapping(value={"/scanqrcode/{provider}"}, method = RequestMethod.GET)
-	@ResponseBody
-	public SocialsProvider scanQRCode(
-							HttpServletRequest request,
-							@PathVariable("provider") String provider) {
-	    AuthRequest authRequest =buildAuthRequest(provider);
-	   
-	    if(authRequest == null ) {
-	        _logger.error("build authRequest fail .");
-	    }
-	    String state = request.getSession().getId();
-	    authRequest.authorize(state);
-	    
-		SocialsProvider socialSignOnProvider = socialSignOnProviderService.get(provider);
-		SocialsProvider scanQRCodeProvider = new SocialsProvider();
-		
-		scanQRCodeProvider.setId(socialSignOnProvider.getId());
-		scanQRCodeProvider.setProvider(socialSignOnProvider.getProvider());
-		scanQRCodeProvider.setProviderName(socialSignOnProvider.getProviderName());
-		scanQRCodeProvider.setState(state);
-		scanQRCodeProvider.setClientId(socialSignOnProvider.getClientId());
-		scanQRCodeProvider.setRedirectUri(applicationConfig.getServerPrefix()+ 
-                "/logon/oauth20/callback/"+provider);
-		scanQRCodeProvider.setAgentId(socialSignOnProvider.getAgentId());
-		
-		return scanQRCodeProvider;
-	}
-	
-	
-	@RequestMapping(value={"/callback/{provider}"}, method = RequestMethod.GET)
-	public ModelAndView callback(@PathVariable String provider) {
-		 //auth call back may exception
-	    try {
-	    	SocialsAssociate socialsAssociate = null;
-    		this.provider=provider;
-    		this.authCallback();
-    		_logger.debug(this.accountId);
-    		socialsAssociate =new SocialsAssociate();
-    		socialsAssociate.setProvider(provider);
-    		socialsAssociate.setSocialUserId(this.accountId);
-    		
-    		//for login
-    		String socialSignOnType= 
-    		        (WebContext.getAttribute(SOCIALSIGNON_TYPE_SESSION)!=null) ? 
-    		                  (WebContext.getAttribute(SOCIALSIGNON_TYPE_SESSION).toString()) : "";
-    		
-    		
-    		if(socialSignOnType.equals(SOCIALSIGNON_TYPE.SOCIALSIGNON_TYPE_LOGON)
-    		        ||socialSignOnType.equals("")){
-    			socialSignOn(socialsAssociate);
-    			return WebContext.redirect("/index");
-    		}else{
-    			socialBind(socialsAssociate);
-    		}
-    		Object redirect_uri = WebContext.getAttribute(SOCIALSIGNON_SESSION_REDIRECT_URI);
-    		if(redirect_uri != null){
-    			return WebContext.redirect(redirect_uri.toString());
-    		}else{
-    			return WebContext.forward("/socialsignon/list");
-    		}
-    		
-	    }catch(Exception e) {
-	        _logger.error("callback Exception  ",e);
-	    }
-	    
-	    return WebContext.redirect("/login");
-	}
-
-	/**
-	 *  重写钉钉回到方法
-	 * @return
-	 */
-	@RequestMapping(value={"/callback/login_dingtalk"}, method = RequestMethod.GET)
-	public ModelAndView callbackDingTalk() {
-		//auth call back may exception
-		// TODO  钉钉 回调函数
-		try {
-			SocialsAssociate socialsAssociate = null;
-			this.provider="dingtalk";
-			this.authCallback();
-			_logger.debug(this.accountId);
-			socialsAssociate =new SocialsAssociate();
-			socialsAssociate.setProvider(provider);
-			socialsAssociate.setSocialUserId(this.accountId);
-
-			//for login
-			String socialSignOnType=
-					(WebContext.getAttribute(SOCIALSIGNON_TYPE_SESSION)!=null) ?
-							(WebContext.getAttribute(SOCIALSIGNON_TYPE_SESSION).toString()) : "";
-
-
-			if(socialSignOnType.equals(SOCIALSIGNON_TYPE.SOCIALSIGNON_TYPE_LOGON)
-					||socialSignOnType.equals("")){
-				socialSignOn(socialsAssociate);
-				return WebContext.redirect("/index");
-			}else{
-				socialBind(socialsAssociate);
-			}
-			Object redirect_uri = WebContext.getAttribute(SOCIALSIGNON_SESSION_REDIRECT_URI);
-			if(redirect_uri != null){
-				return WebContext.redirect(redirect_uri.toString());
-			}else{
-				return WebContext.forward("/socialsignon/list");
-			}
-
-		}catch(Exception e) {
-			_logger.error("callback Exception  ",e);
-		}
-
-		return WebContext.redirect("/login");
-	}
-	
-	public boolean socialBind(SocialsAssociate socialsAssociate){
-	    if(null == socialsAssociate) {
-	        return false;
-	    }
-	    
-	    socialsAssociate.setSocialUserInfo(accountJsonString);
-	    socialsAssociate.setUserId(WebContext.getUserInfo().getId());
-		socialsAssociate.setUsername(WebContext.getUserInfo().getUsername());
-		//socialsAssociate.setAccessToken(JsonUtils.object2Json(accessToken));
-		//socialsAssociate.setExAttribute(JsonUtils.object2Json(accessToken.getResponseObject()));
-		_logger.debug("Social Bind : "+socialsAssociate);
-		this.socialsAssociateService.delete(socialsAssociate);
-		this.socialsAssociateService.insert(socialsAssociate);
-		return true;
-	}
-	
-	public boolean socialSignOn(SocialsAssociate socialsAssociate){
-		
-	    socialsAssociate=this.socialsAssociateService.get(socialsAssociate);
-		
-		_logger.debug("Loaded SocialSignOn Socials Associate : "+socialsAssociate);
-		
-		if(null == socialsAssociate) {
-		    WebContext.getRequest().getSession().setAttribute(
-		            WebAttributes.AUTHENTICATION_EXCEPTION, 
-		            new BadCredentialsException(WebContext.getI18nValue("login.error.social"))
-		          );
+
+
+//	@RequestMapping(value={"/callback/{provider}"}, method = RequestMethod.GET)
+//	public ModelAndView callback(@PathVariable String provider) {
+//		 //auth call back may exception
+//	    try {
+//	    	SocialsAssociate socialsAssociate = null;
+//    		this.provider=provider;
+//    		this.authCallback();
+//    		_logger.debug(this.accountId);
+//    		socialsAssociate =new SocialsAssociate();
+//    		socialsAssociate.setProvider(provider);
+//    		socialsAssociate.setSocialUserId(this.accountId);
+//
+//    		//for login
+//    		String socialSignOnType=
+//    		        (WebContext.getAttribute(SOCIALSIGNON_TYPE_SESSION)!=null) ?
+//    		                  (WebContext.getAttribute(SOCIALSIGNON_TYPE_SESSION).toString()) : "";
+//
+//
+//    		if(socialSignOnType.equals(SOCIALSIGNON_TYPE.SOCIALSIGNON_TYPE_LOGON)
+//    		        ||socialSignOnType.equals("")){
+//    			socialSignOn(socialsAssociate);
+//    			return WebContext.redirect("/index");
+//    		}else{
+//    			socialBind(socialsAssociate);
+//    		}
+//    		Object redirect_uri = WebContext.getAttribute(SOCIALSIGNON_SESSION_REDIRECT_URI);
+//    		if(redirect_uri != null){
+//    			return WebContext.redirect(redirect_uri.toString());
+//    		}else{
+//    			return WebContext.forward("/socialsignon/list");
+//    		}
+//
+//	    }catch(Exception e) {
+//	        _logger.error("callback Exception  ",e);
+//	    }
+//
+//	    return WebContext.redirect("/login");
+//	}
+
+    /**
+     *  重写钉钉回到方法
+     * @return
+     */
+    @RequestMapping(value = {"/callback/{provider}"}, method = RequestMethod.GET)
+    public ModelAndView callbackDingTalk(@PathVariable String provider)
+    {
+        //auth call back may exception
+        // TODO  钉钉 回调函数
+        try
+        {
+            SocialsAssociate socialsAssociate = null;
+            this.provider = provider;
+            this.authCallback();
+            _logger.debug(this.accountId);
+            socialsAssociate = new SocialsAssociate();
+            socialsAssociate.setProvider(provider);
+            socialsAssociate.setSocialUserId(this.accountId);
+
+
+            //for login
+            String socialSignOnType =
+                    (WebContext.getAttribute(SOCIALSIGNON_TYPE_SESSION) != null) ?
+                            (WebContext.getAttribute(SOCIALSIGNON_TYPE_SESSION).toString()) : "";
+
+
+            if (socialSignOnType.equals(SOCIALSIGNON_TYPE.SOCIALSIGNON_TYPE_LOGON)
+                    || socialSignOnType.equals(""))
+            {
+                socialSignOn(socialsAssociate);
+                return WebContext.redirect("http://www.baidu.com");
+            } else
+            {
+                socialBind(socialsAssociate);
+            }
+
+            Object redirect_uri = WebContext.getAttribute(SOCIALSIGNON_SESSION_REDIRECT_URI);
+            if (redirect_uri != null)
+            {
+                return WebContext.redirect(redirect_uri.toString());
+            } else
+            {
+                return WebContext.forward("/socialsignon/list");
+            }
+
+        } catch (Exception e)
+        {
+            _logger.error("callback Exception  ", e);
+        }
+
+        return WebContext.redirect("/login");
+    }
+
+    public boolean socialBind(SocialsAssociate socialsAssociate)
+    {
+        if (null == socialsAssociate)
+        {
             return false;
-		}
-		
-		_logger.debug("Social Sign On from {} mapping to user {}",
-		                socialsAssociate.getProvider(),socialsAssociate.getUsername());
-		
-		LoginCredential loginCredential =new LoginCredential(
-		        socialsAssociate.getUsername(),"",ConstantsLoginType.SOCIALSIGNON);
-		loginCredential.setProvider(this.socialSignOnProvider.getProviderName());
-        authenticationProvider.authentication(loginCredential,true);
+        }
+
+        socialsAssociate.setSocialUserInfo(accountJsonString);
+        socialsAssociate.setUserId(WebContext.getUserInfo().getId());
+        socialsAssociate.setUsername(WebContext.getUserInfo().getUsername());
+        //socialsAssociate.setAccessToken(JsonUtils.object2Json(accessToken));
+        //socialsAssociate.setExAttribute(JsonUtils.object2Json(accessToken.getResponseObject()));
+        _logger.debug("Social Bind : " + socialsAssociate);
+        this.socialsAssociateService.delete(socialsAssociate);
+        this.socialsAssociateService.insert(socialsAssociate);
+        return true;
+    }
+
+    public boolean socialSignOn(SocialsAssociate socialsAssociate)
+    {
+
+        socialsAssociate = this.socialsAssociateService.get(socialsAssociate);
+
+        _logger.debug("Loaded SocialSignOn Socials Associate : " + socialsAssociate);
+
+        if (null == socialsAssociate)
+        {
+            WebContext.getRequest().getSession().setAttribute(
+                    WebAttributes.AUTHENTICATION_EXCEPTION,
+                    new BadCredentialsException(WebContext.getI18nValue("login.error.social"))
+            );
+            return false;
+        }
+
+        _logger.debug("Social Sign On from {} mapping to user {}",
+                socialsAssociate.getProvider(), socialsAssociate.getUsername());
+
+        LoginCredential loginCredential = new LoginCredential(
+                socialsAssociate.getUsername(), "", ConstantsLoginType.SOCIALSIGNON);
+        loginCredential.setProvider(this.socialSignOnProvider.getProviderName());
+        authenticationProvider.authentication(loginCredential, true);
         //socialsAssociate.setAccessToken(JsonUtils.object2Json(this.accessToken));
-		socialsAssociate.setSocialUserInfo(accountJsonString);
-		//socialsAssociate.setExAttribute(JsonUtils.object2Json(accessToken.getResponseObject()));
-		
-		this.socialsAssociateService.update(socialsAssociate);
-		return true;
-	}
+        socialsAssociate.setSocialUserInfo(accountJsonString);
+        //socialsAssociate.setExAttribute(JsonUtils.object2Json(accessToken.getResponseObject()));
+
+        this.socialsAssociateService.update(socialsAssociate);
+        return true;
+    }
 }

+ 2 - 2
maxkey-identitys/maxkey-synchronizers-dingding/src/main/java/org/maxkey/synchronizer/dingding/DingdingUsersService.java

@@ -23,7 +23,7 @@ import org.maxkey.constants.ConstantsPasswordSetType;
 import org.maxkey.entity.SocialsAssociate;
 import org.maxkey.entity.Synchronizers;
 import org.maxkey.entity.UserInfo;
-import org.maxkey.persistence.service.SocialsAssociateService;
+import org.maxkey.persistence.service.SocialsAssociateOptService;
 import org.maxkey.persistence.service.UserInfoService;
 import org.maxkey.synchronizer.ISynchronizerService;
 import org.slf4j.Logger;
@@ -50,7 +50,7 @@ public class DingdingUsersService implements ISynchronizerService
     UserInfoService userInfoService;
 
     @Autowired
-    SocialsAssociateService socialsAssociateService;
+    SocialsAssociateOptService socialsAssociateService;
 
 
     String access_token;

+ 0 - 0
maxkey-persistence/src/main/java/org/maxkey/persistence/service/SocialsAssociateService.java → maxkey-persistence/src/main/java/org/maxkey/persistence/service/SocialsAssociateOptService.java


+ 4 - 7
maxkey-persistence/src/test/java/org/apache/mybatis/jpa/test/SocialsAssociateServiceTest.java

@@ -2,11 +2,8 @@ package org.apache.mybatis.jpa.test;
 
 import org.apache.mybatis.jpa.util.WebContext;
 import org.junit.Test;
-import org.maxkey.entity.Accounts;
 import org.maxkey.entity.SocialsAssociate;
-import org.maxkey.entity.SocialsProvider;
-import org.maxkey.persistence.service.AccountsService;
-import org.maxkey.persistence.service.SocialsAssociateService;
+import org.maxkey.persistence.service.SocialsAssociateOptService;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.context.ApplicationContext;
@@ -17,11 +14,11 @@ public class SocialsAssociateServiceTest
 
     public static ApplicationContext context;
 
-    public static SocialsAssociateService service;
+    public static SocialsAssociateOptService service;
 
-    public SocialsAssociateService getservice()
+    public SocialsAssociateOptService getservice()
     {
-        service = (SocialsAssociateService) WebContext.getBean("socialsAssociateService");
+        service = (SocialsAssociateOptService) WebContext.getBean("socialsAssociateService");
         return service;
     }