mirror of
https://github.com/dqjdda/Aurora.git
synced 2018-11-06 05:33:29 +00:00
工具类优化,删除无用filter
This commit is contained in:
@@ -6,8 +6,8 @@ import me.aurora.domain.ResponseEntity;
|
||||
import me.aurora.domain.User;
|
||||
import me.aurora.service.MenuService;
|
||||
import me.aurora.service.UserService;
|
||||
import me.aurora.util.EncryptHelper;
|
||||
import me.aurora.util.HttpContextUtils;
|
||||
import me.aurora.util.MD5Utils;
|
||||
import org.apache.shiro.SecurityUtils;
|
||||
import org.apache.shiro.authc.*;
|
||||
import org.apache.shiro.subject.Subject;
|
||||
@@ -35,7 +35,7 @@ public class UserSecurityController {
|
||||
@PostMapping(value = "/login")
|
||||
public ResponseEntity login(@RequestParam String username, @RequestParam String password, @RequestParam Boolean rememberMe){
|
||||
log.warn("REST request to login User: "+username);
|
||||
password = MD5Utils.encrypt(password);
|
||||
password = EncryptHelper.encrypt(password);
|
||||
UsernamePasswordToken token = new UsernamePasswordToken(username, password, rememberMe);
|
||||
Subject subject = SecurityUtils.getSubject();
|
||||
try {
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package me.aurora.config.baidu.ueditor.upload;
|
||||
|
||||
import me.aurora.config.baidu.ueditor.PathFormat;
|
||||
import me.aurora.config.baidu.ueditor.define.AppInfo;
|
||||
import me.aurora.config.baidu.ueditor.define.BaseState;
|
||||
import me.aurora.config.baidu.ueditor.define.State;
|
||||
@@ -9,13 +8,9 @@ import me.aurora.domain.User;
|
||||
import me.aurora.service.PictureService;
|
||||
import me.aurora.util.FileUtil;
|
||||
import me.aurora.util.SpringContextUtils;
|
||||
import org.apache.commons.fileupload.servlet.ServletFileUpload;
|
||||
import org.apache.shiro.SecurityUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.HttpRequestHandler;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import org.springframework.web.multipart.MultipartHttpServletRequest;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.util.Map;
|
||||
|
||||
|
||||
@@ -1,41 +0,0 @@
|
||||
package me.aurora.config.filter;
|
||||
|
||||
import javax.servlet.*;
|
||||
import javax.servlet.annotation.WebFilter;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpSession;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* 将项目路径装入session
|
||||
* @author 郑杰
|
||||
* @date 2018/08/20 13:41:13
|
||||
*/
|
||||
@WebFilter(filterName = "pathFilter",urlPatterns = "/*")
|
||||
public class PathFilter implements Filter {
|
||||
|
||||
@Override
|
||||
public void init(FilterConfig filterConfig) throws ServletException {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
|
||||
HttpServletRequest httpServletRequest=(HttpServletRequest)request;
|
||||
/**
|
||||
*request.getSchema()可以返回当前页面使用的协议
|
||||
*request.getServerName()可以返回当前页面所在的服务器的名字
|
||||
*request.getServerPort()可以返回当前页面所在的服务器使用的端口
|
||||
*request.getContextPath()可以返回当前页面所在的应用的名字
|
||||
*这四个拼装起来,就是当前应用的跟路径了
|
||||
*/
|
||||
// +":"+request.getServerPort()
|
||||
String path=request.getScheme()+"://"+request.getServerName();
|
||||
HttpSession httpSession= httpServletRequest.getSession();
|
||||
httpServletRequest.getSession().setAttribute("path",path);
|
||||
chain.doFilter(httpServletRequest, response);
|
||||
}
|
||||
@Override
|
||||
public void destroy() {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -71,7 +71,7 @@ public class PictureServiceImpl implements PictureService {
|
||||
}
|
||||
//转成实体类
|
||||
picture = JSON.parseObject(jsonObject.get("data").toString(), Picture.class);
|
||||
picture.setSize(SizeUtil.getSize(Integer.valueOf(picture.getSize())));
|
||||
picture.setSize(FileUtil.getSize(Integer.valueOf(picture.getSize())));
|
||||
picture.setUser(user);
|
||||
picture.setFilename(FileUtil.getFileNameNoEx(multipartFile.getOriginalFilename()));
|
||||
pictureRepo.save(picture);
|
||||
|
||||
@@ -19,9 +19,9 @@ import me.aurora.repository.QiNiuConfigRepo;
|
||||
import me.aurora.repository.QiniuContentRepo;
|
||||
import me.aurora.repository.spec.QiNiuContentSpec;
|
||||
import me.aurora.service.QiNiuService;
|
||||
import me.aurora.util.FileUtil;
|
||||
import me.aurora.util.PageUtil;
|
||||
import me.aurora.util.QiNiuUtil;
|
||||
import me.aurora.util.SizeUtil;
|
||||
import me.aurora.util.ValidationUtil;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.domain.Page;
|
||||
@@ -96,7 +96,7 @@ public class QiNiuServiceImpl implements QiNiuService {
|
||||
qiniuContent.setType(qiniuConfig.getType());
|
||||
qiniuContent.setKey(putRet.key);
|
||||
qiniuContent.setUrl(qiniuConfig.getHost()+"/"+putRet.key);
|
||||
qiniuContent.setSize(SizeUtil.getSize(Integer.parseInt(file.getSize()+"")));
|
||||
qiniuContent.setSize(FileUtil.getSize(Integer.parseInt(file.getSize()+"")));
|
||||
qiniuContentRepo.save(qiniuContent);
|
||||
} catch (Exception e) {
|
||||
throw new AuroraException(HttpStatus.HTTP_INTERNAL_ERROR,e.getMessage());
|
||||
@@ -165,7 +165,7 @@ public class QiNiuServiceImpl implements QiNiuService {
|
||||
for (FileInfo item : items) {
|
||||
if(qiniuContentRepo.findByKey(item.key) == null){
|
||||
qiniuContent = new QiniuContent();
|
||||
qiniuContent.setSize(SizeUtil.getSize(Integer.parseInt(item.fsize+"")));
|
||||
qiniuContent.setSize(FileUtil.getSize(Integer.parseInt(item.fsize+"")));
|
||||
qiniuContent.setKey(item.key);
|
||||
qiniuContent.setType(config.getType());
|
||||
qiniuContent.setBucket(config.getBucket());
|
||||
|
||||
@@ -84,7 +84,7 @@ public class SysLogServiceImpl implements SysLogService {
|
||||
// 获取request
|
||||
HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
|
||||
// 设置IP地址
|
||||
sysLog.setIp(IPUtils.getIpAddr(request));
|
||||
sysLog.setIp(AddressUtils.getIpAddr(request));
|
||||
sysLog.setLocation(AddressUtils.getCityInfo(sysLog.getIp()));
|
||||
// 用户名
|
||||
User user = (User) SecurityUtils.getSubject().getPrincipal();
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package me.aurora.service.impl;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.http.HttpStatus;
|
||||
import me.aurora.config.AuroraProperties;
|
||||
import me.aurora.domain.Role;
|
||||
@@ -11,7 +10,7 @@ import me.aurora.service.RoleService;
|
||||
import me.aurora.service.UserService;
|
||||
import me.aurora.service.dto.UserDTO;
|
||||
import me.aurora.service.mapper.UserMapper;
|
||||
import me.aurora.util.MD5Utils;
|
||||
import me.aurora.util.EncryptHelper;
|
||||
import me.aurora.util.PageUtil;
|
||||
import me.aurora.config.exception.AuroraException;
|
||||
import me.aurora.util.ValidationUtil;
|
||||
@@ -21,7 +20,6 @@ import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
import java.util.*;
|
||||
|
||||
@@ -84,7 +82,7 @@ public class UserServiceImpl implements UserService {
|
||||
if(userRepo.findByEmail(user.getEmail())!=null){
|
||||
throw new AuroraException(HttpStatus.HTTP_BAD_REQUEST,"用户邮箱已存在");
|
||||
}
|
||||
user.setPassword(MD5Utils.encrypt(user.getPassword()));
|
||||
user.setPassword(EncryptHelper.encrypt(user.getPassword()));
|
||||
Set<Role> roleSet = new HashSet<>();
|
||||
for(String roleId:roles.split(",")){
|
||||
Role role = new Role();
|
||||
@@ -107,7 +105,7 @@ public class UserServiceImpl implements UserService {
|
||||
throw new AuroraException(HttpStatus.HTTP_BAD_REQUEST,"用户邮箱已存在");
|
||||
}
|
||||
old = userRepo.findById(user.getId()).get();
|
||||
old.setPassword(MD5Utils.encrypt(user.getPassword()));
|
||||
old.setPassword(EncryptHelper.encrypt(user.getPassword()));
|
||||
old.setUsername(user.getUsername());
|
||||
old.setEmail(user.getEmail());
|
||||
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
package me.aurora.util;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.lionsoul.ip2region.DataBlock;
|
||||
import org.lionsoul.ip2region.DbConfig;
|
||||
import org.lionsoul.ip2region.DbSearcher;
|
||||
import org.lionsoul.ip2region.Util;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.io.File;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
@@ -15,13 +15,37 @@ import java.lang.reflect.Method;
|
||||
* @author 郑杰
|
||||
* @date 2018/09/18 9:48:00
|
||||
*/
|
||||
@Slf4j
|
||||
public class AddressUtils {
|
||||
|
||||
private AddressUtils() {
|
||||
}
|
||||
|
||||
private static Logger log = LoggerFactory.getLogger(AddressUtils.class);
|
||||
/**
|
||||
* 获取IP地址
|
||||
* 使用Nginx等反向代理软件, 则不能通过request.getRemoteAddr()获取IP地址
|
||||
* 如果使用了多级反向代理的话,X-Forwarded-For的值并不止一个,而是一串IP地址,X-Forwarded-For中第一个非unknown的有效IP字符串,则为真实IP地址
|
||||
*/
|
||||
public static String getIpAddr(HttpServletRequest request) {
|
||||
|
||||
String ip = request.getHeader("x-forwarded-for");
|
||||
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
|
||||
ip = request.getHeader("Proxy-Client-IP");
|
||||
}
|
||||
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
|
||||
ip = request.getHeader("WL-Proxy-Client-IP");
|
||||
}
|
||||
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
|
||||
ip = request.getRemoteAddr();
|
||||
}
|
||||
return "0:0:0:0:0:0:0:1".equals(ip) ? "127.0.0.1" : ip;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据ip获取详细地址
|
||||
* @param ip
|
||||
* @return
|
||||
*/
|
||||
public static String getCityInfo(String ip) {
|
||||
try {
|
||||
Resource resource = new ClassPathResource("ip2region/ip2region.db");
|
||||
@@ -53,6 +77,9 @@ public class AddressUtils {
|
||||
}
|
||||
dataBlock = (DataBlock) method.invoke(searcher, ip);
|
||||
String address = dataBlock.getRegion().replace("0|","");
|
||||
if(address.charAt(address.length()-1) == '|'){
|
||||
address = address.substring(0,address.length() - 1);
|
||||
}
|
||||
/**
|
||||
* 太难看了,转换下
|
||||
*/
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
package me.aurora.util;
|
||||
import org.apache.shiro.crypto.hash.SimpleHash;
|
||||
import org.apache.shiro.util.ByteSource;
|
||||
|
||||
import javax.crypto.Cipher;
|
||||
import javax.crypto.SecretKey;
|
||||
import javax.crypto.SecretKeyFactory;
|
||||
@@ -6,14 +9,26 @@ import javax.crypto.spec.DESKeySpec;
|
||||
import javax.crypto.spec.IvParameterSpec;
|
||||
|
||||
/**
|
||||
* @date 2017/10/26.
|
||||
* Github Breakeval13
|
||||
* @author blog firsh.me
|
||||
* @date 2018/10/03 19:02:51
|
||||
* @author ~
|
||||
* @update 郑杰
|
||||
*/
|
||||
public class EncryptHelper {
|
||||
|
||||
private static String strKey = "Passw0rd", strParam = "Passw0rd";
|
||||
|
||||
private static final String SALT = "aurora";
|
||||
|
||||
private static final String ALGORITH_NAME = "md5";
|
||||
|
||||
private static final int HASH_ITERATIONS = 2;
|
||||
|
||||
/**
|
||||
* 对称加密
|
||||
* @param source
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public static String desEncrypt(String source) throws Exception {
|
||||
if (source == null || source.length() == 0){
|
||||
return null;
|
||||
@@ -28,11 +43,9 @@ public class EncryptHelper {
|
||||
cipher.doFinal(source.getBytes("UTF-8"))).toUpperCase();
|
||||
}
|
||||
|
||||
|
||||
public static String byte2hex(byte[] inStr) {
|
||||
String stmp;
|
||||
StringBuffer out = new StringBuffer(inStr.length * 2);
|
||||
|
||||
for (int n = 0; n < inStr.length; n++) {
|
||||
stmp = Integer.toHexString(inStr[n] & 0xFF);
|
||||
if (stmp.length() == 1) {
|
||||
@@ -58,6 +71,12 @@ public class EncryptHelper {
|
||||
return b2;
|
||||
}
|
||||
|
||||
/**
|
||||
* 对称解密
|
||||
* @param source
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public static String desDecrypt(String source) throws Exception {
|
||||
if (source == null || source.length() == 0){
|
||||
return null;
|
||||
@@ -73,18 +92,24 @@ public class EncryptHelper {
|
||||
return new String(retByte);
|
||||
}
|
||||
|
||||
public static String formatKey(String key){
|
||||
// 固定长度
|
||||
int length=8;
|
||||
// 自动补位
|
||||
return key+String.format("%1$0"+(length-key.length())+"d",0);
|
||||
/**
|
||||
* MD5加盐加密
|
||||
* @param pswd
|
||||
* @return
|
||||
*/
|
||||
public static String encrypt(String pswd) {
|
||||
return new SimpleHash(ALGORITH_NAME, pswd, ByteSource.Util.bytes(SALT), HASH_ITERATIONS).toHex();
|
||||
}
|
||||
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
System.out.println(EncryptHelper.desDecrypt("53c8927a4dd1e9b6f44c9d53af92c794"));
|
||||
System.out.println(EncryptHelper.desEncrypt("Hello World!"));
|
||||
|
||||
/**
|
||||
* MD5加盐加密
|
||||
* @param username
|
||||
* @param pswd
|
||||
* @return
|
||||
*/
|
||||
public static String encrypt(String username, String pswd) {
|
||||
return new SimpleHash(ALGORITH_NAME, pswd, ByteSource.Util.bytes(username.toLowerCase() + SALT),
|
||||
HASH_ITERATIONS).toHex();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.text.DecimalFormat;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
@@ -13,6 +14,24 @@ import java.util.UUID;
|
||||
*/
|
||||
public class FileUtil {
|
||||
|
||||
/**
|
||||
* 定义GB的计算常量
|
||||
*/
|
||||
private static final int GB = 1024 * 1024 * 1024;
|
||||
/**
|
||||
* 定义MB的计算常量
|
||||
*/
|
||||
private static final int MB = 1024 * 1024;
|
||||
/**
|
||||
* 定义KB的计算常量
|
||||
*/
|
||||
private static final int KB = 1024;
|
||||
|
||||
/**
|
||||
* 格式化小数
|
||||
*/
|
||||
private static final DecimalFormat DF = new DecimalFormat("0.00");
|
||||
|
||||
/**
|
||||
* MultipartFile转File
|
||||
* @param multipartFile
|
||||
@@ -76,4 +95,26 @@ public class FileUtil {
|
||||
}
|
||||
return filename;
|
||||
}
|
||||
|
||||
/**
|
||||
* 文件大小转换
|
||||
* @param size
|
||||
* @return
|
||||
*/
|
||||
public static String getSize(int size){
|
||||
String resultSize = "";
|
||||
if (size / GB >= 1) {
|
||||
//如果当前Byte的值大于等于1GB
|
||||
resultSize = DF.format(size / (float) GB) + "GB ";
|
||||
} else if (size / MB >= 1) {
|
||||
//如果当前Byte的值大于等于1MB
|
||||
resultSize = DF.format(size / (float) MB) + "MB ";
|
||||
} else if (size / KB >= 1) {
|
||||
//如果当前Byte的值大于等于1KB
|
||||
resultSize = DF.format(size / (float) KB) + "KB ";
|
||||
} else {
|
||||
resultSize = size + "B ";
|
||||
}
|
||||
return resultSize;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,31 +0,0 @@
|
||||
package me.aurora.util;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
/**
|
||||
* @author 郑杰
|
||||
* @date 2018/08/23 11:54:10
|
||||
*/
|
||||
public class IPUtils {
|
||||
|
||||
/**
|
||||
* 获取IP地址
|
||||
* 使用Nginx等反向代理软件, 则不能通过request.getRemoteAddr()获取IP地址
|
||||
* 如果使用了多级反向代理的话,X-Forwarded-For的值并不止一个,而是一串IP地址,X-Forwarded-For中第一个非unknown的有效IP字符串,则为真实IP地址
|
||||
*/
|
||||
public static String getIpAddr(HttpServletRequest request) {
|
||||
|
||||
String ip = request.getHeader("x-forwarded-for");
|
||||
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
|
||||
ip = request.getHeader("Proxy-Client-IP");
|
||||
}
|
||||
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
|
||||
ip = request.getHeader("WL-Proxy-Client-IP");
|
||||
}
|
||||
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
|
||||
ip = request.getRemoteAddr();
|
||||
}
|
||||
return "0:0:0:0:0:0:0:1".equals(ip) ? "127.0.0.1" : ip;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,26 +0,0 @@
|
||||
package me.aurora.util;
|
||||
|
||||
import org.apache.shiro.crypto.hash.SimpleHash;
|
||||
import org.apache.shiro.util.ByteSource;
|
||||
|
||||
/**
|
||||
* @author 郑杰
|
||||
* @date 2018/08/23 11:54:10
|
||||
*/
|
||||
public class MD5Utils {
|
||||
|
||||
private static final String SALT = "aurora";
|
||||
|
||||
private static final String ALGORITH_NAME = "md5";
|
||||
|
||||
private static final int HASH_ITERATIONS = 2;
|
||||
|
||||
public static String encrypt(String pswd) {
|
||||
return new SimpleHash(ALGORITH_NAME, pswd, ByteSource.Util.bytes(SALT), HASH_ITERATIONS).toHex();
|
||||
}
|
||||
|
||||
public static String encrypt(String username, String pswd) {
|
||||
return new SimpleHash(ALGORITH_NAME, pswd, ByteSource.Util.bytes(username.toLowerCase() + SALT),
|
||||
HASH_ITERATIONS).toHex();
|
||||
}
|
||||
}
|
||||
@@ -1,48 +0,0 @@
|
||||
package me.aurora.util;
|
||||
|
||||
import java.text.DecimalFormat;
|
||||
|
||||
/**
|
||||
* 字节转换工具
|
||||
* @author 郑杰
|
||||
* @date 2018/09/20 14:01:28
|
||||
*/
|
||||
public class SizeUtil {
|
||||
|
||||
/**
|
||||
* 定义GB的计算常量
|
||||
*/
|
||||
private static final int GB = 1024 * 1024 * 1024;
|
||||
/**
|
||||
* 定义MB的计算常量
|
||||
*/
|
||||
private static final int MB = 1024 * 1024;
|
||||
/**
|
||||
* 定义KB的计算常量
|
||||
*/
|
||||
private static final int KB = 1024;
|
||||
|
||||
/**
|
||||
* 格式化小数
|
||||
*/
|
||||
private static final DecimalFormat DF = new DecimalFormat("0.00");
|
||||
|
||||
public static String getSize(int size){
|
||||
String resultSize = "";
|
||||
if (size / GB >= 1) {
|
||||
//如果当前Byte的值大于等于1GB
|
||||
resultSize = DF.format(size / (float) GB) + "GB ";
|
||||
} else if (size / MB >= 1) {
|
||||
//如果当前Byte的值大于等于1MB
|
||||
resultSize = DF.format(size / (float) MB) + "MB ";
|
||||
} else if (size / KB >= 1) {
|
||||
//如果当前Byte的值大于等于1KB
|
||||
resultSize = DF.format(size / (float) KB) + "KB ";
|
||||
} else {
|
||||
resultSize = size + "B ";
|
||||
}
|
||||
return resultSize;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -7,7 +7,6 @@ import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* Spring Context 工具类
|
||||
*
|
||||
* @author MrBird
|
||||
*
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user