socialSignOn

This commit is contained in:
MaxKey
2021-08-02 21:24:40 +08:00
parent ad2dbb1c7c
commit 7315252c87
7 changed files with 69 additions and 60 deletions

View File

@@ -83,20 +83,23 @@ public class AbstractSocialSignOnEndpoint {
ApplicationConfig applicationConfig;
protected AuthRequest buildAuthRequest(String provider){
SocialSignOnProvider socialSignOnProvider = socialSignOnProviderService.get(provider);
_logger.debug("socialSignOn Provider : "+socialSignOnProvider);
if(socialSignOnProvider!=null){
authRequest=socialSignOnProviderService.getAuthRequest(provider,applicationConfig);
WebContext.setAttribute(SOCIALSIGNON_OAUTH_SERVICE_SESSION, authRequest);
WebContext.setAttribute(SOCIALSIGNON_PROVIDER_SESSION, socialSignOnProvider);
return authRequest;
}
try {
SocialSignOnProvider socialSignOnProvider = socialSignOnProviderService.get(provider);
_logger.debug("socialSignOn Provider : "+socialSignOnProvider);
if(socialSignOnProvider!=null){
authRequest=socialSignOnProviderService.getAuthRequest(provider,applicationConfig);
WebContext.setAttribute(SOCIALSIGNON_OAUTH_SERVICE_SESSION, authRequest);
WebContext.setAttribute(SOCIALSIGNON_PROVIDER_SESSION, socialSignOnProvider);
return authRequest;
}
}catch(Exception e) {
_logger.debug("buildAuthRequest Exception ",e);
}
return null;
}
protected String authCallback() {
protected String authCallback() throws Exception {
authRequest=(AuthRequest)WebContext.getAttribute(SOCIALSIGNON_OAUTH_SERVICE_SESSION);
socialSignOnProvider=(SocialSignOnProvider)WebContext.getAttribute(SOCIALSIGNON_PROVIDER_SESSION);
WebContext.removeAttribute(SOCIALSIGNON_OAUTH_SERVICE_SESSION);

View File

@@ -24,7 +24,6 @@ import javax.servlet.http.HttpServletRequest;
import org.maxkey.authn.support.socialsignon.service.SocialSignOnProvider;
import org.maxkey.authn.support.socialsignon.service.SocialsAssociate;
import org.maxkey.configuration.ApplicationConfig;
import org.maxkey.constants.ConstantsLoginType;
import org.maxkey.web.WebContext;
import org.slf4j.Logger;
@@ -48,9 +47,9 @@ public class SocialSignOnEndpoint extends AbstractSocialSignOnEndpoint{
final static Logger _logger = LoggerFactory.getLogger(SocialSignOnEndpoint.class);
public ModelAndView socialSignOnAuthorize(HttpServletRequest request,String provider){
_logger.debug("SocialSignOn provider : "+provider);
_logger.trace("SocialSignOn provider : "+provider);
String authorizationUrl=buildAuthRequest(provider).authorize(request.getSession().getId());
_logger.debug("authorize SocialSignOn : "+authorizationUrl);
_logger.trace("authorize SocialSignOn : "+authorizationUrl);
return WebContext.redirect(authorizationUrl);
}
@@ -111,12 +110,10 @@ public class SocialSignOnEndpoint extends AbstractSocialSignOnEndpoint{
@RequestMapping(value={"/callback/{provider}"}, method = RequestMethod.GET)
public ModelAndView callback(@PathVariable String provider
) {
SocialsAssociate socialsAssociate = null;
//auth call back may exception
public ModelAndView callback(@PathVariable String provider) {
//auth call back may exception
try {
SocialsAssociate socialsAssociate = null;
this.provider=provider;
this.authCallback();
_logger.debug(this.accountId);
@@ -124,29 +121,30 @@ public class SocialSignOnEndpoint extends AbstractSocialSignOnEndpoint{
socialsAssociate.setProvider(provider);
socialsAssociate.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(socialsAssociate);
return WebContext.redirect("/index");
}else{
socialBind(socialsAssociate);
}
if(WebContext.getAttribute(SOCIALSIGNON_SESSION_REDIRECT_URI)!=null){
return WebContext.redirect(WebContext.getAttribute(SOCIALSIGNON_SESSION_REDIRECT_URI).toString());
}else{
return WebContext.forward("/socialsignon/list");
}
}catch(Exception e) {
_logger.error("callback Exception ",e);
}
//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(socialsAssociate);
return WebContext.redirect("/index");
}else{
socialBind(socialsAssociate);
}
if(WebContext.getAttribute(SOCIALSIGNON_SESSION_REDIRECT_URI)!=null){
return WebContext.redirect(WebContext.getAttribute(SOCIALSIGNON_SESSION_REDIRECT_URI).toString());
}else{
return WebContext.forward("/socialsignon/list");
}
return WebContext.redirect("/login");
}
public boolean socialBind(SocialsAssociate socialsAssociate){

View File

@@ -21,7 +21,6 @@ import java.util.HashMap;
import java.util.List;
import org.maxkey.configuration.ApplicationConfig;
import org.maxkey.web.WebContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -44,7 +43,7 @@ public class SocialSignOnProviderService{
return socialSignOnProviderMaps.get(provider);
}
public AuthRequest getAuthRequest(String provider,ApplicationConfig applicationConfig) {
public AuthRequest getAuthRequest(String provider,ApplicationConfig applicationConfig) throws Exception {
AuthRequest authRequest = null;
AuthConfig authConfig = AuthConfig.builder()
.clientId(this.get(provider).getClientId())
@@ -107,7 +106,7 @@ public class SocialSignOnProviderService{
return authRequest;
}
public String getAccountId(String provider,AuthResponse<?> authResponse) {
public String getAccountId(String provider,AuthResponse<?> authResponse) throws Exception {
if(provider.equalsIgnoreCase("WeChatOpen")) {
return ((AuthUser)authResponse.getData()).getUuid();
}else if(provider.equalsIgnoreCase("sinaweibo")) {