zhouhao 8 роки тому
батько
коміт
a3d03bf0d5

+ 5 - 0
hsweb-web-oauth2/hsweb-web-oauth2-controller/pom.xml

@@ -38,9 +38,14 @@
             <groupId>org.apache.oltu.oauth2</groupId>
             <artifactId>org.apache.oltu.oauth2.authzserver</artifactId>
         </dependency>
+        <dependency>
+            <groupId>org.hsweb</groupId>
+            <artifactId>hsweb-web-controller</artifactId>
+        </dependency>
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-web</artifactId>
+            <optional>true</optional>
         </dependency>
         <dependency>
             <groupId>org.hsweb</groupId>

+ 1 - 0
hsweb-web-oauth2/hsweb-web-oauth2-controller/src/main/java/org/hsweb/web/oauth2/controller/OAuth2Controller.java

@@ -92,6 +92,7 @@ public class OAuth2Controller {
                 oAuth2Service.addAccessToken(access);
                 OAuthResponse response = OAuthASResponse
                         .tokenResponse(HttpServletResponse.SC_OK)
+                        .setTokenType("bearer")
                         .setAccessToken(accessToken)
                         .setExpiresIn(String.valueOf(oAuth2Service.getDefaultExpireIn()))
                         .setRefreshToken(refreshToken)

+ 6 - 1
hsweb-web-oauth2/hsweb-web-oauth2-core/pom.xml

@@ -27,5 +27,10 @@
 
     <artifactId>hsweb-web-oauth2-core</artifactId>
 
-
+    <dependencies>
+        <dependency>
+            <groupId>org.hsweb</groupId>
+            <artifactId>hsweb-web-service-interface</artifactId>
+        </dependency>
+    </dependencies>
 </project>

+ 4 - 0
hsweb-web-oauth2/hsweb-web-oauth2-core/src/main/java/org/hsweb/web/oauth2/po/OAuth2Access.java

@@ -145,4 +145,8 @@ public class OAuth2Access extends GenericPo<String> {
     public void setUser(User user) {
         this.user = user;
     }
+
+    public long getLeftTime() {
+        return getExpireIn() - (System.currentTimeMillis() - getCreateDate().getTime()) / 1000;
+    }
 }

+ 16 - 0
hsweb-web-oauth2/hsweb-web-oauth2-core/src/main/java/org/hsweb/web/oauth2/service/OAuth2ClientService.java

@@ -16,12 +16,28 @@
 
 package org.hsweb.web.oauth2.service;
 
+import org.hsweb.web.bean.common.PagerResult;
+import org.hsweb.web.bean.common.QueryParam;
+import org.hsweb.web.oauth2.po.OAuth2Access;
 import org.hsweb.web.oauth2.po.OAuth2Client;
 import org.hsweb.web.service.GenericService;
 
+import java.util.List;
+
 /**
  * OAuth2客户端服务类
  * Created by generator
  */
 public interface OAuth2ClientService extends GenericService<OAuth2Client, String> {
+
+    PagerResult<OAuth2Access> selectAccessList(QueryParam param);
+
+    int deleteAccess(String accessId);
+
+    String refreshSecret(String clientId);
+
+    void enable(String id);
+
+    void disable(String id);
+
 }

+ 63 - 0
hsweb-web-oauth2/hsweb-web-oauth2-service-common/src/main/java/org/hsweb/web/oauth2/service/OAuth2ClientServiceImpl.java

@@ -16,12 +16,22 @@
 
 package org.hsweb.web.oauth2.service;
 
+import org.hsweb.commons.MD5;
+import org.hsweb.web.bean.common.PagerResult;
+import org.hsweb.web.bean.common.QueryParam;
+import org.hsweb.web.bean.common.UpdateMapParam;
+import org.hsweb.web.bean.common.UpdateParam;
+import org.hsweb.web.core.exception.NotFoundException;
+import org.hsweb.web.core.utils.RandomUtil;
+import org.hsweb.web.oauth2.dao.OAuth2AccessMapper;
 import org.hsweb.web.oauth2.dao.OAuth2ClientMapper;
+import org.hsweb.web.oauth2.po.OAuth2Access;
 import org.hsweb.web.oauth2.po.OAuth2Client;
 import org.hsweb.web.service.impl.AbstractServiceImpl;
 import org.springframework.stereotype.Service;
 
 import javax.annotation.Resource;
+import java.util.UUID;
 
 @Service("oAuth2ClientService")
 public class OAuth2ClientServiceImpl extends AbstractServiceImpl<OAuth2Client, String> implements OAuth2ClientService {
@@ -29,8 +39,61 @@ public class OAuth2ClientServiceImpl extends AbstractServiceImpl<OAuth2Client, S
     @Resource
     private OAuth2ClientMapper oAuth2ClientMapper;
 
+    @Resource
+    private OAuth2AccessMapper oAuth2AccessMapper;
+
     @Override
     protected OAuth2ClientMapper getMapper() {
         return oAuth2ClientMapper;
     }
+
+    @Override
+    public PagerResult<OAuth2Access> selectAccessList(QueryParam param) {
+        PagerResult<OAuth2Access> pagerResult = new PagerResult<>();
+        param.setPaging(false);
+        int total = getMapper().total(param);
+        pagerResult.setTotal(total);
+        param.rePaging(total);
+        pagerResult.setData(oAuth2AccessMapper.select(param));
+        return pagerResult;
+    }
+
+    @Override
+    public int deleteAccess(String accessId) {
+        return oAuth2AccessMapper.deleteById(accessId);
+    }
+
+    @Override
+    public String insert(OAuth2Client data) {
+        data.setSecret(MD5.encode(UUID.randomUUID().toString() + Math.random()));
+        data.setStatus(1);
+        return super.insert(data);
+    }
+
+    @Override
+    public String refreshSecret(String clientId) {
+        String secret = MD5.encode(UUID.randomUUID().toString() + Math.random());
+        int size = oAuth2ClientMapper.update((UpdateParam) UpdateMapParam.build().set("secret", secret).where("id", clientId));
+        if (size != 1) throw new NotFoundException("客户端不存在");
+        return secret;
+    }
+
+    @Override
+    public void enable(String id) {
+        OAuth2Client old = selectByPk(id);
+        assertNotNull(old, "客户端不存在");
+        oAuth2ClientMapper.update((UpdateParam) UpdateMapParam.build().set("status", 1).where("id", id));
+    }
+
+    @Override
+    public void disable(String id) {
+        OAuth2Client old = selectByPk(id);
+        assertNotNull(old, "客户端不存在");
+        oAuth2ClientMapper.update((UpdateParam) UpdateMapParam.build().set("status", -1).where("id", id));
+    }
+
+    @Override
+    public int update(OAuth2Client data) {
+        return getMapper().update(UpdateParam.build(data).excludes("secret","status").where("id", data.getId()));
+    }
 }

+ 1 - 3
hsweb-web-oauth2/hsweb-web-oauth2-service-common/src/main/java/org/hsweb/web/oauth2/service/OAuth2ServiceImpl.java

@@ -117,10 +117,8 @@ public class OAuth2ServiceImpl implements OAuth2Service {
         if (auth2Access == null) {
             return null;
         }
-        long createTime = auth2Access.getCreateDate().getTime();
-        long expireInMs = auth2Access.getExpireIn() * 60 * 1000;
         //判断是否已超时
-        if (System.currentTimeMillis() - createTime > expireInMs) {
+        if (auth2Access.getLeftTime() <= 0) {
             if (cache != null) {
                 cache.evict(cacheKey);
             }

+ 0 - 1
hsweb-web-oauth2/hsweb-web-oauth2-simple/src/main/java/org/hsweb/web/oauth2/simple/SimpleOAuth2Manager.java

@@ -16,7 +16,6 @@
 
 package org.hsweb.web.oauth2.simple;
 
-import org.apache.oltu.oauth2.as.request.OAuthAuthzRequest;
 import org.hsweb.web.bean.po.user.User;
 import org.hsweb.web.core.authorize.oauth2.OAuth2Manager;
 import org.hsweb.web.oauth2.service.OAuth2Service;

+ 4 - 3
hsweb-web-oauth2/pom.xml

@@ -38,9 +38,10 @@
     <dependencies>
 
         <dependency>
-            <groupId>org.hsweb</groupId>
-            <artifactId>hsweb-web-service-interface</artifactId>
+            <groupId>javax.servlet</groupId>
+            <artifactId>servlet-api</artifactId>
+            <version>2.5</version>
+            <scope>provided</scope>
         </dependency>
-
     </dependencies>
 </project>