更换了Redis的库
新增了对机器人的操作(redis)
This commit is contained in:
parent
2a06117ec7
commit
d247235ca5
2
pom.xml
2
pom.xml
@ -10,7 +10,7 @@
|
|||||||
</parent>
|
</parent>
|
||||||
<groupId>com.yutou</groupId>
|
<groupId>com.yutou</groupId>
|
||||||
<artifactId>tools</artifactId>
|
<artifactId>tools</artifactId>
|
||||||
<version>1.0.6</version>
|
<version>1.0.7</version>
|
||||||
<name>tools</name>
|
<name>tools</name>
|
||||||
<description>Demo project for Spring Boot</description>
|
<description>Demo project for Spring Boot</description>
|
||||||
|
|
||||||
|
@ -5,7 +5,6 @@ import com.alibaba.fastjson.JSONObject;
|
|||||||
import com.yutou.tools.mybatis.dao.BilibiliLiveDao;
|
import com.yutou.tools.mybatis.dao.BilibiliLiveDao;
|
||||||
import com.yutou.tools.mybatis.model.BilibiliLive;
|
import com.yutou.tools.mybatis.model.BilibiliLive;
|
||||||
import com.yutou.tools.mybatis.model.BilibiliLiveExample;
|
import com.yutou.tools.mybatis.model.BilibiliLiveExample;
|
||||||
import com.yutou.tools.utils.RedisTools;
|
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
29
src/main/java/com/yutou/tools/home/nas/QQBot.java
Normal file
29
src/main/java/com/yutou/tools/home/nas/QQBot.java
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
package com.yutou.tools.home.nas;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSONObject;
|
||||||
|
import com.yutou.tools.utils.RedisTools;
|
||||||
|
import org.springframework.stereotype.Controller;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.ResponseBody;
|
||||||
|
|
||||||
|
@Controller
|
||||||
|
@RequestMapping("/localhome/bot")
|
||||||
|
public class QQBot {
|
||||||
|
|
||||||
|
|
||||||
|
@RequestMapping("send.do")
|
||||||
|
@ResponseBody
|
||||||
|
public String send(String msg){
|
||||||
|
JSONObject json=new JSONObject();
|
||||||
|
try {
|
||||||
|
boolean ret= RedisTools.set(1,"msg_"+System.currentTimeMillis(),msg);
|
||||||
|
json.put("code",0);
|
||||||
|
json.put("msg",ret);
|
||||||
|
}catch (Exception e){
|
||||||
|
e.printStackTrace();
|
||||||
|
json.put("code",-1);
|
||||||
|
json.put("msg",e.getMessage());
|
||||||
|
}
|
||||||
|
return json.toJSONString();
|
||||||
|
}
|
||||||
|
}
|
@ -21,8 +21,6 @@ import java.util.List;
|
|||||||
|
|
||||||
@Controller
|
@Controller
|
||||||
public class NasManager {
|
public class NasManager {
|
||||||
@Resource
|
|
||||||
RedisTools redisTools;
|
|
||||||
@Resource
|
@Resource
|
||||||
NasAdminAddressDao adminAddressDao;
|
NasAdminAddressDao adminAddressDao;
|
||||||
@ResponseBody
|
@ResponseBody
|
||||||
@ -30,7 +28,7 @@ public class NasManager {
|
|||||||
public String getAdminAddress(HttpServletRequest request){
|
public String getAdminAddress(HttpServletRequest request){
|
||||||
JSONObject json=new JSONObject();
|
JSONObject json=new JSONObject();
|
||||||
|
|
||||||
String address=redisTools.get("adminAddress");
|
String address=RedisTools.get("adminAddress");
|
||||||
if(address==null){
|
if(address==null){
|
||||||
json.put("code",-1);
|
json.put("code",-1);
|
||||||
json.put("msg","暂未设置管理后台");
|
json.put("msg","暂未设置管理后台");
|
||||||
@ -136,7 +134,7 @@ public class NasManager {
|
|||||||
try {
|
try {
|
||||||
NasAdminAddress address=adminAddressDao.selectByPrimaryKey(Integer.parseInt(id));
|
NasAdminAddress address=adminAddressDao.selectByPrimaryKey(Integer.parseInt(id));
|
||||||
if(address!=null){
|
if(address!=null){
|
||||||
redisTools.set("adminAddress",JSONObject.toJSONString(address));
|
RedisTools.set("adminAddress",JSONObject.toJSONString(address));
|
||||||
}
|
}
|
||||||
json.put("code",0);
|
json.put("code",0);
|
||||||
json.put("msg","ok");
|
json.put("msg","ok");
|
||||||
|
@ -24,9 +24,6 @@ import java.util.regex.Pattern;
|
|||||||
public class UpdateIp {
|
public class UpdateIp {
|
||||||
private static List<String> keys = new ArrayList<>();
|
private static List<String> keys = new ArrayList<>();
|
||||||
public static String nas_ip=null;
|
public static String nas_ip=null;
|
||||||
@Resource
|
|
||||||
RedisTools redisTools;
|
|
||||||
|
|
||||||
/* static {
|
/* static {
|
||||||
keys.add("nas.yutou233.cn;");
|
keys.add("nas.yutou233.cn;");
|
||||||
keys.add("bt.yutou233.cn;");
|
keys.add("bt.yutou233.cn;");
|
||||||
@ -64,7 +61,7 @@ public class UpdateIp {
|
|||||||
if (testIp != null) {
|
if (testIp != null) {
|
||||||
System.out.println("检测到目标:" + line + " 修改IP " + testIp + " > " + ip);
|
System.out.println("检测到目标:" + line + " 修改IP " + testIp + " > " + ip);
|
||||||
if(isNas){
|
if(isNas){
|
||||||
String adminAddress=redisTools.get("adminAddress");
|
String adminAddress=RedisTools.get("adminAddress");
|
||||||
if(adminAddress!=null) {
|
if(adminAddress!=null) {
|
||||||
JSONObject json=JSONObject.parseObject(adminAddress);
|
JSONObject json=JSONObject.parseObject(adminAddress);
|
||||||
line = "proxy_pass http://" +ip+":"+json.getInteger("port")+"/;";
|
line = "proxy_pass http://" +ip+":"+json.getInteger("port")+"/;";
|
||||||
@ -100,41 +97,12 @@ public class UpdateIp {
|
|||||||
JSONObject json = new JSONObject();
|
JSONObject json = new JSONObject();
|
||||||
json.put("code", -1);
|
json.put("code", -1);
|
||||||
json.put("msg", "未登录");
|
json.put("msg", "未登录");
|
||||||
if (Tools.checkWebLogin(request, redisTools) != 1) {
|
if (Tools.checkWebLogin(request) != 1) {
|
||||||
// return json.toJSONString();
|
// return json.toJSONString();
|
||||||
}
|
}
|
||||||
updateList();
|
json.put("code", 0);
|
||||||
File file = new File("/etc/nginx/nginx.conf");
|
json.put("msg", "ok");
|
||||||
//file = new File("D:\\nginx.conf");
|
json.put("data", nas_ip);
|
||||||
if (file.exists()) {
|
|
||||||
String testIp = "0.0.0.0";
|
|
||||||
try {
|
|
||||||
BufferedReader reader = new BufferedReader(new FileReader(file));
|
|
||||||
String line;
|
|
||||||
boolean isIp = false;
|
|
||||||
while ((line = reader.readLine()) != null) {
|
|
||||||
// System.out.println(line.trim().replace("server_name","").replace("upstream","").trim());
|
|
||||||
if (keys.contains(line.trim().replace("server_name", "").replace("upstream", "").replace("{", "").trim())) {
|
|
||||||
isIp = true;
|
|
||||||
}
|
|
||||||
if (isIp) {
|
|
||||||
testIp = testIp(line);
|
|
||||||
if (testIp != null)
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
reader.close();
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
json.put("code", 0);
|
|
||||||
json.put("msg", "ok");
|
|
||||||
json.put("data", testIp);
|
|
||||||
} else {
|
|
||||||
json.put("code", 0);
|
|
||||||
json.put("msg", "没有找到ip");
|
|
||||||
json.put("data", "0.0.0.0");
|
|
||||||
}
|
|
||||||
return json.toJSONString();
|
return json.toJSONString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -19,8 +19,6 @@ import java.util.Enumeration;
|
|||||||
public class APIFilter implements Filter {
|
public class APIFilter implements Filter {
|
||||||
@Resource
|
@Resource
|
||||||
UKeyDao keyDao;
|
UKeyDao keyDao;
|
||||||
@Resource
|
|
||||||
RedisTools redisTools;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
|
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 (cookie != null) {
|
||||||
if ("ok".equals(redisTools.get(cookie.getValue()))) {
|
if ("ok".equals(RedisTools.get(cookie.getValue()))) {
|
||||||
isCookie = true;
|
isCookie = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!isToken) {
|
|
||||||
System.out.println("token验证不通过:" + token);
|
|
||||||
} else if (!isCookie) {
|
|
||||||
System.out.println("请求无令牌,拦截");
|
|
||||||
}
|
|
||||||
if (!isCookie && !isToken) {
|
if (!isCookie && !isToken) {
|
||||||
//response.sendRedirect("/");
|
//response.sendRedirect("/");
|
||||||
if(!request.getRequestURI().contains("/login/")){
|
System.out.println("请求无令牌,拦截");
|
||||||
|
if(!request.getRequestURI().contains("/login/")&&!request.getRequestURI().equals("/favicon.ico")){
|
||||||
response.sendRedirect("/");
|
response.sendRedirect("/");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -12,8 +12,6 @@ import org.springframework.data.redis.core.*;
|
|||||||
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
|
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
|
||||||
import org.springframework.data.redis.serializer.StringRedisSerializer;
|
import org.springframework.data.redis.serializer.StringRedisSerializer;
|
||||||
|
|
||||||
@Configuration
|
|
||||||
@EnableCaching
|
|
||||||
public class RedisConfig extends CachingConfigurerSupport {
|
public class RedisConfig extends CachingConfigurerSupport {
|
||||||
@Bean
|
@Bean
|
||||||
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
|
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
|
||||||
|
@ -1,33 +1,137 @@
|
|||||||
package com.yutou.tools.utils;
|
package com.yutou.tools.utils;
|
||||||
|
|
||||||
import org.springframework.context.annotation.Bean;
|
import java.io.IOException;
|
||||||
import org.springframework.data.redis.core.RedisTemplate;
|
import java.io.InputStream;
|
||||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
import java.util.Properties;
|
||||||
import org.springframework.stereotype.Component;
|
import java.util.Set;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import redis.clients.jedis.Jedis;
|
||||||
import java.util.concurrent.TimeUnit;
|
|
||||||
|
|
||||||
@Component
|
|
||||||
public class RedisTools {
|
public class RedisTools {
|
||||||
@Resource
|
private static String host;
|
||||||
RedisTemplate<String, String> redisTemplate;
|
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);
|
static {
|
||||||
redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
|
//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 static boolean set(int dbIndex,String key,String value){
|
||||||
public String get(String key) {
|
|
||||||
try {
|
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) {
|
} 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;
|
package com.yutou.tools.utils;
|
||||||
|
|
||||||
import com.alibaba.fastjson.JSONArray;
|
import com.alibaba.fastjson.JSONArray;
|
||||||
|
import com.yutou.tools.nas.UpdateIp;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
@ -19,43 +20,48 @@ import java.util.Random;
|
|||||||
|
|
||||||
public class Tools {
|
public class Tools {
|
||||||
/**
|
/**
|
||||||
* 设置Cookie
|
* 设置Cookie
|
||||||
|
*
|
||||||
* @param response
|
* @param response
|
||||||
* @param key
|
* @param key
|
||||||
* @param value
|
* @param value
|
||||||
* @param time
|
* @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);
|
Cookie cookie = new Cookie(key, value);
|
||||||
if(time!=-1) {
|
if (time != -1) {
|
||||||
cookie.setMaxAge(time);
|
cookie.setMaxAge(time);
|
||||||
}
|
}
|
||||||
cookie.setPath("/");
|
cookie.setPath("/");
|
||||||
response.addCookie(cookie);
|
response.addCookie(cookie);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取Cookie
|
* 获取Cookie
|
||||||
|
*
|
||||||
* @param request
|
* @param request
|
||||||
* @param key
|
* @param key
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public static Cookie getCookie(HttpServletRequest request,String key) {
|
public static Cookie getCookie(HttpServletRequest request, String key) {
|
||||||
Cookie[] cookies = request.getCookies();
|
Cookie[] cookies = request.getCookies();
|
||||||
try {
|
try {
|
||||||
for (Cookie cookie : cookies) {
|
for (Cookie cookie : cookies) {
|
||||||
if (key!=null&&cookie.getName().equals(key)) {
|
if (key != null && cookie.getName().equals(key)) {
|
||||||
return cookie;
|
return cookie;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}catch (Exception ignored){
|
} catch (Exception ignored) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 删除Cookie
|
* 删除Cookie
|
||||||
|
*
|
||||||
* @param request
|
* @param request
|
||||||
* @param response
|
* @param response
|
||||||
* @param key
|
* @param key
|
||||||
@ -63,8 +69,8 @@ public class Tools {
|
|||||||
*/
|
*/
|
||||||
public static String deleteCookie(HttpServletRequest request, HttpServletResponse response, String key) {
|
public static String deleteCookie(HttpServletRequest request, HttpServletResponse response, String key) {
|
||||||
for (Cookie cookie : request.getCookies()) {
|
for (Cookie cookie : request.getCookies()) {
|
||||||
if(cookie.getName().equals(key)) {
|
if (cookie.getName().equals(key)) {
|
||||||
System.out.println("删除key="+key);
|
System.out.println("删除key=" + key);
|
||||||
cookie.setMaxAge(0);
|
cookie.setMaxAge(0);
|
||||||
cookie.setPath("/");
|
cookie.setPath("/");
|
||||||
cookie.setValue(null);
|
cookie.setValue(null);
|
||||||
@ -73,29 +79,36 @@ public class Tools {
|
|||||||
}
|
}
|
||||||
return "ok";
|
return "ok";
|
||||||
}
|
}
|
||||||
public static void sendServer(String title,String msg){
|
|
||||||
try{
|
public static void sendServer(String title, String msg) {
|
||||||
System.out.println("title="+title+" msg="+msg);
|
try {
|
||||||
HttpURLConnection connection= (HttpURLConnection) new URL("https://sc.ftqq.com/SCU64034T5adf5c5940dcecc016e0e9d0cf9b1e725da126ff47475.send?text="
|
System.out.println("title=" + title + " msg=" + msg);
|
||||||
+ URLEncoder.encode(title,"UTF-8")+"&desp="+URLEncoder.encode(msg,"UTF-8")).openConnection();
|
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();
|
connection.connect();
|
||||||
InputStream inputStream=connection.getInputStream();
|
InputStream inputStream = connection.getInputStream();
|
||||||
int i=inputStream.read();
|
int i = inputStream.read();
|
||||||
inputStream.close();
|
inputStream.close();
|
||||||
connection.disconnect();
|
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();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取项目路径
|
* 获取项目路径
|
||||||
|
*
|
||||||
* @param request
|
* @param request
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public static String getPath(HttpServletRequest request) {
|
public static String getPath(HttpServletRequest request) {
|
||||||
return request.getServletContext().getRealPath("/") + "/";
|
return request.getServletContext().getRealPath("/") + "/";
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取客户端IP
|
* 获取客户端IP
|
||||||
*
|
*
|
||||||
@ -115,13 +128,12 @@ public class Tools {
|
|||||||
}
|
}
|
||||||
return ip;
|
return ip;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* N以内的不重复随机数
|
* N以内的不重复随机数
|
||||||
*
|
*
|
||||||
* @param min
|
* @param min 最小值
|
||||||
* 最小值
|
* @param max 最大值
|
||||||
* @param max
|
|
||||||
* 最大值
|
|
||||||
* @param n
|
* @param n
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
@ -148,12 +160,13 @@ public class Tools {
|
|||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
public static int checkWebLogin(HttpServletRequest request,RedisTools redisTools){
|
|
||||||
JSONArray array=new JSONArray();
|
public static int checkWebLogin(HttpServletRequest request) {
|
||||||
if(redisTools.get("bean")!=null){
|
JSONArray array = new JSONArray();
|
||||||
array=JSONArray.parseArray(redisTools.get("bean"));
|
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已被封禁");
|
System.out.println("IP已被封禁");
|
||||||
return -100;
|
return -100;
|
||||||
}
|
}
|
||||||
@ -161,57 +174,57 @@ public class Tools {
|
|||||||
if (cookie == null) {
|
if (cookie == null) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
if (redisTools.get(cookie.getValue()).equals("ok")) {
|
if (RedisTools.get(cookie.getValue()).equals("ok")) {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
public static String get(String url){
|
|
||||||
try{
|
public static String get(String url) {
|
||||||
HttpURLConnection connection= (HttpURLConnection) new URL(url).openConnection();
|
try {
|
||||||
BufferedReader reader=new BufferedReader(new InputStreamReader(connection.getInputStream()));
|
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
|
||||||
StringBuilder str= new StringBuilder();
|
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
|
||||||
|
StringBuilder str = new StringBuilder();
|
||||||
String tmp;
|
String tmp;
|
||||||
while ((tmp=reader.readLine())!=null){
|
while ((tmp = reader.readLine()) != null) {
|
||||||
str.append(tmp);
|
str.append(tmp);
|
||||||
}
|
}
|
||||||
reader.close();
|
reader.close();
|
||||||
connection.disconnect();
|
connection.disconnect();
|
||||||
return str.toString();
|
return str.toString();
|
||||||
}catch (Exception e){
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 保存上传的文件
|
* 保存上传的文件
|
||||||
*
|
*
|
||||||
* @param path
|
* @param path 路径
|
||||||
* 路径
|
* @param file 文件
|
||||||
* @param file
|
|
||||||
* 文件
|
|
||||||
* @return
|
* @return
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public static String createFile(String path, MultipartFile file, String newFileName) 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);
|
File servicePath = new File(savePath);
|
||||||
if (!servicePath.exists()) {
|
if (!servicePath.exists()) {
|
||||||
servicePath.mkdirs();
|
servicePath.mkdirs();
|
||||||
}
|
}
|
||||||
String fileName=file.getOriginalFilename();
|
String fileName = file.getOriginalFilename();
|
||||||
if(newFileName!=null) {
|
if (newFileName != null) {
|
||||||
fileName=newFileName;
|
fileName = newFileName;
|
||||||
}
|
}
|
||||||
File saveFile = new File(savePath + "/" + fileName);
|
File saveFile = new File(savePath + "/" + fileName);
|
||||||
if(saveFile.exists()) {
|
if (saveFile.exists()) {
|
||||||
if(!saveFile.delete()) {
|
if (!saveFile.delete()) {
|
||||||
saveFile = new File(savePath + "/" + fileName.replace(".", "_"+new Date().getTime()+"."));
|
saveFile = new File(savePath + "/" + fileName.replace(".", "_" + new Date().getTime() + "."));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
file.transferTo(saveFile);
|
file.transferTo(saveFile);
|
||||||
fileName=URLEncoder.encode(fileName,"UTF-8");
|
fileName = URLEncoder.encode(fileName, "UTF-8");
|
||||||
System.out.println("上传文件保存路径:"+saveFile.getAbsolutePath());
|
System.out.println("上传文件保存路径:" + saveFile.getAbsolutePath());
|
||||||
return path+fileName;
|
return path + fileName;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -19,8 +19,6 @@ import java.util.UUID;
|
|||||||
|
|
||||||
@Controller
|
@Controller
|
||||||
public class userController {
|
public class userController {
|
||||||
@Resource
|
|
||||||
RedisTools redisTools;
|
|
||||||
|
|
||||||
@RequestMapping("/login/check.do")
|
@RequestMapping("/login/check.do")
|
||||||
@ResponseBody
|
@ResponseBody
|
||||||
@ -29,8 +27,8 @@ public class userController {
|
|||||||
json.put("code", -1);
|
json.put("code", -1);
|
||||||
json.put("msg", "未登录");
|
json.put("msg", "未登录");
|
||||||
JSONArray array = new JSONArray();
|
JSONArray array = new JSONArray();
|
||||||
if (redisTools.get("ban") != null) {
|
if (RedisTools.get("ban") != null) {
|
||||||
array = JSONArray.parseArray(redisTools.get("ban"));
|
array = JSONArray.parseArray(RedisTools.get("ban"));
|
||||||
}
|
}
|
||||||
if (array.contains(Tools.getRemoteAddress(request))) {
|
if (array.contains(Tools.getRemoteAddress(request))) {
|
||||||
json.put("code", -2);
|
json.put("code", -2);
|
||||||
@ -42,7 +40,7 @@ public class userController {
|
|||||||
if (cookie == null) {
|
if (cookie == null) {
|
||||||
return json.toJSONString();
|
return json.toJSONString();
|
||||||
}
|
}
|
||||||
if ("ok".equals(redisTools.get(cookie.getValue()))) {
|
if ("ok".equals(RedisTools.get(cookie.getValue()))) {
|
||||||
json.put("code", 0);
|
json.put("code", 0);
|
||||||
json.put("msg", "登录成功");
|
json.put("msg", "登录成功");
|
||||||
return json.toJSONString();
|
return json.toJSONString();
|
||||||
@ -56,8 +54,8 @@ public class userController {
|
|||||||
@ResponseBody
|
@ResponseBody
|
||||||
public String captcha(HttpServletRequest request) {
|
public String captcha(HttpServletRequest request) {
|
||||||
JSONArray array = new JSONArray();
|
JSONArray array = new JSONArray();
|
||||||
if (redisTools.get("ban") != null) {
|
if (RedisTools.get("ban") != null) {
|
||||||
array = JSONArray.parseArray(redisTools.get("ban"));
|
array = JSONArray.parseArray(RedisTools.get("ban"));
|
||||||
}
|
}
|
||||||
if (array.contains(Tools.getRemoteAddress(request))) {
|
if (array.contains(Tools.getRemoteAddress(request))) {
|
||||||
|
|
||||||
@ -69,9 +67,9 @@ public class userController {
|
|||||||
for (int value : captcha) {
|
for (int value : captcha) {
|
||||||
cc += value;
|
cc += value;
|
||||||
}
|
}
|
||||||
redisTools.set("login", cc, 5 * 60 * 1000);
|
RedisTools.set("login", cc, 5 * 60 * 1000);
|
||||||
String token=UUID.randomUUID().toString().replace("-","");
|
String token=UUID.randomUUID().toString().replace("-","");
|
||||||
redisTools.set(token,Tools.getRemoteAddress(request),10 * 60 * 1000);
|
RedisTools.set(token,Tools.getRemoteAddress(request),10 * 60 * 1000);
|
||||||
String url="http://tools.yutou233.cn/login/ban.do?token="+token;
|
String url="http://tools.yutou233.cn/login/ban.do?token="+token;
|
||||||
Tools.sendServer("管理后台登录验证码", "本次登录验证码为:" + cc
|
Tools.sendServer("管理后台登录验证码", "本次登录验证码为:" + cc
|
||||||
+ ",登录IP:" + Tools.getRemoteAddress(request)
|
+ ",登录IP:" + Tools.getRemoteAddress(request)
|
||||||
@ -81,14 +79,14 @@ public class userController {
|
|||||||
@RequestMapping("/login/ban.do")
|
@RequestMapping("/login/ban.do")
|
||||||
@ResponseBody
|
@ResponseBody
|
||||||
public String banIp(String token){
|
public String banIp(String token){
|
||||||
String ip=redisTools.get(token);
|
String ip=RedisTools.get(token);
|
||||||
if(ip!=null){
|
if(ip!=null){
|
||||||
JSONArray array = new JSONArray();
|
JSONArray array = new JSONArray();
|
||||||
if (redisTools.get("ban") != null) {
|
if (RedisTools.get("ban") != null) {
|
||||||
array = JSONArray.parseArray(redisTools.get("bean"));
|
array = JSONArray.parseArray(RedisTools.get("bean"));
|
||||||
}
|
}
|
||||||
array.add(ip);
|
array.add(ip);
|
||||||
redisTools.set("ban",array.toJSONString());
|
RedisTools.set("ban",array.toJSONString());
|
||||||
return "已封禁";
|
return "已封禁";
|
||||||
}
|
}
|
||||||
return "ERROR";
|
return "ERROR";
|
||||||
@ -98,10 +96,10 @@ public class userController {
|
|||||||
@ResponseBody
|
@ResponseBody
|
||||||
public String login(HttpServletResponse response, String code) {
|
public String login(HttpServletResponse response, String code) {
|
||||||
JSONObject json = new JSONObject();
|
JSONObject json = new JSONObject();
|
||||||
if (redisTools.get("login").equals(code.trim())) {
|
if (RedisTools.get("login").equals(code.trim())) {
|
||||||
String uuid = UUID.randomUUID().toString();
|
String uuid = UUID.randomUUID().toString();
|
||||||
Tools.setCookie(response, "user", uuid.replace("-", ""), 30 * 24 * 60 * 60);
|
Tools.setCookie(response, "user", uuid.replace("-", ""), 30 * 24 * 60 * 60);
|
||||||
redisTools.set(uuid.replace("-", ""), "ok", 30 * 24 * 60 * 60);
|
RedisTools.set(uuid.replace("-", ""), "ok", 30 * 24 * 60 * 60);
|
||||||
json.put("code", 0);
|
json.put("code", 0);
|
||||||
json.put("msg", "登录成功");
|
json.put("msg", "登录成功");
|
||||||
return json.toJSONString();
|
return json.toJSONString();
|
||||||
@ -119,8 +117,8 @@ public class userController {
|
|||||||
json.put("code", -1);
|
json.put("code", -1);
|
||||||
json.put("msg", "退出失败");
|
json.put("msg", "退出失败");
|
||||||
if (cookie != null) {
|
if (cookie != null) {
|
||||||
if ("ok".equals(redisTools.get(cookie.getValue()))) {
|
if ("ok".equals(RedisTools.get(cookie.getValue()))) {
|
||||||
redisTools.set(cookie.getValue(), "ok", 1);
|
RedisTools.set(cookie.getValue(), "ok", 1);
|
||||||
Tools.deleteCookie(request, response, "user");
|
Tools.deleteCookie(request, response, "user");
|
||||||
json.put("code", 0);
|
json.put("code", 0);
|
||||||
json.put("msg", "退出成功");
|
json.put("msg", "退出成功");
|
||||||
|
Loading…
Reference in New Issue
Block a user