瀏覽代碼

优化oauth2 server

zhouhao 7 年之前
父節點
當前提交
69e987e19e

+ 15 - 0
hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-auth-server/src/main/java/org/hswebframework/web/authorization/oauth2/server/event/OAuth2GrantEvent.java

@@ -0,0 +1,15 @@
+package org.hswebframework.web.authorization.oauth2.server.event;
+
+import lombok.AllArgsConstructor;
+import lombok.Getter;
+import org.hswebframework.web.authorization.oauth2.server.OAuth2AccessToken;
+
+/**
+ * @author zhouhao
+ * @since 1.0
+ */
+@Getter
+@AllArgsConstructor
+public class OAuth2GrantEvent {
+    private OAuth2AccessToken accessToken;
+}

+ 7 - 0
hsweb-system/hsweb-system-oauth2-server/hsweb-system-oauth2-server-controller/src/main/java/org/hswebframework/web/authorization/oauth2/controller/OAuth2AuthorizeController.java

@@ -25,6 +25,7 @@ import org.hswebframework.web.authorization.Authentication;
 import org.hswebframework.web.authorization.annotation.Authorize;
 import org.hswebframework.web.authorization.exception.UnAuthorizedException;
 import org.hswebframework.web.authorization.oauth2.server.OAuth2AccessToken;
+import org.hswebframework.web.authorization.oauth2.server.event.OAuth2GrantEvent;
 import org.hswebframework.web.authorization.oauth2.server.support.OAuth2Granter;
 import org.hswebframework.web.authorization.oauth2.server.support.code.AuthorizationCodeRequest;
 import org.hswebframework.web.authorization.oauth2.server.support.code.AuthorizationCodeService;
@@ -35,6 +36,8 @@ import org.hswebframework.web.oauth2.core.GrantType;
 import org.hswebframework.web.oauth2.core.OAuth2Constants;
 import org.hswebframework.web.oauth2.model.AuthorizationCodeModel;
 import org.hswebframework.web.oauth2.model.ImplicitAccessTokenModel;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.ApplicationEventPublisher;
 import org.springframework.web.bind.annotation.*;
 
 import javax.annotation.Resource;
