Browse Source

Merge branch 'master' of github.com:/hs-web/hsweb-framework

zhouhao 7 years ago
parent
commit
56ff18fec4

+ 0 - 1
hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/request/OAuth2Session.java

@@ -18,7 +18,6 @@
 
 package org.hswebframework.web.authorization.oauth2.client.request;
 
-import org.hswebframework.web.authorization.annotation.Authorize;
 import org.hswebframework.web.authorization.oauth2.client.AccessTokenInfo;
 
 import java.io.Serializable;

+ 10 - 0
hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-resource-server/pom.xml

@@ -40,5 +40,15 @@
             <artifactId>hsweb-authorization-oauth2-core</artifactId>
             <version>${project.version}</version>
         </dependency>
+        <dependency>
+            <groupId>org.hswebframework.web</groupId>
+            <artifactId>hsweb-authorization-api</artifactId>
+            <version>${project.version}</version>
+        </dependency>
+        <dependency>
+            <groupId>org.hswebframework.web</groupId>
+            <artifactId>hsweb-authorization-oauth2-client</artifactId>
+            <version>${project.version}</version>
+        </dependency>
     </dependencies>
 </project>

+ 1 - 1
hsweb-examples/hsweb-examples-oauth2/hsweb-examples-oauth2-client/pom.xml

@@ -96,7 +96,7 @@
 
         <dependency>
             <groupId>org.hswebframework.web</groupId>
-            <artifactId>hsweb-authorization-shiro</artifactId>
+            <artifactId>hsweb-authorization-basic</artifactId>
             <version>${project.version}</version>
         </dependency>
         <dependency>

+ 4 - 2
hsweb-examples/hsweb-examples-oauth2/hsweb-examples-oauth2-client/src/main/java/org/hswebframework/web/example/oauth2/OAuth2ClientApplication.java

@@ -23,7 +23,7 @@ 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.shiro.oauth2sso.OAuth2SSOAuthorizingListener;
+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;
@@ -89,6 +89,8 @@ public class OAuth2ClientApplication implements CommandLineRunner {
     @Autowired
     OAuth2RequestService      oAuth2RequestService;
 
+    @Autowired
+    UserTokenManager userTokenManager;
     @Override
     public void run(String... strings) throws Exception {
         OAuth2ServerConfigEntity entity = entityFactory.newInstance(OAuth2ServerConfigEntity.class);
@@ -108,7 +110,7 @@ public class OAuth2ClientApplication implements CommandLineRunner {
         //add
         serverConfigService.insert(entity);
 
-        OAuth2SSOAuthorizingListener listener = new OAuth2SSOAuthorizingListener(oAuth2RequestService, entity.getId());
+        OAuth2SSOAuthorizingListener listener = new OAuth2SSOAuthorizingListener(oAuth2RequestService, entity.getId(),userTokenManager);
 
         oAuth2RequestService.registerListener(entity.getId(), listener);
     }

+ 79 - 0
hsweb-examples/hsweb-examples-oauth2/hsweb-examples-oauth2-client/src/main/java/org/hswebframework/web/example/oauth2/OAuth2SSOAuthorizingListener.java

@@ -0,0 +1,79 @@
+/*
+ *  Copyright 2016 http://www.hswebframework.org
+ *
+ *  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.hswebframework.web.example.oauth2;
+
+import org.hswebframework.web.WebUtil;
+import org.hswebframework.web.authorization.Authentication;
+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.oauth2.client.response.OAuth2Response;
+import org.hswebframework.web.authorization.token.UserTokenManager;
+
+import javax.servlet.http.HttpSession;
+
+/**
+ * @author zhouhao
+ */
+public class OAuth2SSOAuthorizingListener
+        implements OAuth2Listener<OAuth2CodeAuthBeforeEvent> {
+
+    private OAuth2RequestService oAuth2RequestService;
+
+    private UserTokenManager userTokenManager;
+
+
+    private String userCenterServerId;
+
+    private String userAuthInfoApi = "oauth2/user-auth-info";
+
+    public OAuth2SSOAuthorizingListener(OAuth2RequestService oAuth2RequestService, String userCenterServerId,UserTokenManager userTokenManager) {
+        this.oAuth2RequestService = oAuth2RequestService;
+        this.userCenterServerId = userCenterServerId;
+        this.userTokenManager=userTokenManager;
+    }
+
+    public void setUserAuthInfoApi(String userAuthInfoApi) {
+        this.userAuthInfoApi = userAuthInfoApi;
+    }
+
+    public void setUserCenterServerId(String userCenterServerId) {
+        this.userCenterServerId = userCenterServerId;
+    }
+
+    @Override
+    public void on(OAuth2CodeAuthBeforeEvent event) {
+        OAuth2Session session = oAuth2RequestService
+                .create(userCenterServerId)
+                .byAuthorizationCode(event.getCode())
+                .authorize();
+
+        Authentication authentication = session
+                .request(userAuthInfoApi)
+                .get().onError(OAuth2Response.throwOnError)
+                .as(Authentication.class);
+
+        HttpSession httpSession= WebUtil.getHttpServletRequest().getSession();
+
+        userTokenManager.signIn(httpSession.getId(),authentication.getUser().getId());
+
+
+    }
+}

+ 1 - 1
hsweb-examples/hsweb-examples-oauth2/hsweb-examples-oauth2-server/pom.xml

@@ -94,7 +94,7 @@
         </dependency>
         <dependency>
             <groupId>org.hswebframework.web</groupId>
-            <artifactId>hsweb-authorization-shiro</artifactId>
+            <artifactId>hsweb-authorization-basic</artifactId>
             <version>${project.version}</version>
         </dependency>
         <dependency>