From c17a67bb6cb8a4e341353c871b947aa2a8b8c43c Mon Sep 17 00:00:00 2001 From: zhou-hao Date: Fri, 1 Dec 2017 20:18:23 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BD=BF=E7=94=A8github?= =?UTF-8?q?=E7=99=BB=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../oauth2/GithubSSOAuthorizingListener.java | 75 ++++++++++++ .../oauth2/MemoryAuthenticationManager.java | 26 +++++ .../oauth2/OAuth2ClientApplication.java | 107 +++++++++--------- .../oauth2/OAuth2SSOAuthorizingListener.java | 6 +- .../src/main/resources/static/login.html | 32 ++---- 5 files changed, 167 insertions(+), 79 deletions(-) create mode 100644 hsweb-examples/hsweb-examples-oauth2/hsweb-examples-oauth2-client/src/main/java/org/hswebframework/web/example/oauth2/GithubSSOAuthorizingListener.java create mode 100644 hsweb-examples/hsweb-examples-oauth2/hsweb-examples-oauth2-client/src/main/java/org/hswebframework/web/example/oauth2/MemoryAuthenticationManager.java diff --git a/hsweb-examples/hsweb-examples-oauth2/hsweb-examples-oauth2-client/src/main/java/org/hswebframework/web/example/oauth2/GithubSSOAuthorizingListener.java b/hsweb-examples/hsweb-examples-oauth2/hsweb-examples-oauth2-client/src/main/java/org/hswebframework/web/example/oauth2/GithubSSOAuthorizingListener.java new file mode 100644 index 000000000..cf73eb32d --- /dev/null +++ b/hsweb-examples/hsweb-examples-oauth2/hsweb-examples-oauth2-client/src/main/java/org/hswebframework/web/example/oauth2/GithubSSOAuthorizingListener.java @@ -0,0 +1,75 @@ +package org.hswebframework.web.example.oauth2; + +import org.hswebframework.web.WebUtil; +import org.hswebframework.web.authorization.Authentication; +import org.hswebframework.web.authorization.basic.web.SessionIdUserTokenGenerator; +import org.hswebframework.web.authorization.basic.web.UserTokenGenerator; +import org.hswebframework.web.authorization.oauth2.client.OAuth2RequestService; +import org.hswebframework.web.authorization.oauth2.client.listener.OAuth2CodeAuthBeforeEvent; +import org.hswebframework.web.authorization.oauth2.client.listener.OAuth2Listener; +import org.hswebframework.web.authorization.oauth2.client.request.OAuth2Session; +import org.hswebframework.web.authorization.simple.SimpleAuthentication; +import org.hswebframework.web.authorization.simple.SimplePermission; +import org.hswebframework.web.authorization.simple.SimpleRole; +import org.hswebframework.web.authorization.simple.SimpleUser; +import org.hswebframework.web.authorization.simple.builder.SimpleAuthenticationBuilder; +import org.hswebframework.web.authorization.simple.builder.SimpleDataAccessConfigBuilderFactory; +import org.hswebframework.web.authorization.token.UserTokenManager; + +import javax.servlet.http.HttpSession; +import java.io.Serializable; +import java.util.Arrays; +import java.util.HashSet; +import java.util.Map; + +public class GithubSSOAuthorizingListener + implements OAuth2Listener { + + + private OAuth2RequestService oAuth2RequestService; + + private UserTokenManager userTokenManager; + + private String userCenterServerId; + + private String userAuthInfoApi = "/user"; + + public GithubSSOAuthorizingListener(OAuth2RequestService oAuth2RequestService, String userCenterServerId, UserTokenManager userTokenManager) { + this.oAuth2RequestService = oAuth2RequestService; + this.userCenterServerId = userCenterServerId; + this.userTokenManager = userTokenManager; + } + + @Override + @SuppressWarnings("all") + public void on(OAuth2CodeAuthBeforeEvent event) { + String code = event.getCode(); + + Map userInfo = oAuth2RequestService + .create(userCenterServerId) + .byAuthorizationCode(code) + .authorize() + .request(userAuthInfoApi) + .get() + .as(Map.class); + + String name = String.valueOf(userInfo.get("name")); + String id = String.valueOf(userInfo.get("id")); + String bio = String.valueOf(userInfo.get("bio")); + + Authentication authentication = new SimpleAuthenticationBuilder(new SimpleDataAccessConfigBuilderFactory()) + .user(SimpleUser.builder().username(bio).name(name).id("github-user:" + id).build()) + .role(Arrays.asList(SimpleRole.builder().id("github-user").name("github用户").build())) + .permission(Arrays.asList(SimplePermission.builder().id("user-info").actions(new HashSet<>(Arrays.asList("get"))).build())) + .attributes((Map) userInfo) + .build(); + + MemoryAuthenticationManager.addAuthentication(authentication); + + HttpSession session = WebUtil.getHttpServletRequest().getSession(); + + userTokenManager.signIn(session.getId(), UserTokenGenerator.TOKEN_TYPE_SESSION_ID, authentication.getUser().getId(), -1); + + + } +} diff --git a/hsweb-examples/hsweb-examples-oauth2/hsweb-examples-oauth2-client/src/main/java/org/hswebframework/web/example/oauth2/MemoryAuthenticationManager.java b/hsweb-examples/hsweb-examples-oauth2/hsweb-examples-oauth2-client/src/main/java/org/hswebframework/web/example/oauth2/MemoryAuthenticationManager.java new file mode 100644 index 000000000..45616d70b --- /dev/null +++ b/hsweb-examples/hsweb-examples-oauth2/hsweb-examples-oauth2-client/src/main/java/org/hswebframework/web/example/oauth2/MemoryAuthenticationManager.java @@ -0,0 +1,26 @@ +package org.hswebframework.web.example.oauth2; + +import org.hswebframework.web.authorization.Authentication; +import org.hswebframework.web.authorization.AuthenticationManager; + +import java.util.HashMap; +import java.util.Map; + +public class MemoryAuthenticationManager implements AuthenticationManager { + static Map users = new HashMap<>(); + + public static void addAuthentication(Authentication authentication) { + users.put(authentication.getUser().getId(), authentication); + } + + @Override + public Authentication getByUserId(String userId) { + + return users.get(userId); + } + + @Override + public Authentication sync(Authentication authentication) { + return authentication; + } +} diff --git a/hsweb-examples/hsweb-examples-oauth2/hsweb-examples-oauth2-client/src/main/java/org/hswebframework/web/example/oauth2/OAuth2ClientApplication.java b/hsweb-examples/hsweb-examples-oauth2/hsweb-examples-oauth2-client/src/main/java/org/hswebframework/web/example/oauth2/OAuth2ClientApplication.java index fb6cc050c..8f29cd145 100644 --- a/hsweb-examples/hsweb-examples-oauth2/hsweb-examples-oauth2-client/src/main/java/org/hswebframework/web/example/oauth2/OAuth2ClientApplication.java +++ b/hsweb-examples/hsweb-examples-oauth2/hsweb-examples-oauth2-client/src/main/java/org/hswebframework/web/example/oauth2/OAuth2ClientApplication.java @@ -18,16 +18,14 @@ package org.hswebframework.web.example.oauth2; -import org.hswebframework.web.authorization.Authentication; -import org.hswebframework.web.authorization.AuthenticationManager; import org.hswebframework.web.authorization.oauth2.client.OAuth2RequestService; -import org.hswebframework.web.authorization.oauth2.client.request.OAuth2Session; -import org.hswebframework.web.authorization.oauth2.client.response.OAuth2Response; +import org.hswebframework.web.authorization.oauth2.client.OAuth2ServerConfig; +import org.hswebframework.web.authorization.oauth2.client.simple.OAuth2ServerConfigRepository; +import org.hswebframework.web.authorization.oauth2.client.simple.provider.github.GithubResponseConvert; +import org.hswebframework.web.authorization.oauth2.client.simple.provider.github.GithubResponseJudge; import org.hswebframework.web.authorization.token.UserTokenManager; import org.hswebframework.web.commons.entity.DataStatus; import org.hswebframework.web.commons.entity.factory.EntityFactory; -import org.hswebframework.web.entity.oauth2.client.OAuth2ServerConfigEntity; -import org.hswebframework.web.service.oauth2.client.OAuth2ServerConfigService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; @@ -37,9 +35,6 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.EnableAspectJAutoProxy; -import java.util.HashMap; -import java.util.Map; - /** * TODO 完成注释 * @@ -56,63 +51,71 @@ public class OAuth2ClientApplication implements CommandLineRunner { } @Bean - public AuthenticationManager authenticationManager() { - // 由于没有使用用户管理, - // 而且暂时没有实现默认的OAuth2相关的权限获取策略, - // 所以这里使用通过OAuth2进行获取 - // 实现类似sso的功能,这里实际上应该将权限信息存储起来 - Map sessionMap = new HashMap<>(); + public GithubResponseConvert githubResponseConvert() { + return new GithubResponseConvert(); + } - return new AuthenticationManager() { - @Override - public Authentication getByUserId(String userId) { - //获取远程的用户权限信息 - return sessionMap.computeIfAbsent("auth", key -> oAuth2RequestService.create("hsweb-oauth-server") - .byClientCredentials()) - .request("oauth2/user-auth-info/" + userId) - .get().onError(OAuth2Response.throwOnError) - .as(Authentication.class); - } + @Bean + public GithubResponseJudge githubResponseJudge() { + return new GithubResponseJudge(); + } - @Override - public Authentication sync(Authentication authentication) { - //暂时不支持 - return authentication; - } - }; + @Bean + public MemoryAuthenticationManager memoryAuthenticationManager() { + return new MemoryAuthenticationManager(); } @Autowired - EntityFactory entityFactory; + EntityFactory entityFactory; @Autowired - OAuth2ServerConfigService serverConfigService; + OAuth2ServerConfigRepository repository; @Autowired - OAuth2RequestService oAuth2RequestService; + OAuth2RequestService oAuth2RequestService; @Autowired UserTokenManager userTokenManager; + @Override public void run(String... strings) throws Exception { - OAuth2ServerConfigEntity entity = entityFactory.newInstance(OAuth2ServerConfigEntity.class); - entity.setId("hsweb-oauth-server"); - entity.setName("hsweb OAuth2"); - //可以修改hosts文件改为域名 - entity.setApiBaseUrl("http://localhost:8080/"); - entity.setAuthUrl("oauth2/login.html"); - entity.setAccessTokenUrl("oauth2/token"); - //和服务端创建的一致 - entity.setClientId("hsweb_oauth2_example"); - entity.setClientSecret("hsweb_oauth2_example_secret"); - entity.setRedirectUri("http://localhost:8808/"); - //hsweb - entity.setProvider("hsweb"); - entity.setStatus(DataStatus.STATUS_ENABLED); - //add - serverConfigService.insert(entity); + //github + OAuth2ServerConfig github = OAuth2ServerConfig.builder() + .id("github") + .name("github test") + .clientId("b9cd11eae646a5a5c4bf") + .clientSecret("6b664ebfc051f5919589ccd20cc9e774b026f6f5") + .apiBaseUrl("https://api.github.com/") + .authUrl("https://github.com/login/oauth/authorize") + .accessTokenUrl("https://github.com/login/oauth/access_token") + .redirectUri("http://localhost:8808/") + .provider("github") + .status(DataStatus.STATUS_ENABLED) + .build(); + repository.save(github); - OAuth2SSOAuthorizingListener listener = new OAuth2SSOAuthorizingListener(oAuth2RequestService, entity.getId(),userTokenManager); - oAuth2RequestService.registerListener(entity.getId(), listener); + OAuth2ServerConfig hsweb = OAuth2ServerConfig.builder() + .id("hsweb-oauth-server") + .name("hsweb OAuth2") + .clientId("hsweb_oauth2_example") + .clientSecret("hsweb_oauth2_example_secret") + .apiBaseUrl("http://localhost:8080/") + .authUrl("oauth2/login.html") + .accessTokenUrl("oauth2/token") + .redirectUri("http://localhost:8808/") + .provider("hsweb") + .status(DataStatus.STATUS_ENABLED) + .build(); + + repository.save(hsweb); + + + OAuth2SSOAuthorizingListener listener = new OAuth2SSOAuthorizingListener(oAuth2RequestService, hsweb.getId(), userTokenManager); + + GithubSSOAuthorizingListener githubSSOAuthorizingListener = + new GithubSSOAuthorizingListener(oAuth2RequestService, github.getId(), userTokenManager); + + oAuth2RequestService.registerListener(hsweb.getId(), listener); + oAuth2RequestService.registerListener(github.getId(), githubSSOAuthorizingListener); } diff --git a/hsweb-examples/hsweb-examples-oauth2/hsweb-examples-oauth2-client/src/main/java/org/hswebframework/web/example/oauth2/OAuth2SSOAuthorizingListener.java b/hsweb-examples/hsweb-examples-oauth2/hsweb-examples-oauth2-client/src/main/java/org/hswebframework/web/example/oauth2/OAuth2SSOAuthorizingListener.java index 9baa68847..452765288 100644 --- a/hsweb-examples/hsweb-examples-oauth2/hsweb-examples-oauth2-client/src/main/java/org/hswebframework/web/example/oauth2/OAuth2SSOAuthorizingListener.java +++ b/hsweb-examples/hsweb-examples-oauth2/hsweb-examples-oauth2-client/src/main/java/org/hswebframework/web/example/oauth2/OAuth2SSOAuthorizingListener.java @@ -70,8 +70,10 @@ public class OAuth2SSOAuthorizingListener .get().onError(OAuth2Response.throwOnError) .as(Authentication.class); - HttpSession httpSession = WebUtil.getHttpServletRequest() - .getSession(); + //添加用户信息 + MemoryAuthenticationManager.addAuthentication(authentication); + + HttpSession httpSession = WebUtil.getHttpServletRequest().getSession(); userTokenManager.signIn(httpSession.getId(), "sessionId",authentication.getUser().getId(), 60 * 60 * 1000L); diff --git a/hsweb-examples/hsweb-examples-oauth2/hsweb-examples-oauth2-client/src/main/resources/static/login.html b/hsweb-examples/hsweb-examples-oauth2/hsweb-examples-oauth2-client/src/main/resources/static/login.html index fd2903d1c..92460538e 100644 --- a/hsweb-examples/hsweb-examples-oauth2/hsweb-examples-oauth2-client/src/main/resources/static/login.html +++ b/hsweb-examples/hsweb-examples-oauth2/hsweb-examples-oauth2-client/src/main/resources/static/login.html @@ -25,26 +25,20 @@ - - + +   + \ No newline at end of file