@@ -54,6 +57,9 @@ public class OAuth2AuthorizeController {
     @Resource
     private OAuth2Granter oAuth2Granter;
 
+    @Autowired
+    private ApplicationEventPublisher publisher;
+
     @GetMapping(params = "response_type=code")
     @ApiOperation("获取当前登录用户OAuth2.0授权码")
     @Authorize
@@ -86,6 +92,7 @@ public class OAuth2AuthorizeController {
 
         ImplicitRequest implicitRequest = new HttpImplicitRequest(request);
         OAuth2AccessToken accessToken = oAuth2Granter.grant(GrantType.implicit, implicitRequest);
+        publisher.publishEvent(new OAuth2GrantEvent(accessToken));
 
         ImplicitAccessTokenModel model = new ImplicitAccessTokenModel();
         model.setState(state);

+ 6 - 0
hsweb-system/hsweb-system-oauth2-server/hsweb-system-oauth2-server-controller/src/main/java/org/hswebframework/web/authorization/oauth2/controller/OAuth2TokenController.java

@@ -20,6 +20,7 @@ package org.hswebframework.web.authorization.oauth2.controller;
 
 import io.swagger.annotations.*;
 import org.hswebframework.web.authorization.oauth2.server.OAuth2AccessToken;
+import org.hswebframework.web.authorization.oauth2.server.event.OAuth2GrantEvent;
 import org.hswebframework.web.authorization.oauth2.server.exception.GrantTokenException;
 import org.hswebframework.web.authorization.oauth2.server.support.OAuth2Granter;
 import org.hswebframework.web.authorization.oauth2.server.support.client.HttpClientCredentialRequest;
@@ -31,6 +32,8 @@ import org.hswebframework.web.oauth2.core.ErrorType;
 import org.hswebframework.web.oauth2.core.GrantType;
 import org.hswebframework.web.oauth2.core.OAuth2Constants;
 import org.hswebframework.web.oauth2.model.AccessTokenModel;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.ApplicationEventPublisher;
 import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestParam;
@@ -51,6 +54,8 @@ public class OAuth2TokenController {
     @Resource
     private OAuth2Granter oAuth2Granter;
 
+    @Autowired
+    private ApplicationEventPublisher publisher;
     @PostMapping
     @ApiOperation(value = "申请token", notes = "具体请求方式请参照: http://www.ruanyifeng.com/blog/2014/05/oauth_2_0.html")
     @ApiImplicitParams(
@@ -88,6 +93,7 @@ public class OAuth2TokenController {
             default:
                 ErrorType.UNSUPPORTED_GRANT_TYPE.throwThis(GrantTokenException::new);
         }
+        publisher.publishEvent(new OAuth2GrantEvent(accessToken));
         return entityToModel(accessToken);
     }
 

+ 26 - 0
hsweb-system/hsweb-system-oauth2-server/hsweb-system-oauth2-server-starter/src/main/java/org/hswebframework/web/oauth2/OAuth2GrantEventListener.java

@@ -0,0 +1,26 @@
+package org.hswebframework.web.oauth2;
+
+import lombok.AllArgsConstructor;
+import org.hswebframework.web.authorization.oauth2.server.event.OAuth2GrantEvent;
+import org.hswebframework.web.authorization.token.UserTokenManager;
+import org.springframework.context.event.EventListener;
+
+/**
+ * @author zhouhao
+ * @since 1.0
+ */
+@AllArgsConstructor
+public class OAuth2GrantEventListener {
+
+    private UserTokenManager userTokenManager;
+
+    @EventListener
+    public void handleOAuth2GrantEvent(OAuth2GrantEvent event) {
+        userTokenManager.signIn(
+                event.getAccessToken().getAccessToken(),
+                "oauth2-access-token",
+                event.getAccessToken().getOwnerId(),
+                event.getAccessToken().getExpiresIn() * 1000L);
+
+    }
+}

+ 11 - 3
hsweb-system/hsweb-system-oauth2-server/hsweb-system-oauth2-server-starter/src/main/java/org/hswebframework/web/oauth2/OAuth2GranterAutoConfiguration.java

@@ -34,6 +34,7 @@ import org.hswebframework.web.authorization.oauth2.server.support.password.Passw
 import org.hswebframework.web.authorization.oauth2.server.support.refresh.DefaultRefreshTokenGranter;
 import org.hswebframework.web.authorization.oauth2.server.support.refresh.RefreshTokenGranter;
 import org.hswebframework.web.authorization.oauth2.server.token.AccessTokenService;
+import org.hswebframework.web.authorization.token.UserTokenManager;
 import org.hswebframework.web.commons.entity.factory.EntityFactory;
 import org.hswebframework.web.dao.oauth2.AuthorizationCodeDao;
 import org.hswebframework.web.dao.oauth2.OAuth2AccessDao;
@@ -43,6 +44,7 @@ import org.hswebframework.web.service.oauth2.server.simple.*;
 import org.springframework.beans.BeansException;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.config.BeanPostProcessor;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
 import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
 import org.springframework.boot.context.properties.ConfigurationProperties;
 import org.springframework.context.annotation.Bean;
@@ -91,16 +93,22 @@ public class OAuth2GranterAutoConfiguration {
                 .setTokenGenerator(tokenGenerator);
     }
 
+    @Bean
+    @ConditionalOnBean(UserTokenManager.class)
+    public OAuth2GrantEventListener oAuth2GrantEventListener(UserTokenManager userTokenManager) {
+        return new OAuth2GrantEventListener(userTokenManager);
+    }
+
     @Configuration
     public static class OAuth2GranterConfiguration {
         @Autowired
-        private AuthorizationCodeService authorizationCodeService;
+        private AuthorizationCodeService     authorizationCodeService;
         @Autowired
         private OAuth2ClientConfigRepository oAuth2ClientConfigRepository;
         @Autowired
-        private AccessTokenService       accessTokenService;
+        private AccessTokenService           accessTokenService;
         @Autowired
-        private PasswordService          passwordService;
+        private PasswordService              passwordService;
 
         private <T extends AbstractAuthorizationService> T setProperty(T abstractAuthorizationService) {
             abstractAuthorizationService.setAccessTokenService(accessTokenService);

+ 1 - 2
hsweb-system/hsweb-system-oauth2-server/hsweb-system-oauth2-server-starter/src/main/java/org/hswebframework/web/oauth2/authorization/OAuth2UserTokenParser.java

@@ -28,14 +28,13 @@ public class OAuth2UserTokenParser implements UserTokenParser {
 
     @Override
     public ParsedToken parseToken(HttpServletRequest request) {
-        if (request.getRequestURI().contains("oauth2")&&request.getParameter(OAuth2Constants.grant_type) != null) {
+        if (request.getRequestURI().contains("oauth2") && request.getParameter(OAuth2Constants.grant_type) != null) {
             return null;
         }
         String accessToken = request.getHeader(OAuth2Constants.authorization);
         if (StringUtils.isEmpty(accessToken)) {
             accessToken = request.getParameter(OAuth2Constants.access_token);
         } else {
-
             String[] arr = accessToken.split("[ ]");
             if (arr.length > 1) {
                 accessToken = arr[1];