wuheng 1 سال پیش
والد
کامیت
02ba085863

+ 4 - 0
common/pom.xml

@@ -14,6 +14,10 @@
     </parent>
 
     <dependencies>
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-data-redis</artifactId>
+        </dependency>
         <dependency>
             <groupId>org.springframework</groupId>
             <artifactId>spring-beans</artifactId>

+ 30 - 0
common/src/main/java/com/koobietech/eas/common/config/RedisConfig.java

@@ -0,0 +1,30 @@
+package com.koobietech.eas.common.config;
+
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.data.redis.connection.RedisConnectionFactory;
+import org.springframework.data.redis.core.RedisTemplate;
+import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
+import org.springframework.data.redis.serializer.RedisSerializer;
+
+@Configuration
+public class RedisConfig {
+
+    @Bean("CommonRedisTemplate")
+    @SuppressWarnings("all")
+    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory){
+        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
+        redisTemplate.setConnectionFactory( redisConnectionFactory );
+
+        redisTemplate.setKeySerializer( RedisSerializer.string() );
+        redisTemplate.setHashKeySerializer( RedisSerializer.string() );
+
+        GenericJackson2JsonRedisSerializer jsonRedisSerializer =
+                new GenericJackson2JsonRedisSerializer();
+
+        redisTemplate.setValueSerializer( jsonRedisSerializer );
+        redisTemplate.setHashValueSerializer( jsonRedisSerializer );
+
+        return redisTemplate;
+    }
+}

+ 5 - 0
common/src/main/java/com/koobietech/eas/common/constant/UserType.java

@@ -0,0 +1,5 @@
+package com.koobietech.eas.common.constant;
+
+public enum UserType {
+    TEACHER, MEMBER, ADMIN
+}

+ 190 - 0
common/src/main/java/com/koobietech/eas/common/service/RedisService.java

@@ -0,0 +1,190 @@
+package com.koobietech.eas.common.service;
+
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+
+public interface RedisService {
+
+    /**
+     * 保存属性
+     */
+    void set(String key, Object value, long time);
+
+    /**
+     * 保存属性
+     */
+    void set(String key, Object value);
+
+    /**
+     * 获取属性
+     */
+    Object get(String key);
+
+    /**
+     * 删除属性
+     */
+    Boolean del(String key);
+
+    /**
+     * 批量删除属性
+     */
+    Long del(List<String> keys);
+
+    /**
+     * 设置过期时间
+     */
+    Boolean expire(String key, long time);
+
+    /**
+     * 获取过期时间
+     */
+    Long getExpire(String key);
+
+    /**
+     * 判断是否有该属性
+     */
+    Boolean hasKey(String key);
+
+    /**
+     * 按delta递增
+     */
+    Long incr(String key, long delta);
+
+    /**
+     * 按delta递减
+     */
+    Long decr(String key, long delta);
+
+    /**
+     * 获取Hash结构中的属性
+     */
+    Object hGet(String key, String hashKey);
+
+    /**
+     * 向Hash结构中放入一个属性
+     */
+    Boolean hSet(String key, String hashKey, Object value, long time);
+
+    /**
+     * 向Hash结构中放入一个属性
+     */
+    void hSet(String key, String hashKey, Object value);
+
+    /**
+     * 直接获取整个Hash结构
+     */
+    Map<Object, Object> hGetAll(String key);
+
+    /**
+     * 直接设置整个Hash结构
+     */
+    Boolean hSetAll(String key, Map<String, Object> map, long time);
+
+    /**
+     * 直接设置整个Hash结构
+     */
+    void hSetAll(String key, Map<String, Object> map);
+
+    /**
+     * 删除Hash结构中的属性
+     */
+    void hDel(String key, Object... hashKey);
+
+    /**
+     * 判断Hash结构中是否有该属性
+     */
+    Boolean hHasKey(String key, String hashKey);
+
+    /**
+     * Hash结构中属性递增
+     */
+    Long hIncr(String key, String hashKey, Long delta);
+
+    /**
+     * Hash结构中属性递减
+     */
+    Long hDecr(String key, String hashKey, Long delta);
+
+    /**
+     * 获取Set结构
+     */
+    Set<Object> sMembers(String key);
+
+    /**
+     * 向Set结构中添加属性
+     */
+    Long sAdd(String key, Object... values);
+
+    /**
+     * 向Set结构中添加属性
+     */
+    Long sAdd(String key, long time, Object... values);
+
+    /**
+     * 是否为Set中的属性
+     */
+    Boolean sIsMember(String key, Object value);
+
+    /**
+     * 获取Set结构的长度
+     */
+    Long sSize(String key);
+
+    /**
+     * 删除Set结构中的属性
+     */
+    Long sRemove(String key, Object... values);
+
+    /**
+     * 获取List结构中的属性
+     */
+    List<Object> lRange(String key, long start, long end);
+
+    /**
+     * 获取List结构的长度
+     */
+    Long lSize(String key);
+
+    /**
+     * 根据索引获取List中的属性
+     */
+    Object lIndex(String key, long index);
+
+    /**
+     * 向List结构中添加属性
+     */
+    Long lPush(String key, Object value);
+
+    /**
+     * 向List结构中添加属性
+     */
+    Long lPush(String key, Object value, long time);
+
+    /**
+     * 向List结构中批量添加属性
+     */
+    Long lPushAll(String key, Object... values);
+
+    /**
+     * 向List结构中批量添加属性
+     */
+    Long lPushAll(String key, Long time, Object... values);
+
+    /**
+     * 从List结构中移除属性
+     */
+    Long lRemove(String key, long count, Object value);
+
+    /**
+     * 获取KEY
+     */
+    Set<String> keys(String pattern);
+
+    /**
+     * 事务锁
+     */
+    Boolean setnx(String key, long time, Object value);
+
+}

