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

静态摄影网站模板微信裂变营销软件

静态摄影网站模板,微信裂变营销软件,wordpress 图片位置,常州网站建设哪儿好薇使用拦截器 在方法前标注自定义注解拦截所有请求&#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.hkea.cn/news/622043/

相关文章:

  • 高端网站建设 引擎技企业网页
  • 模仿别人网站百度外链查询工具
  • 教程建设网站广告免费发布信息平台
  • wordpress php5.4支持宁波seo排名优化
  • 宁波制作网站哪个好百度怎么发自己的小广告
  • 新浪网站用什么语言做的百度软件下载
  • wordpress如何做网站重庆seo俱乐部联系方式
  • 教育局两学一做网站深圳全网推广平台
  • 淘宝做详情页代码网站免费大数据查询平台
  • 苹果做安卓游戏下载网站好新媒体营销案例ppt
  • 网络营销实务关键词优化seo优化排名
  • 网站推广优化教程游戏代理加盟平台
  • 网站提升权重全国疫情高峰感染进度
  • 营销型网站怎么做智能建站abc
  • 捷信做单官方网站网络服务主要包括什么
  • 网站建设的方案费用什么时候网络推广
  • 这么做3d展示网站公司百度官网优化
  • 工业设计软件上市公司搜索引擎优化的方法
  • 网站建设公司创意网站网络推广推广
  • 浙江三建建设集团有限公司网站关键词的作用
  • 网站建设官方网站教育培训机构加盟十大排名
  • 万网上传网站seo免费
  • 孝感做网站公司百度热议排名软件
  • 建设网站费用吗廊坊seo快速排名
  • 网站建设公司怎样拓展网站业务大连网站推广
  • 什么网站可以免费做找客户东莞seo建站推广费用
  • 合肥微信网站建设购物网站如何推广
  • 网站建设课程简介图片百度官网认证免费
  • 月夜影视在线观看免费完整版韩剧关键词排名优化报价
  • 做网站的工作时间兰州seo公司