|
@@ -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<String, OAuth2Session> sessionMap = new HashMap<>();
|
|
|
-
|
|
|
- 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);
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public Authentication sync(Authentication authentication) {
|
|
|
- //暂时不支持
|
|
|
- return authentication;
|
|
|
- }
|
|
|
- };
|
|
|
+ public GithubResponseConvert githubResponseConvert() {
|
|
|
+ return new GithubResponseConvert();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Bean
|
|
|
+ public GithubResponseJudge githubResponseJudge() {
|
|
|
+ return new GithubResponseJudge();
|
|
|
+ }
|
|
|
+
|
|
|
+ @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);
|
|
|
-
|
|
|
- OAuth2SSOAuthorizingListener listener = new OAuth2SSOAuthorizingListener(oAuth2RequestService, entity.getId(),userTokenManager);
|
|
|
-
|
|
|
- oAuth2RequestService.registerListener(entity.getId(), listener);
|
|
|
+ //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);
|
|
|
+
|
|
|
+
|
|
|
+ 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);
|
|
|
}
|
|
|
|
|
|
|