+ 213 - 0
common/src/main/java/com/koobietech/eas/common/service/impl/RedisServiceImpl.java

@@ -0,0 +1,213 @@
+package com.koobietech.eas.common.service.impl;
+
+import com.koobietech.eas.common.service.RedisService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Qualifier;
+import org.springframework.data.redis.core.RedisTemplate;
+import org.springframework.stereotype.Service;
+
+import javax.annotation.Resource;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * redis操作实现类
+ * Created by sifu on 2020/3/3.
+ */
+@Service
+public class RedisServiceImpl implements RedisService {
+
+    @Autowired
+    @Qualifier("CommonRedisTemplate")
+    private RedisTemplate<String, Object> redisTemplate;
+
+    @Override
+    public void set(String key, Object value, long time) {
+        redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
+    }
+
+    @Override
+    public void set(String key, Object value) {
+        redisTemplate.opsForValue().set(key, value);
+    }
+
+    @Override
+    public Object get(String key) {
+        return redisTemplate.opsForValue().get(key);
+    }
+
+    @Override
+    public Boolean del(String key) {
+        return redisTemplate.delete(key);
+    }
+
+    @Override
+    public Long del(List<String> keys) {
+        return redisTemplate.delete(keys);
+    }
+
+    @Override
+    public Boolean expire(String key, long time) {
+        return redisTemplate.expire(key, time, TimeUnit.SECONDS);
+    }
+
+    @Override
+    public Long getExpire(String key) {
+        return redisTemplate.getExpire(key, TimeUnit.SECONDS);
+    }
+
+    @Override
+    public Boolean hasKey(String key) {
+        return redisTemplate.hasKey(key);
+    }
+
+    @Override
+    public Long incr(String key, long delta) {
+        return redisTemplate.opsForValue().increment(key, delta);
+    }
+
+    @Override
+    public Long decr(String key, long delta) {
+        return redisTemplate.opsForValue().increment(key, -delta);
+    }
+
+    @Override
+    public Object hGet(String key, String hashKey) {
+        return redisTemplate.opsForHash().get(key, hashKey);
+    }
+
+    @Override
+    public Boolean hSet(String key, String hashKey, Object value, long time) {
+        redisTemplate.opsForHash().put(key, hashKey, value);
+        return expire(key, time);
+    }
+
+    @Override
+    public void hSet(String key, String hashKey, Object value) {
+        redisTemplate.opsForHash().put(key, hashKey, value);
+    }
+
+    @Override
+    public Map<Object, Object> hGetAll(String key) {
+        return redisTemplate.opsForHash().entries(key);
+    }
+
+    @Override
+    public Boolean hSetAll(String key, Map<String, Object> map, long time) {
+        redisTemplate.opsForHash().putAll(key, map);
+        return expire(key, time);
+    }
+
+    @Override
+    public void hSetAll(String key, Map<String, Object> map) {
+        redisTemplate.opsForHash().putAll(key, map);
+    }
+
+    @Override
+    public void hDel(String key, Object... hashKey) {
+        redisTemplate.opsForHash().delete(key, hashKey);
+    }
+
+    @Override
+    public Boolean hHasKey(String key, String hashKey) {
+        return redisTemplate.opsForHash().hasKey(key, hashKey);
+    }
+
+    @Override
+    public Long hIncr(String key, String hashKey, Long delta) {
+        return redisTemplate.opsForHash().increment(key, hashKey, delta);
+    }
+
+    @Override
+    public Long hDecr(String key, String hashKey, Long delta) {
+        return redisTemplate.opsForHash().increment(key, hashKey, -delta);
+    }
+
+    @Override
+    public Set<Object> sMembers(String key) {
+        return redisTemplate.opsForSet().members(key);
+    }
+
+    @Override
+    public Long sAdd(String key, Object... values) {
+        return redisTemplate.opsForSet().add(key, values);
+    }
+
+    @Override
+    public Long sAdd(String key, long time, Object... values) {
+        Long count = redisTemplate.opsForSet().add(key, values);
+        expire(key, time);
+        return count;
+    }
+
+    @Override
+    public Boolean sIsMember(String key, Object value) {
+        return redisTemplate.opsForSet().isMember(key, value);
+    }
+
+    @Override
+    public Long sSize(String key) {
+        return redisTemplate.opsForSet().size(key);
+    }
+
+    @Override
+    public Long sRemove(String key, Object... values) {
+        return redisTemplate.opsForSet().remove(key, values);
+    }
+
+    @Override
+    public List<Object> lRange(String key, long start, long end) {
+        return redisTemplate.opsForList().range(key, start, end);
+    }
+
+    @Override
+    public Long lSize(String key) {
+        return redisTemplate.opsForList().size(key);
+    }
+
+    @Override
+    public Object lIndex(String key, long index) {
+        return redisTemplate.opsForList().index(key, index);
+    }
+
+    @Override
+    public Long lPush(String key, Object value) {
+        return redisTemplate.opsForList().rightPush(key, value);
+    }
+
+    @Override
+    public Long lPush(String key, Object value, long time) {
+        Long index = redisTemplate.opsForList().rightPush(key, value);
+        expire(key, time);
+        return index;
+    }
+
+    @Override
+    public Long lPushAll(String key, Object... values) {
+        return redisTemplate.opsForList().rightPushAll(key, values);
+    }
+
+    @Override
+    public Long lPushAll(String key, Long time, Object... values) {
+        Long count = redisTemplate.opsForList().rightPushAll(key, values);
+        expire(key, time);
+        return count;
+    }
+
+    @Override
+    public Long lRemove(String key, long count, Object value) {
+        return redisTemplate.opsForList().remove(key, count, value);
+    }
+
+    @Override
+    public Set<String> keys(String pattern) {
+        return redisTemplate.keys(pattern);
+    }
+
+    @Override
+    public Boolean setnx(String key, long time, Object value) {
+        return redisTemplate.opsForValue().setIfAbsent(key, value, time, TimeUnit.SECONDS);
+    }
+}

