yonge 5 years ago
parent
commit
3d1d50eabb

+ 7 - 2
mec-auth/mec-auth-server/src/main/java/com/ym/mec/auth/core/service/PermissionCheckService.java

@@ -30,8 +30,8 @@ public class PermissionCheckService {
 		Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
 
 		for (String perm : permissions) {
-			for(GrantedAuthority authority : authorities){
-				if(StringUtils.equals(perm, authority.getAuthority())){
+			for (GrantedAuthority authority : authorities) {
+				if (StringUtils.equals(perm, authority.getAuthority())) {
 					return true;
 				}
 			}
@@ -41,6 +41,11 @@ public class PermissionCheckService {
 	}
 
 	public boolean hasRoles(String... roles) {
+
+		if (hasPermissions(roles)) {
+			return true;
+		}
+
 		Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
 		if (authentication == null) {
 			return false;

+ 8 - 8
mec-common/src/main/java/com/ym/mec/common/cache/Cache.java

@@ -2,7 +2,7 @@ package com.ym.mec.common.cache;
 
 import java.util.Set;
 
-public interface Cache {
+public interface Cache<K, V> {
 
 	/**
 	 * 获得缓存值
@@ -11,7 +11,7 @@ public interface Cache {
 	 *            缓存key
 	 * @return 缓存值
 	 */
-	Object get(String key) throws CacheException;
+	Object get(K key) throws CacheException;
 
 	/**
 	 * 设置缓存
@@ -21,7 +21,7 @@ public interface Cache {
 	 * @param value
 	 *            缓存值
 	 */
-	void put(String key, Object value) throws CacheException;
+	void put(K key, V value) throws CacheException;
 
 	/**
 	 * 设置缓存
@@ -33,28 +33,28 @@ public interface Cache {
 	 * @param expireTimes
 	 * 		过期时间(单位:秒)
 	 */
-	void put(String key, Object value, int expireTimes) throws CacheException;
+	void put(K key, V value, int expireTimes) throws CacheException;
 
 	/**
 	 * 删除缓存
 	 * 
 	 * @param key
 	 */
-	void delete(String key) throws CacheException;
+	void delete(K key) throws CacheException;
 
 	/**
 	 * 判断key是否已经存在
 	 * @param key
 	 * @return
 	 */
-	public boolean exists(String key);
+	public boolean exists(K key);
 
 	/**
 	 * 获得所有的key
 	 * 
 	 * @return key集合
 	 */
-	Set<String> keys() throws CacheException;
+	Set<K> keys() throws CacheException;
 
 	/**
 	 * 获得缓存的key-value个数
@@ -68,6 +68,6 @@ public interface Cache {
 	 * @param key
 	 * @param expireTimes
 	 */
-	void expire(String key, int expireTimes);
+	void expire(K key, int expireTimes);
 
 }

+ 18 - 18
mec-common/src/main/java/com/ym/mec/common/redis/service/RedisCache.java

@@ -3,51 +3,51 @@ package com.ym.mec.common.redis.service;
 import java.util.Set;
 import java.util.concurrent.TimeUnit;
 
-import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.data.redis.core.RedisTemplate;
 import org.springframework.data.redis.core.ValueOperations;
-import org.springframework.stereotype.Component;
 
 import com.ym.mec.common.cache.Cache;
 import com.ym.mec.common.cache.CacheException;
 
-@Component
-public class RedisCache implements Cache {
+public class RedisCache<K, V> implements Cache<K, V> {
 
-	@Autowired
-	private RedisTemplate<String, Object> redisTemplate;
+	private RedisTemplate<K, V> redisTemplate;
+
+	public RedisCache(RedisTemplate<K, V> redisTemplate) {
+		this.redisTemplate = redisTemplate;
+	}
 
 	@Override
-	public Object get(String key) throws CacheException {
-		ValueOperations<String, Object> valueOps = redisTemplate.opsForValue();
+	public V get(K key) throws CacheException {
+		ValueOperations<K, V> valueOps = redisTemplate.opsForValue();
 		return valueOps.get(key);
 	}
 
 	@Override
-	public void put(String key, Object value) throws CacheException {
-		ValueOperations<String, Object> valueOps = redisTemplate.opsForValue();
+	public void put(K key, V value) throws CacheException {
+		ValueOperations<K, V> valueOps = redisTemplate.opsForValue();
 		valueOps.set(key, value);
 	}
 
 	@Override
-	public void put(String key, Object value, int expireTimes) throws CacheException {
-		ValueOperations<String, Object> valueOps = redisTemplate.opsForValue();
+	public void put(K key, V value, int expireTimes) throws CacheException {
+		ValueOperations<K, V> valueOps = redisTemplate.opsForValue();
 		valueOps.set(key, value, expireTimes, TimeUnit.SECONDS);
 	}
 
 	@Override
-	public void delete(String key) throws CacheException {
+	public void delete(K key) throws CacheException {
 		redisTemplate.delete(key);
 	}
 
 	@Override
-	public boolean exists(String key) {
+	public boolean exists(K key) {
 		return redisTemplate.hasKey(key);
 	}
 
 	@Override
-	public Set<String> keys() throws CacheException {
-		return redisTemplate.keys("*");
+	public Set<K> keys() throws CacheException {
+		return redisTemplate.keys((K) "*");
 	}
 
 	@Override
@@ -56,11 +56,11 @@ public class RedisCache implements Cache {
 	}
 
 	@Override
-	public void expire(String key, int expireTimes) {
+	public void expire(K key, int expireTimes) {
 		redisTemplate.expire(key, expireTimes, TimeUnit.SECONDS);
 	}
 
-	public void setRedisTemplate(RedisTemplate<String, Object> redisTemplate) {
+	public void setRedisTemplate(RedisTemplate<K, V> redisTemplate) {
 		this.redisTemplate = redisTemplate;
 	}
 }

+ 5 - 1
mec-common/src/main/java/com/ym/mec/common/validcode/impl/SmsCodeServiceImpl.java

@@ -1,18 +1,22 @@
 package com.ym.mec.common.validcode.impl;
 
-import com.ym.mec.common.validcode.SmsCodeService;
 import org.apache.commons.lang3.RandomStringUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.data.redis.core.RedisTemplate;
 import org.springframework.stereotype.Service;
 
+import com.ym.mec.common.redis.service.RedisCache;
+import com.ym.mec.common.validcode.SmsCodeService;
+
 @Service
 public class SmsCodeServiceImpl implements SmsCodeService {
 
 	@Autowired
 	private RedisTemplate<String,String> redisTemplate;
 	
+	private RedisCache<String,String> redisCache = new RedisCache<String, String>(redisTemplate);
+	
 	private final String loginVerifyCodeKey = "loginVerifyCode:";
 	
 	private int expireTime = 60;