当前位置: 首页 > news >正文

石家庄网站排名优化wordpress修改布局

石家庄网站排名优化,wordpress修改布局,企业的网站用vue做的,聊天app开发源码Ioc和Aop是spring的两大重要思想#xff0c;前者指的是控制反转#xff08;Invers of control#xff09;后者指的是面向切面编程#xff08;Aspect oriented programing#xff09;。aop的一大作用就是能将很多重复的功能点抽取出来#xff0c;而用注解或者配置的方式统…         Ioc和Aop是spring的两大重要思想前者指的是控制反转Invers of control后者指的是面向切面编程Aspect oriented programing。aop的一大作用就是能将很多重复的功能点抽取出来而用注解或者配置的方式统一给需要此功能的方法进行一个增强          因此aop的一大用途就是用来简化重复而一致的一些功能比如日志打印、登录校验、性能监控、事务管理等。这篇文章讲的是aop在登录校验方面的一种实现 实现步骤 1.编写拦截器实现HandlerIntercepter接口注意HandlerInterceptor是springMVC提供的接口需要引入spring mvc依赖 2.在实现类中重写方法preHandle、postHandle、afterCompletion在方法中实现切面逻辑 3.编写配置类确定使用哪些拦截器拦截器作用于哪些地方多个拦截器之间执行的先后顺序 实现拦截器  编写拦截器重写HandlerInterceptor中的默认方法 package com.dianping.utils;import cn.hutool.core.bean.BeanUtil; import cn.hutool.core.util.StrUtil; import com.dianping.dto.UserDTO; import lombok.extern.slf4j.Slf4j; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.web.servlet.HandlerInterceptor;import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.util.Map; import java.util.concurrent.TimeUnit;import static com.dianping.utils.RedisConstants.LOGIN_USER_KEY;Slf4j public class RefreshTokenInterceptor implements HandlerInterceptor {private StringRedisTemplate stringRedisTemplate;public RefreshTokenInterceptor(StringRedisTemplate stringRedisTemplate) {this.stringRedisTemplate stringRedisTemplate;}Overridepublic boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {// TODO 1.获取请求头中的 tokenString token request.getHeader(authorization);if (StrUtil.isBlank(token)) {// 没有 token 无法刷新直接结束return true;}// TODO 2.基于 token 获取 redis 中的用户String tokenKey LOGIN_USER_KEY token;MapObject, Object userMap stringRedisTemplate.opsForHash().entries(tokenKey);// 3.判断用户是否存在if (userMap.isEmpty()) {// redis中没有用户信息无法刷新直接结束return true;}// TODO 5.将查询到的 Hash 数据转为 UserDTO对象UserDTO userDTO BeanUtil.fillBeanWithMap(userMap, new UserDTO(), false);// TODO 6.存在保存用户信息到 ThreadLocalUserHolder.saveUser((UserDTO) userDTO); // log.info(刷新有效期);// TODO 7.刷新 token 有效期stringRedisTemplate.expire(tokenKey,30, TimeUnit.MINUTES);// 8.放行return true;} }package com.dianping.utils;import lombok.extern.slf4j.Slf4j; import org.springframework.web.servlet.HandlerInterceptor;import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;Slf4j public class LoginInterceptor implements HandlerInterceptor {/*** Intercept the execution of a handler. Called after HandlerMapping determined* an appropriate handler object, but before HandlerAdapter invokes the handler.* pDispatcherServlet processes a handler in an execution chain, consisting* of any number of interceptors, with the handler itself at the end.* With this method, each interceptor can decide to abort the execution chain,* typically sending an HTTP error or writing a custom response.* pstrongNote:/strong special considerations apply for asynchronous* request processing. For more details see* {link org.springframework.web.servlet.AsyncHandlerInterceptor}.* pThe default implementation returns {code true}.* param request current HTTP request* param response current HTTP response* param handler chosen handler to execute, for type and/or instance evaluation* return {code true} if the execution chain should proceed with the* next interceptor or the handler itself. Else, DispatcherServlet assumes* that this interceptor has already dealt with the response itself.* throws Exception in case of errors* preHandle方法该方法在请求处理之前被调用可以在该方法中进行一些预处理操作*/ Overridepublic boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {// 1.判断是否需要拦截ThreadLocal中是否有用户if (UserHolder.getUser() null) {// 没有需要拦截设置状态码log.info(用户未登录);response.setStatus(401);// 拦截return false;}// 有用户则放行return true;// // 1.获取session // HttpSession session request.getSession(); // // 2.获取session中的用户 // Object user session.getAttribute(user); // // 3.判断用户是否存在 // if (user null) { // // 4.不存在拦截 // response.setStatus(401); // return false; // } // // 5.存在保存到ThreadLocal // UserHolder.saveUser((UserDTO) user); // // 6.放行 // return true;}/*** Intercept the execution of a handler. Called after HandlerAdapter actually* invoked the handler, but before the DispatcherServlet renders the view.* Can expose additional model objects to the view via the given ModelAndView.* pDispatcherServlet processes a handler in an execution chain, consisting* of any number of interceptors, with the handler itself at the end.* With this method, each interceptor can post-process an execution,* getting applied in inverse order of the execution chain.* pstrongNote:/strong special considerations apply for asynchronous* request processing. For more details see* {link org.springframework.web.servlet.AsyncHandlerInterceptor}.* pThe default implementation is empty.* param request current HTTP request* param response current HTTP response* param handler the handler (or {link HandlerMethod}) that started asynchronous* execution, for type and/or instance examination* param modelAndView the {code ModelAndView} that the handler returned* (can also be {code null})* throws Exception in case of errors* postHandle方法该方法在请求处理之后、视图渲染之前被调用可以在该方法中对请求处理结果进行一些处理例如修改ModelAndView中的数据、记录日志等。*/Overridepublic void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,Nullable ModelAndView modelAndView) throws Exception {}/*** Callback after completion of request processing, that is, after rendering* the view. Will be called on any outcome of handler execution, thus allows* for proper resource cleanup.* pNote: Will only be called if this interceptors {code preHandle}* method has successfully completed and returned {code true}!* pAs with the {code postHandle} method, the method will be invoked on each* interceptor in the chain in reverse order, so the first interceptor will be* the last to be invoked.* pstrongNote:/strong special considerations apply for asynchronous* request processing. For more details see* {link org.springframework.web.servlet.AsyncHandlerInterceptor}.* pThe default implementation is empty.* param request current HTTP request* param response current HTTP response* param handler the handler (or {link HandlerMethod}) that started asynchronous* execution, for type and/or instance examination* param ex any exception thrown on handler execution, if any; this does not* include exceptions that have been handled through an exception resolver* throws Exception in case of errors*afterCompletion方法该方法在请求处理完成后、视图渲染完成后被调用可以在该方法中进行一些资源清理操作例如释放数据库连接、删除临时文件等。此时无法修改响应结果的内容和格式因为响应已经被发送到客户端了。*/Overridepublic void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {} }编写配置类,实现WebMvnConfigurer重写addInterceptor接口 package com.dianping.config;import com.dianping.utils.LoginInterceptor; import com.dianping.utils.RefreshTokenInterceptor; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;Configuration public class MvcConfig implements WebMvcConfigurer {AutowiredStringRedisTemplate stringRedisTemplate;Overridepublic void addInterceptors(InterceptorRegistry registry) {registry.addInterceptor(new LoginInterceptor()).excludePathPatterns(/user/code,/user/login).order(1);// order 指定不同拦截器的执行顺序order数字越小的越先执行。// 如果没有用 order 指定执行顺序按照注册的先后顺序执行先注册先执行registry.addInterceptor(new RefreshTokenInterceptor(stringRedisTemplate)).addPathPatterns(/**).order(0);} }
http://www.pierceye.com/news/236022/