+ 19 - 2
common/src/main/java/com/koobietech/eas/common/utils/JwtManager.java

@@ -5,20 +5,31 @@ import com.auth0.jwt.algorithms.Algorithm;
 import com.auth0.jwt.exceptions.JWTVerificationException;
 import com.auth0.jwt.interfaces.DecodedJWT;
 import com.auth0.jwt.interfaces.JWTVerifier;
-import com.koobietech.eas.common.constant.Gender;
+import com.koobietech.eas.common.constant.UserType;
 import com.koobietech.eas.common.exception.EasException;
 import com.koobietech.eas.common.pojo.JwtUserDto;
 import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Component;
 
 import java.util.Calendar;
 
+@Component
 public class JwtManager {
 
     @Value("${eas.jwt-secret-key}")
     private String SECRET;
 
+    @Value("${eas.jwt-expires-date}")
+    private int expires;
+
+    /**
+     * 创建 token
+     * @param userDto
+     * @return
+     */
     public String createJwt(JwtUserDto userDto){
         Calendar calendar = Calendar.getInstance();
+        calendar.add(calendar.DATE, expires);
         String sign = JWT.create()
                 .withClaim("user", userDto.getUsername())
                 .withClaim("id", userDto.getId())
@@ -27,6 +38,12 @@ public class JwtManager {
                 .sign(Algorithm.HMAC256(SECRET));
         return sign;
     }
+
+    /**
+     * 解密 Token
+     * @param token
+     * @return
+     */
     public JwtUserDto decodeJwt(String token){
         JwtUserDto jwtUserDto = new JwtUserDto();
         JWTVerifier build = JWT.require(Algorithm.HMAC256(SECRET)).build();
@@ -34,7 +51,7 @@ public class JwtManager {
             DecodedJWT verify = build.verify(token);
             jwtUserDto.setId(verify.getClaim("id").asLong());
             jwtUserDto.setUsername(verify.getClaim("user").asString());
-            jwtUserDto.setType(Gender.valueOf(verify.getClaim("type").asString()));
+            jwtUserDto.setType(UserType.valueOf(verify.getClaim("type").asString()));
         } catch ( JWTVerificationException e){
             throw new EasException("token 不正确!");
         }

+ 5 - 0
controller/pom.xml

@@ -39,6 +39,11 @@
             <version>${project.version}</version>
             <scope>compile</scope>
         </dependency>
+        <dependency>
+            <groupId>com.koobietech.eas</groupId>
+            <artifactId>service</artifactId>
+            <version>${project.version}</version>
+        </dependency>
     </dependencies>
 
     <dependencyManagement>

+ 5 - 1
controller/src/main/java/com/koobietech/eas/ControllerApplication.java

@@ -3,9 +3,13 @@ package com.koobietech.eas;
 import org.mybatis.spring.annotation.MapperScan;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.context.annotation.ComponentScan;
 
 @SpringBootApplication
-@MapperScan(basePackages = {"com.koobietech.eas"})
+@MapperScan(basePackages = {"com.koobietech.eas.mbg.mapper"})
+@ComponentScan(basePackages = {
+        "com.koobietech.eas.controller", "com.koobietech.eas.security",
+        "com.koobietech.eas.service", "com.koobietech.eas.common"})
 public class ControllerApplication {
 
     public static void main(String[] args) {

+ 9 - 3
controller/src/main/java/com/koobietech/eas/controller/EasUserController.java

@@ -2,7 +2,10 @@ package com.koobietech.eas.controller;
 
 import com.koobietech.eas.mbg.mapper.EasSysPermissionMapper;
 import com.koobietech.eas.mbg.model.EasSysPermission;
+import com.koobietech.eas.service.DemoService;
 import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
 
 import javax.annotation.Resource;
@@ -14,9 +17,12 @@ public class EasUserController {
     @Resource
     EasSysPermissionMapper easSysPermissionMapper;
 
-    @GetMapping("/test")
-    public List<EasSysPermission> test(){
-        return easSysPermissionMapper.selectByExample(null);
+    @Resource
+    DemoService demoService;
+
+    @RequestMapping("/test")
+    public String test(){
+        return demoService.test();
     }
 
 }

+ 6 - 1
controller/src/main/resources/application-dev.yaml

@@ -1,11 +1,16 @@
 server:
-  port: 8080
+  port: 8081
 spring:
   datasource:
     url: jdbc:mysql://39.105.160.25:10992/eas?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai&useSSL=true&tinyInt1isBit=false
     username: eas
     password: eas
     driver-class-name: com.mysql.cj.jdbc.Driver
+  redis:
+    host: localhost
+    database: 9
+    password:
+    port: 26379
 
   security:
     user:

+ 9 - 1
controller/src/main/resources/application.yaml

@@ -8,6 +8,12 @@ spring:
     call-setters-on-nulls: true
     map-underscore-to-camel-case: true
 
+  redis:
+    host: localhost
+    database: 0
+    password:
+    port: 6379
+
 security:
   url:
     ignored:
@@ -16,4 +22,6 @@ security:
 
 eas:
   jwt-secret-key: 123456
-  password-sign-key: eas-key-password
+  jwt-expires-date: 1
+  password-sign-key: eas-key-password
+

+ 20 - 6
controller/src/test/java/com/koobietech/eas/controller/ControllerApplicationTests.java

@@ -1,26 +1,40 @@
 package com.koobietech.eas.controller;
 
+import com.koobietech.eas.common.constant.UserType;
+import com.koobietech.eas.common.pojo.JwtUserDto;
+import com.koobietech.eas.common.service.RedisService;
+import com.koobietech.eas.common.utils.JwtManager;
 import com.koobietech.eas.common.utils.PasswordManager;
 import org.junit.jupiter.api.Test;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.boot.test.context.SpringBootTest;
 
+import javax.annotation.Resource;
+
 
 @SpringBootTest
 class ControllerApplicationTests {
 
-    @Value("${eas.password-sign-key}")
-    String easKey;
+    @Resource
+    RedisService redisService;
+
+    @Resource
+    JwtManager jwtManager;
 
     @Test
     void contextLoads() {
 
-        String data = "nImYEyKu38ClXCpm886JNNyk9PzQ2SN6IrCaWFqrASo=";
+        JwtUserDto jwtUserDto = new JwtUserDto();
+
+        jwtUserDto.setType(UserType.TEACHER);
+        jwtUserDto.setId(12L);
+        jwtUserDto.setUsername("李四");
+
+        String jwt = jwtManager.createJwt(jwtUserDto);
+        redisService.set("jwttoken", jwt);
+
 
-        String s = new PasswordManager()
-                .decryptPassword(data, easKey);
 
-        System.out.println( s );
     }
 
 }

+ 6 - 0
pom.xml

@@ -31,9 +31,15 @@
         <spring-context.version>5.3.24</spring-context.version>
         <spring-beans.version>5.3.24</spring-beans.version>
         <javax.servlet-api.version>4.0.1</javax.servlet-api.version>
+        <spring-data-redis>2.7.3</spring-data-redis>
     </properties>
     <dependencyManagement>
         <dependencies>
+            <dependency>
+                <groupId>org.springframework.boot</groupId>
+                <artifactId>spring-boot-starter-data-redis</artifactId>
+                <version>${spring-data-redis}</version>
+            </dependency>
             <dependency>
                 <groupId>com.auth0</groupId>
                 <artifactId>java-jwt</artifactId>

+ 2 - 0
security/src/main/java/com/koobietech/eas/security/config/SecurityConfig.java

@@ -34,6 +34,8 @@ public class SecurityConfig {
         registry.anyRequest().authenticated();
 
         HttpSecurity and = httpSecurity
+                .csrf()
+                .disable()
                 .sessionManagement()
                 .sessionCreationPolicy(SessionCreationPolicy.NEVER)
                 .and();

+ 11 - 0
service/pom.xml

@@ -7,6 +7,17 @@
     <name>service</name>
     <description>service</description>
 
+    <dependencies>
+        <dependency>
+            <groupId>org.springframework</groupId>
+            <artifactId>spring-beans</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.springframework</groupId>
+            <artifactId>spring-context</artifactId>
+        </dependency>
+    </dependencies>
+
     <parent>
         <artifactId>eas-system</artifactId>
         <groupId>com.koobietech.eas</groupId>

+ 1 - 0
service/src/main/java/com/koobietech/eas/service/DemoService.java

@@ -1,4 +1,5 @@
 package com.koobietech.eas.service;
 
 public interface DemoService {
+    String test();
 }

+ 6 - 0
service/src/main/java/com/koobietech/eas/service/impl/DemoServiceImpl.java

@@ -1,6 +1,12 @@
 package com.koobietech.eas.service.impl;
 
 import com.koobietech.eas.service.DemoService;
+import org.springframework.stereotype.Service;
 
+@Service
 public class DemoServiceImpl implements DemoService {
+    @Override
+    public String test() {
+        return "hello";
+    }
 }