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

微网站栏目图标特价网站建设价格

微网站栏目图标,特价网站建设价格,短期培训班学什么好,一个网页的制作流程使用拦截器 在方法前标注自定义注解拦截所有请求&#xff0c;只处理带有该注解的方法 自定义注解&#xff1a; 常用元注解&#xff1a;Target, Rentention, Document, Inherited如何读取注解&#xff1a; - Method.getDeclaredAnnotations() - Method.getAnnotaion(Class<T&…
  • 使用拦截器
    • 在方法前标注自定义注解
    • 拦截所有请求,只处理带有该注解的方法
  • 自定义注解:
    • 常用元注解:@Target, @Rentention, @Document, @Inherited
    • 如何读取注解:
      - Method.getDeclaredAnnotations()
      - Method.getAnnotaion(Class<T> annotationClass)

业务场景:未登陆状态下,用户不能访问需要登陆才能访问的页面,例如修改个人信息页面等。

1. 自定义注解

package com.nowcoder.community.annotation;import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface LoginRequired {}

2. 在方法前加上该注解

@LoginRequired
@RequestMapping(path = "/setting",method = RequestMethod.GET)
public String getSettingPage(){return "/site/setting";
}

3. 定义拦截器

package com.nowcoder.community.controller.Interceptor;import com.nowcoder.community.annotation.LoginRequired;
import com.nowcoder.community.entity.User;
import com.nowcoder.community.util.HostHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;@Component
public class LoginRequireInterception implements HandlerInterceptor {@Autowiredprivate HostHolder hostHolder;@Overridepublic boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {if(handler instanceof HandlerMethod) {  // 拦截到类型为方法HandlerMethod handlerMethod = (HandlerMethod) handler;Method method = handlerMethod.getMethod();  // 获取方法LoginRequired loginRequired = method.getAnnotation(LoginRequired.class);  // 获取方法的注解if (loginRequired != null && hostHolder.getUser() == null) {  // 方法是loginRequired且user没登陆,需要拦截response.sendRedirect(request.getContextPath() + "/login");return false;}}return true;}
}

4. 配置拦截器

package com.nowcoder.community.config;import com.nowcoder.community.controller.Interceptor.AlphaInterceptor;
import com.nowcoder.community.controller.Interceptor.LoginRequireInterception;
import com.nowcoder.community.controller.Interceptor.LoginTicketInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;@Configurationpublic class WebMvcConfig implements WebMvcConfigurer {@Autowiredprivate AlphaInterceptor alphaInterceptor;@Autowiredprivate LoginTicketInterceptor loginTicketInterceptor;@Autowiredprivate LoginRequireInterception loginRequireInterception;@Overridepublic void addInterceptors(InterceptorRegistry registry) {// 通过重写addInterceptors()方法,可以配置拦截器,对请求进行预处理或后处理。registry.addInterceptor(loginRequireInterception).excludePathPatterns("/**/*.css", "/**/*.js", "/**/*.png", "/**/*.jpg", "/**/*.jpeg");}}
http://www.yayakq.cn/news/704016/

相关文章:

  • 公司注册网上核名一直提交不成功兰州网络推广优化网
  • 青海专业网页设计免费建站网站视频建设
  • 网站建设不能在淘宝发布小程序制作平台排行榜前十名
  • 深圳网站建设哪些注册公司代理记账公司
  • 网站的优化排名怎么做关键字优化
  • 建设网站的时候如何查看网站架构
  • 企业网站建设能解决什么问题茂名网站设计
  • 网站建设服务器域名南阳做网站费用
  • 网站文章更新怎么做网站后台分模块
  • h5网站开发软件下载定制vx免费
  • 看乱码的网站十大办公室设计公司
  • 怎么做网站旅游宣传河南省建设协会网站
  • 电子工程网站大全东台建设企业网站
  • 自己建的网站能用吗手机网址2021年免费不封
  • 在天极网做网站有效果吗网站建设先进技术
  • 自媒体123网站珠海移动网站建设公司
  • 做论坛网站需要多大空间网站建设用什么服务器
  • 网站开发平台建设电子商务网站建设目标分析
  • 网站建设平台招商wd mycloud wordpress
  • 网站建设视频l福建漳州东山建设局官方网站
  • 网站开发与rest长春网站建设解决方案
  • 济南网站建设网站建网站需要注意的问题
  • 视频网站开发是什么搜索引擎优化代理
  • 获取网站访客qq信息wordpress自动发帖
  • 天津手动网站建设调试给中小企业提供网站建设服务
  • 烟台产品网站建设青海省建设厅网站备案资料
  • 招聘网站哪个平台比较好互联网公司排名 中国
  • 网站的建设部署与发布做会计网站的流程
  • 大学专业网站大兴网站建设公司
  • 4s店网站模板ui设计网上培训课程