소스 검색

优化jwt

zhouhao 7 년 전
부모
커밋
851e66fbe2

+ 1 - 1
hsweb-authorization/hsweb-authorization-basic/src/main/java/org/hswebframework/web/authorization/basic/web/SessionIdUserTokenGenerator.java

@@ -24,7 +24,7 @@ public class SessionIdUserTokenGenerator implements UserTokenGenerator ,Serializ
         if(null==request)throw new UnsupportedOperationException();
 
 
-        int timeout =request.getSession().getMaxInactiveInterval();
+        int timeout =request.getSession().getMaxInactiveInterval()*1000;
 
         String sessionId = request.getSession().getId();
 

+ 28 - 0
hsweb-authorization/hsweb-authorization-jwt/src/main/java/org/hswebframework/web/authorization/jwt/JwtAutoConfiguration.java

@@ -0,0 +1,28 @@
+package org.hswebframework.web.authorization.jwt;
+
+import org.springframework.boot.context.properties.ConfigurationProperties;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+/**
+ * @author zhouhao
+ */
+@Configuration
+public class JwtAutoConfiguration  {
+
+    @Bean
+    @ConfigurationProperties(prefix = "hsweb.authorize.jwt")
+    public JwtConfig jwtConfig(){
+        return new JwtConfig();
+    }
+
+    @Bean
+    public JwtTokenGenerator jwtTokenGenerator(JwtConfig config){
+        return new JwtTokenGenerator(config);
+    }
+
+    @Bean
+    public JwtTokenParser jwtTokenParser(JwtConfig config){
+        return new JwtTokenParser(config);
+    }
+}

+ 4 - 5
hsweb-authorization/hsweb-authorization-jwt/src/main/java/org/hswebframework/web/authorization/jwt/JwtTokenGenarator.java

@@ -11,20 +11,18 @@ import org.hswebframework.web.authorization.basic.web.UserTokenGenerator;
 import org.hswebframework.web.id.IDGenerator;
 
 import javax.crypto.SecretKey;
-import javax.crypto.spec.SecretKeySpec;
-import java.util.Collections;
 import java.util.Date;
 import java.util.HashMap;
 import java.util.Map;
 
 /**
- * Created by zhouhao on 2017/8/30.
+ *
  */
-public class JwtTokenGenarator implements UserTokenGenerator {
+public class JwtTokenGenerator implements UserTokenGenerator {
 
     private JwtConfig jwtConfig;
 
-    public JwtTokenGenarator(JwtConfig jwtConfig) {
+    public JwtTokenGenerator(JwtConfig jwtConfig) {
         this.jwtConfig = jwtConfig;
     }
 
@@ -43,6 +41,7 @@ public class JwtTokenGenarator implements UserTokenGenerator {
         String jwtToken = createJWT(jwtConfig.getId(),token,jwtConfig.getTtl());
 
         String refreshToken = createJWT(jwtConfig.getId(),token,jwtConfig.getRefreshTtl());
+
         int timeout = jwtConfig.getTtl();
 
         return new TokenResult() {

+ 3 - 0
hsweb-authorization/hsweb-authorization-jwt/src/main/resources/META-INF/spring.factories

@@ -0,0 +1,3 @@
+# Auto Configure
+org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
+org.hswebframework.web.authorization.jwt.JwtAutoConfiguration

+ 6 - 0
hsweb-examples/hsweb-examples-simple/pom.xml

@@ -120,6 +120,12 @@
             <artifactId>hsweb-authorization-basic</artifactId>
             <version>${project.version}</version>
         </dependency>
+        
+        <dependency>
+            <groupId>org.hswebframework.web</groupId>
+            <artifactId>hsweb-authorization-jwt</artifactId>
+            <version>${project.version}</version>
+        </dependency>
 
         <!--组织架构-->
         <dependency>

+ 7 - 0
hsweb-examples/hsweb-examples-simple/src/main/java/org/hswebframework/web/example/simple/TestController.java

@@ -1,6 +1,7 @@
 package org.hswebframework.web.example.simple;
 
 import io.swagger.annotations.ApiOperation;
+import org.apache.commons.codec.digest.DigestUtils;
 import org.hswebframework.web.authorization.Authentication;
 import org.hswebframework.web.authorization.Permission;
 import org.hswebframework.web.authorization.annotation.Authorize;
@@ -20,6 +21,7 @@ import org.hswebframework.web.service.QueryByEntityService;
 import org.hswebframework.web.service.QueryService;
 import org.springframework.web.bind.annotation.*;
 
+import java.util.Base64;
 import java.util.List;
 
 
@@ -133,4 +135,9 @@ public class TestController implements QueryController<UserEntity, String, Query
             return null;
         }
     }
+
+    public static void main(String[] args) {
+        String id =org.apache.commons.codec.binary.Base64.encodeBase64String("test".getBytes());
+        System.out.println(id);
+    }
 }

+ 3 - 0
hsweb-examples/hsweb-examples-simple/src/main/resources/application.yml

@@ -17,6 +17,9 @@ spring:
 
 hsweb:
     authorize:
+      jwt:
+        id: test
+        secret: dGVzdA==
 #      filters:
 #           "/swagger-ui.html": "authc"
     app: