package me.wuwenbin.noteblogv4.util;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import me.wuwenbin.noteblogv4.config.application.NBContext;
import me.wuwenbin.noteblogv4.exception.MethodNotMatchException;
import me.wuwenbin.noteblogv4.model.constant.NoteBlogV4;
import me.wuwenbin.noteblogv4.model.constant.Upload;
import me.wuwenbin.noteblogv4.model.entity.permission.NBSysUser;
import me.wuwenbin.noteblogv4.model.pojo.business.Base64MultipartFile;
import me.wuwenbin.noteblogv4.model.pojo.business.IpInfo;
import me.wuwenbin.noteblogv4.service.param.ParamService;
import me.wuwenbin.noteblogv4.service.upload.UploadService;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.MultipartFile;
import sun.misc.BASE64Decoder;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.*;
import java.util.regex.Pattern;
import static java.util.regex.Pattern.*;
import static org.springframework.util.StringUtils.isEmpty;
/**
* created by Wuwenbin on 2018/7/14 at 10:33
*
* @author wuwenbin
*/
@Component
@Slf4j
public class NBUtils implements ApplicationContextAware {
private static ApplicationContext applicationContext = null;
private static final String UNKNOWN = "unknown";
/**
* 获取实际ip地址
*
* @param request
* @return
*/
public static String getRemoteAddress(HttpServletRequest request) {
String remoteAddress;
try {
remoteAddress = request.getHeader("x-forwarded-for");
if (StringUtils.isEmpty(remoteAddress) || UNKNOWN.equalsIgnoreCase(remoteAddress)) {
remoteAddress = request.getHeader("Proxy-Client-IP");
}
if (StringUtils.isEmpty(remoteAddress) || remoteAddress.length() == 0 || UNKNOWN.equalsIgnoreCase(remoteAddress)) {
remoteAddress = request.getHeader("WL-Proxy-Client-IP");
}
if (StringUtils.isEmpty(remoteAddress) || UNKNOWN.equalsIgnoreCase(remoteAddress)) {
remoteAddress = request.getHeader("HTTP_CLIENT_IP");
}
if (StringUtils.isEmpty(remoteAddress) || UNKNOWN.equalsIgnoreCase(remoteAddress)) {
remoteAddress = request.getHeader("HTTP_X_FORWARDED_FOR");
}
if (StringUtils.isEmpty(remoteAddress) || UNKNOWN.equalsIgnoreCase(remoteAddress)) {
remoteAddress = request.getRemoteAddr();
}
} catch (Exception var3) {
remoteAddress = request.getRemoteAddr();
}
return remoteAddress;
}
/**
* 删除不必要的信息,避免暴露过多信息
*
* @param user
* @return
*/
public static Map<Object, Object> user2Map(NBSysUser user) {
if (user == null) {
return null;
}
return MapUtil.of(new Object[][]{
{"id", user.getId()},
{"nickname", user.getNickname()},
{"avatar", user.getAvatar()},
{"dri", user.getDefaultRoleId()}
});
}
/**
* 根据当前请求获取用户对象
*
* @param
* @return
*/
public static NBSysUser getSessionUser() {
Cookie cookie = CookieUtils.getCookie(getCurrentRequest(), NoteBlogV4.Session.SESSION_ID_COOKIE);
if (cookie != null) {
String sessionId = cookie.getValue();
return applicationContext.getBean(NBContext.class).getSessionUser(sessionId);
}
return null;
}
/**
* 获取当前的request对象
*
* @return
*/
public static HttpServletRequest getCurrentRequest() {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
return Objects.requireNonNull(attributes).getRequest();
}
/**
* 获取工程的发布路径根目录
* 即classes的绝对路径
* file:/E:/idea_workplace/target/classes/
*
* @return
*/
public static String getClassesPath() {
return Objects.requireNonNull(NBUtils.class.getClassLoader().getResource("")).getPath();
}
/**
* 获取改文件在工程中所在的完整绝对路径
*
* @param filePath 相对classes的路径
* @return
*/
public static String getFilePathInClassesPath(String filePath) {
return getClassesPath() + filePath;
}
/**
* 判断是否为ajax请求
*
* @param request
* @return
*/
public static boolean isAjaxRequest(HttpServletRequest request) {
return StrUtil.isNotBlank(request.getHeader("x-requested-with")) && "XMLHttpRequest".equals(request.getHeader("x-requested-with"));
}
/**
* 是否为 json 请求
*
* @param request
* @return
*/
public static boolean isJson(HttpServletRequest request) {
String headerAccept = request.getHeader("Accept");
return !isEmpty(headerAccept) && headerAccept.contains("application/json");
}
/**
* 是否为get请求
*
* @param request
* @return
*/
public static boolean isGetRequest(HttpServletRequest request) {
String method = request.getMethod();
return "GET".equalsIgnoreCase(method);
}
/**
* 是否为 router 请求
*
* @param request
* @return
*/
public static boolean isRouterRequest(HttpServletRequest request) {
String headerAccept = request.getHeader("Accept");
return !isEmpty(headerAccept) && headerAccept.contains("text/html") && !isJson(request) && isAjaxRequest(request) && isGetRequest(request);
}
/**
* 获取Bean
*
* @param tClass
* @param <T>
* @return
*/
public static <T> T getBean(Class<T> tClass) {
return applicationContext.getBean(tClass);
}
/**
* http请求获取ip对象
*
* @param ip
* @return
*/
public static IpInfo getIpInfo(String ip) {
String url = "http://ip.taobao.com/service/getIpInfo.php?ip=" + ip;
String resp = HttpUtil.get(url);
log.info("请求 ip.taobao.com,请求参数:{}", ip);
try {
JSONObject jsonObject = JSONUtil.parseObj(resp);
return jsonObject.toBean(IpInfo.class);
} catch (Exception e) {
log.error("返回结果:" + resp);
log.error("解析json出错,返回结果有误", e);
return IpInfo.builder().data(null).code(1).build();
}
}
/**
* 获取ip地理位置信息
*
* @param ipInfo
* @return
*/
public static String getIpCnInfo(IpInfo ipInfo) {
String undefined = "x";
if (ipInfo == null || ipInfo.getData() == null) {
return "未知位置";
}
boolean isLocal = ipInfo.getData().getIp().contains("127.0.0.1") || ipInfo.getData().getIp().contains("localhost");
String temp = ipInfo.getData().getCountry() + ipInfo.getData().getRegion() + ipInfo.getData().getCity();
if (!ipInfo.getData().getCounty().toLowerCase().contains(undefined) && !isLocal) {
return temp + ipInfo.getData().getCounty();
} else {
return temp;
}
}
/**
* 返回值类型为Map<String, Object>
*
* @param properties
* @return
*/
publ
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
运行环境 jdk8+mysql+IntelliJ IDEA+maven 项目技术 spring+springboot+mybatis+layui+jquery 步骤 1.将项目导入到开发工具IDEA中 2.将数据库脚本语句粘贴至mysql工具中,并执行导入 3.noteblogv4\application-noteblogv4.properties配置文件,修改第29-33行,改为自己的数据库连接地址 4.run as启动noteblogv4\src\main\java\me\wuwenbin\noteblogv4\NoteBlogV4Application.java文件,运行springboot项目 5.浏览器访问地址http://localhost:8089 ,进入博客主页,进入会员中心->后台管理,账户admin 密码111111 ,即可进入博客后台管理 注:如遇到数据库脚本导入失败,请使用mysql5.7版本 项目需要maven支持,需提前配置好springboot项目所需环境方可运行
资源推荐
资源详情
资源评论
收起资源包目录
基于Java web springboot+layui的博客系统设计与实现(源码+数据库+截图).zip (2000个子文件)
AUTHORS 6KB
AUTHORS 6KB
InitListener.class 19KB
ArticleServiceImpl.class 19KB
NBUtils.class 15KB
BaseController.class 15KB
NBArticle.class 14KB
ArticleController.class 13KB
MenuListener.class 12KB
AuthorityServiceImpl.class 11KB
NoteController.class 11KB
AuthorityController.class 11KB
EntranceController.class 11KB
IndexController.class 10KB
SettingsController.class 10KB
ProfileController.class 10KB
MessageController.class 10KB
ResourceListener.class 10KB
AdminTagController.class 9KB
QiniuUploadServiceImpl.class 9KB
AdminKeywordController.class 9KB
AdminCloudFileCateController.class 9KB
AdminProjectCateController.class 9KB
NoteServiceImpl.class 9KB
AdminCateController.class 9KB
CloudFileController.class 9KB
ProjectController.class 9KB
NBSession.class 9KB
LayuiXTree.class 9KB
NBSysUser.class 9KB
NBAuthAspect.class 8KB
UserController.class 8KB
NBSysMenu.class 8KB
ArticleController.class 8KB
CommentController.class 8KB
NBComment.class 8KB
IpInfo$Info.class 7KB
NBMessage.class 7KB
AdminMessageController.class 7KB
AdminCommentController.class 7KB
NBNote.class 7KB
ApplicationInterceptor.class 7KB
UsersServiceImpl.class 7KB
DashboardServiceImpl.class 7KB
MenuTree.class 7KB
CommentServiceImpl.class 7KB
NBProject.class 7KB
MessageServiceImpl.class 6KB
NBLogger.class 6KB
QqLoginServiceImpl.class 6KB
CloudFileController.class 6KB
NBCloudFile.class 6KB
HelloWorldListener.class 6KB
ErrorController.class 6KB
WebMvcConfig.class 6KB
NBArticle$NBArticleBuilder.class 5KB
NBSysResource.class 5KB
SqlGenerator.class 5KB
NBContext.class 5KB
UploadController.class 5KB
NBParam.class 5KB
Pagination.class 5KB
ProjectController.class 5KB
NBPanel.class 5KB
UploadService.class 5KB
SettingsServiceImpl.class 5KB
CookieUtils.class 5KB
UploadFolderListener.class 4KB
LocalUploadServiceImpl.class 4KB
NBAbout.class 4KB
NBUpload.class 4KB
NBTagRefer.class 4KB
InitController.class 4KB
NBFunction.class 4KB
NoteBlogV4ApplicationTests.class 4KB
NoteController.class 4KB
SimpleLoginServiceImpl.class 4KB
SimpleLoginData.class 4KB
NBR.class 4KB
NBSysMenu$NBSysMenuBuilder.class 4KB
MailServiceImpl.class 4KB
NBFile.class 4KB
NBCate.class 4KB
AdminInterceptor.class 4KB
NBSysUser$NBSysUserBuilder.class 4KB
LatestComment.class 4KB
SessionInterceptor.class 4KB
AdminIndexController.class 4KB
LayUploader.class 4KB
NBKeyword.class 3KB
NBSession$NBSessionBuilder.class 3KB
NBComment$NBCommentBuilder.class 3KB
TagServiceImpl.class 3KB
NBSysRole.class 3KB
NBMessage$NBMessageBuilder.class 3KB
NBCloudFileCate.class 3KB
NBProjectCate.class 3KB
GlobalController.class 3KB
ParamServiceImpl.class 3KB
NkUploader.class 3KB
共 2000 条
- 1
- 2
- 3
- 4
- 5
- 6
- 20
资源评论
等天晴i
- 粉丝: 5982
- 资源: 10万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 各类合同-06-实习协议书.docx
- 各类合同-09-退休返聘协议书.docx
- 劳动合同的解除.docx
- 保守商业秘密协议.doc
- 保密协议样本.doc
- 反竞争协议样本.doc
- 岗位劳动合同.doc
- 变更劳动合同协议书.doc
- 变更、终止、解除劳动合同通知书.doc
- 雇佣合同书范本.doc
- 解除劳动合同通知书.doc
- Self-supervised Learning of Adversarial Example:Towards Good Generalizations for Deepfake Detection
- 劳动合同变更协议书.doc
- 劳动合同管理规定.docx
- 介绍信格式范本.doc
- 借用人员劳务协议.doc
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功