更换了Redis的库
新增了对机器人的操作(redis)
This commit is contained in:
@@ -19,8 +19,6 @@ import java.util.Enumeration;
|
||||
public class APIFilter implements Filter {
|
||||
@Resource
|
||||
UKeyDao keyDao;
|
||||
@Resource
|
||||
RedisTools redisTools;
|
||||
|
||||
@Override
|
||||
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
|
||||
@@ -39,18 +37,15 @@ public class APIFilter implements Filter {
|
||||
}
|
||||
}
|
||||
if (cookie != null) {
|
||||
if ("ok".equals(redisTools.get(cookie.getValue()))) {
|
||||
if ("ok".equals(RedisTools.get(cookie.getValue()))) {
|
||||
isCookie = true;
|
||||
}
|
||||
}
|
||||
if (!isToken) {
|
||||
System.out.println("token验证不通过:" + token);
|
||||
} else if (!isCookie) {
|
||||
System.out.println("请求无令牌,拦截");
|
||||
}
|
||||
|
||||
if (!isCookie && !isToken) {
|
||||
//response.sendRedirect("/");
|
||||
if(!request.getRequestURI().contains("/login/")){
|
||||
System.out.println("请求无令牌,拦截");
|
||||
if(!request.getRequestURI().contains("/login/")&&!request.getRequestURI().equals("/favicon.ico")){
|
||||
response.sendRedirect("/");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -12,8 +12,6 @@ import org.springframework.data.redis.core.*;
|
||||
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
|
||||
import org.springframework.data.redis.serializer.StringRedisSerializer;
|
||||
|
||||
@Configuration
|
||||
@EnableCaching
|
||||
public class RedisConfig extends CachingConfigurerSupport {
|
||||
@Bean
|
||||
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
|
||||
|
||||
@@ -1,33 +1,137 @@
|
||||
package com.yutou.tools.utils;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
import org.springframework.stereotype.Component;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Properties;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import redis.clients.jedis.Jedis;
|
||||
|
||||
@Component
|
||||
public class RedisTools {
|
||||
@Resource
|
||||
RedisTemplate<String, String> redisTemplate;
|
||||
private static String host;
|
||||
private static int port;
|
||||
private static int TOKEN_TIMEOUT_DEFAULT=360;
|
||||
|
||||
private RedisTools() {
|
||||
|
||||
public void set(String key, String value) {
|
||||
redisTemplate.opsForValue().set(key, value);
|
||||
}
|
||||
|
||||
public void set(String key, String value, long time) {
|
||||
System.out.println("key=" + key + " value=" + value + " time=" + time);
|
||||
redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
|
||||
// 写成静态代码块形式,只加载一次,节省资源
|
||||
static {
|
||||
//Properties properties = PropertyUtil.loadProperties("jedis.properties");
|
||||
//host = properties.getProperty("redis.host");
|
||||
//port = Integer.valueOf(properties.getProperty("redis.port"));
|
||||
host="127.0.0.1";
|
||||
port=6379;
|
||||
}
|
||||
|
||||
public String get(String key) {
|
||||
public static boolean set(int dbIndex,String key,String value){
|
||||
try {
|
||||
return redisTemplate.opsForValue().get(key);
|
||||
Jedis jedis =getRedis();
|
||||
jedis.select(dbIndex);
|
||||
String ret=jedis.set(key,value);
|
||||
System.out.println("Redis set ="+ret);
|
||||
jedis.close();
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
// TODO: handle exception
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
public static boolean set(String key, String value) {
|
||||
return set(0,key,value);
|
||||
}
|
||||
|
||||
public static boolean set(String key,String value,int timeout) {
|
||||
try {
|
||||
Jedis jedis=getRedis();
|
||||
if(timeout==-1){
|
||||
jedis.set(key,value);
|
||||
}else {
|
||||
jedis.setex(key, timeout, value);
|
||||
}
|
||||
jedis.close();
|
||||
}catch (Exception e) {
|
||||
// TODO: handle exception
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
public static String get(String key,int dbIndex){
|
||||
String value = "-999";
|
||||
Jedis jedis=null;
|
||||
try {
|
||||
jedis = getRedis();
|
||||
jedis.select(dbIndex);
|
||||
value = jedis.get(key);
|
||||
jedis.close();
|
||||
} catch (Exception e) {
|
||||
// TODO: handle exception
|
||||
// e.printStackTrace();
|
||||
}finally {
|
||||
if(jedis!=null)
|
||||
jedis.close();
|
||||
}
|
||||
return value;
|
||||
}
|
||||
public static String get(String key) {
|
||||
return get(key,0);
|
||||
}
|
||||
public static boolean remove(String key) {
|
||||
Jedis jedis=getRedis();
|
||||
Long i=jedis.del(key);
|
||||
jedis.close();
|
||||
if(i>0) {
|
||||
return true;
|
||||
}else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
public static void removeLoginState(String uid) {
|
||||
Jedis jedis=getRedis();
|
||||
Set<String> keys=jedis.keys("*");
|
||||
for (String string : keys) {
|
||||
if(string.equals(uid)) {
|
||||
jedis.del(string);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
public static String ping() {
|
||||
Jedis jedis=getRedis();
|
||||
String tmp=jedis.ping();
|
||||
jedis.close();
|
||||
return tmp;
|
||||
}
|
||||
public static boolean exists(String key,String value) {
|
||||
Jedis jedis=getRedis();
|
||||
boolean flag=value.equals(jedis.get(key));
|
||||
jedis.close();
|
||||
return flag;
|
||||
}
|
||||
public static Jedis getRedis() {
|
||||
return new Jedis(host,port);
|
||||
}
|
||||
private static class PropertyUtil {
|
||||
|
||||
// 加载property文件到io流里面
|
||||
public static Properties loadProperties(String propertyFile) {
|
||||
Properties properties = new Properties();
|
||||
try {
|
||||
InputStream is = PropertyUtil.class.getClassLoader().getResourceAsStream(propertyFile);
|
||||
if (is == null) {
|
||||
is = PropertyUtil.class.getClassLoader().getResourceAsStream(propertyFile);
|
||||
}
|
||||
properties.load(is);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return properties;
|
||||
}
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
String str= "\"aaa\"";
|
||||
str=str.replaceAll("\"aaa\"", "0");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.yutou.tools.utils;
|
||||
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.yutou.tools.nas.UpdateIp;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
@@ -19,43 +20,48 @@ import java.util.Random;
|
||||
|
||||
public class Tools {
|
||||
/**
|
||||
* 设置Cookie
|
||||
* 设置Cookie
|
||||
*
|
||||
* @param response
|
||||
* @param key
|
||||
* @param value
|
||||
* @param time
|
||||
*/
|
||||
public static void setCookie(HttpServletResponse response, String key,String value,int time) {
|
||||
public static void setCookie(HttpServletResponse response, String key, String value, int time) {
|
||||
Cookie cookie = new Cookie(key, value);
|
||||
if(time!=-1) {
|
||||
if (time != -1) {
|
||||
cookie.setMaxAge(time);
|
||||
}
|
||||
cookie.setPath("/");
|
||||
response.addCookie(cookie);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取Cookie
|
||||
*
|
||||
* @param request
|
||||
* @param key
|
||||
* @return
|
||||
*/
|
||||
public static Cookie getCookie(HttpServletRequest request,String key) {
|
||||
public static Cookie getCookie(HttpServletRequest request, String key) {
|
||||
Cookie[] cookies = request.getCookies();
|
||||
try {
|
||||
for (Cookie cookie : cookies) {
|
||||
if (key!=null&&cookie.getName().equals(key)) {
|
||||
if (key != null && cookie.getName().equals(key)) {
|
||||
return cookie;
|
||||
}
|
||||
}
|
||||
}catch (Exception ignored){
|
||||
} catch (Exception ignored) {
|
||||
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除Cookie
|
||||
*
|
||||
* @param request
|
||||
* @param response
|
||||
* @param key
|
||||
@@ -63,8 +69,8 @@ public class Tools {
|
||||
*/
|
||||
public static String deleteCookie(HttpServletRequest request, HttpServletResponse response, String key) {
|
||||
for (Cookie cookie : request.getCookies()) {
|
||||
if(cookie.getName().equals(key)) {
|
||||
System.out.println("删除key="+key);
|
||||
if (cookie.getName().equals(key)) {
|
||||
System.out.println("删除key=" + key);
|
||||
cookie.setMaxAge(0);
|
||||
cookie.setPath("/");
|
||||
cookie.setValue(null);
|
||||
@@ -73,29 +79,36 @@ public class Tools {
|
||||
}
|
||||
return "ok";
|
||||
}
|
||||
public static void sendServer(String title,String msg){
|
||||
try{
|
||||
System.out.println("title="+title+" msg="+msg);
|
||||
HttpURLConnection connection= (HttpURLConnection) new URL("https://sc.ftqq.com/SCU64034T5adf5c5940dcecc016e0e9d0cf9b1e725da126ff47475.send?text="
|
||||
+ URLEncoder.encode(title,"UTF-8")+"&desp="+URLEncoder.encode(msg,"UTF-8")).openConnection();
|
||||
|
||||
public static void sendServer(String title, String msg) {
|
||||
try {
|
||||
System.out.println("title=" + title + " msg=" + msg);
|
||||
HttpURLConnection connection = (HttpURLConnection) new URL("https://sc.ftqq.com/SCU64034T5adf5c5940dcecc016e0e9d0cf9b1e725da126ff47475.send?text="
|
||||
+ URLEncoder.encode(title, "UTF-8") + "&desp=" + URLEncoder.encode(msg, "UTF-8")).openConnection();
|
||||
connection.connect();
|
||||
InputStream inputStream=connection.getInputStream();
|
||||
int i=inputStream.read();
|
||||
InputStream inputStream = connection.getInputStream();
|
||||
int i = inputStream.read();
|
||||
inputStream.close();
|
||||
connection.disconnect();
|
||||
}catch (Exception e){
|
||||
if (UpdateIp.nas_ip != null) {
|
||||
connection = (HttpURLConnection) new URL("http://" + UpdateIp.nas_ip + ":8000/localhome/bot/send.do?msg=" + URLEncoder.encode((title + "\n" + msg), "UTF-8") + "&token=zIrsh9TUZP2lfRW753PannG49E7VJvor").openConnection();
|
||||
connection.connect();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取项目路径
|
||||
*
|
||||
* @param request
|
||||
* @return
|
||||
*/
|
||||
public static String getPath(HttpServletRequest request) {
|
||||
return request.getServletContext().getRealPath("/") + "/";
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取客户端IP
|
||||
*
|
||||
@@ -115,13 +128,12 @@ public class Tools {
|
||||
}
|
||||
return ip;
|
||||
}
|
||||
|
||||
/**
|
||||
* N以内的不重复随机数
|
||||
*
|
||||
* @param min
|
||||
* 最小值
|
||||
* @param max
|
||||
* 最大值
|
||||
* @param min 最小值
|
||||
* @param max 最大值
|
||||
* @param n
|
||||
* @return
|
||||
*/
|
||||
@@ -148,12 +160,13 @@ public class Tools {
|
||||
}
|
||||
return result;
|
||||
}
|
||||
public static int checkWebLogin(HttpServletRequest request,RedisTools redisTools){
|
||||
JSONArray array=new JSONArray();
|
||||
if(redisTools.get("bean")!=null){
|
||||
array=JSONArray.parseArray(redisTools.get("bean"));
|
||||
|
||||
public static int checkWebLogin(HttpServletRequest request) {
|
||||
JSONArray array = new JSONArray();
|
||||
if (RedisTools.get("bean") != null) {
|
||||
array = JSONArray.parseArray(RedisTools.get("bean"));
|
||||
}
|
||||
if(array.contains(Tools.getRemoteAddress(request))){
|
||||
if (array.contains(Tools.getRemoteAddress(request))) {
|
||||
System.out.println("IP已被封禁");
|
||||
return -100;
|
||||
}
|
||||
@@ -161,57 +174,57 @@ public class Tools {
|
||||
if (cookie == null) {
|
||||
return -1;
|
||||
}
|
||||
if (redisTools.get(cookie.getValue()).equals("ok")) {
|
||||
return 1;
|
||||
if (RedisTools.get(cookie.getValue()).equals("ok")) {
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
public static String get(String url){
|
||||
try{
|
||||
HttpURLConnection connection= (HttpURLConnection) new URL(url).openConnection();
|
||||
BufferedReader reader=new BufferedReader(new InputStreamReader(connection.getInputStream()));
|
||||
StringBuilder str= new StringBuilder();
|
||||
|
||||
public static String get(String url) {
|
||||
try {
|
||||
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
|
||||
StringBuilder str = new StringBuilder();
|
||||
String tmp;
|
||||
while ((tmp=reader.readLine())!=null){
|
||||
while ((tmp = reader.readLine()) != null) {
|
||||
str.append(tmp);
|
||||
}
|
||||
reader.close();
|
||||
connection.disconnect();
|
||||
return str.toString();
|
||||
}catch (Exception e){
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存上传的文件
|
||||
*
|
||||
* @param path
|
||||
* 路径
|
||||
* @param file
|
||||
* 文件
|
||||
* @param path 路径
|
||||
* @param file 文件
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public static String createFile(String path, MultipartFile file, String newFileName) throws Exception {
|
||||
String savePath =new File("").getAbsolutePath()+File.separator +"html"+File.separator+ path;
|
||||
String savePath = new File("").getAbsolutePath() + File.separator + "html" + File.separator + path;
|
||||
File servicePath = new File(savePath);
|
||||
if (!servicePath.exists()) {
|
||||
servicePath.mkdirs();
|
||||
}
|
||||
String fileName=file.getOriginalFilename();
|
||||
if(newFileName!=null) {
|
||||
fileName=newFileName;
|
||||
String fileName = file.getOriginalFilename();
|
||||
if (newFileName != null) {
|
||||
fileName = newFileName;
|
||||
}
|
||||
File saveFile = new File(savePath + "/" + fileName);
|
||||
if(saveFile.exists()) {
|
||||
if(!saveFile.delete()) {
|
||||
saveFile = new File(savePath + "/" + fileName.replace(".", "_"+new Date().getTime()+"."));
|
||||
if (saveFile.exists()) {
|
||||
if (!saveFile.delete()) {
|
||||
saveFile = new File(savePath + "/" + fileName.replace(".", "_" + new Date().getTime() + "."));
|
||||
}
|
||||
}
|
||||
file.transferTo(saveFile);
|
||||
fileName=URLEncoder.encode(fileName,"UTF-8");
|
||||
System.out.println("上传文件保存路径:"+saveFile.getAbsolutePath());
|
||||
return path+fileName;
|
||||
fileName = URLEncoder.encode(fileName, "UTF-8");
|
||||
System.out.println("上传文件保存路径:" + saveFile.getAbsolutePath());
|
||||
return path + fileName;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user