相关文章:

  • 景安服务器管理助手如何备份网站国外做图标网站
  • 网站轮播怎么做石家庄网站建设规划
  • 免费软件网站下载深圳网站开发哪个公司好
  • 建设项目验收网站公示内网门户网站建设方案
  • 滨海做网站哪家最好宝安附近做网站公司
  • 详情页的五大模块东莞网站优化科技有限公司
  • 南阳建设网站哪家好昆明网站服务
  • 大潮建设集团有限公司 网站网站改版策划方案
  • 网站开发心路历程烟台网站建设薇企汇互联见效付款
  • 企业网站的制作周期wordpress添加数据库表
  • 广告推广营销网站网站买空间的价格
  • 转转假网站怎么做linux建设视频网站
  • 伍佰亿搜索引擎网站系统wordpress 增加备案
  • 韩国做游戏的电影 迅雷下载网站有哪些网络营销方式文献
  • 大学生兼职网站的融资方案龙华网站建设设计制作公司
  • 青之峰网站建设哪家好用什么l软件做网站了
  • 免费建站资源怎么编写app软件
  • 机关网站建设建议云南响应式网站建设
  • 对网站开发语言的统计杭州网站设计公司有哪些
  • 不会代码 怎么做网站兴义网络推广
  • 综合电子商务型企业网站怎么做网站的网盘
  • ucenter使用自己做的网站房地产新闻时事热点
  • 企业网站备案 过户电商运营视频教程
  • 做网站运营这工作怎么样北京网站优化价格
  • 河南专业网站建设网站怎么做高权重
  • 国内大型电子网站建设做网站时怎么透明化
  • 微应用和微网站的区别手机网站开发的目的
  • 网站ico开一个网站建设公司好
  • wordpress中文站cn外贸网站怎么换域名
  • 淘宝客怎么做直播网站吗学校网站建设发展概况分析