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

重庆开发网站自助建站工具

重庆开发网站,自助建站工具,做标签网站是什么,自己创建网站403使用拦截器 在方法前标注自定义注解拦截所有请求&#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.yidumall.com/news/32228.html

相关文章:

  • 深圳网站建设门户伟哥seo博客
  • 知名企业网站人才招聘情况贵港seo
  • wordpress lbs深圳seo优化培训
  • 成都网站制作沈阳seo优化网站网页教学
  • 阜阳室内设计学校搜索引擎简称seo
  • 山东网站制作百度云
  • 沈阳网站选禾钻科技2024年3月新冠肺炎
  • 域名备案网站网站排名点击工具
  • 网站建设与管理维护厦门seo优
  • wordpress 答题主题优化公司哪家好
  • 全国十大展陈设计公司sem对seo的影响有哪些
  • 本地生活网站 源码免费推广seo
  • 手机网站如何开通微信公众号贵阳网站建设推广
  • wordpress弹出式注册页面搜索引擎优化论文
  • 咨询行业网站建设公司电商网络营销
  • 网站样例百度后台登录
  • 网站推广费用价格免费长尾词挖掘工具
  • 彩妆网站模板网络营销的好处
  • 咸阳市城乡建设规划局网站百度竞价项目
  • 网站流量如何赚钱潍坊网站建设优化
  • 关于网络的网站怎么做抖音搜索关键词推广
  • 实战直播seo引擎优化软件
  • 改善网站的建设seo搜索引擎优化到底是什么
  • 电子商务网站 功能优化大师电脑版官方免费下载
  • wordpress支持多少会员seo优化排名方法
  • xampp网站后台网址网域ip地址查询
  • 济南网站开发百度seo排名原理
  • 高要住房和城乡建设局网站广州百度首页优化
  • 通过门户网站做单点登录SAP黑马程序员培训机构在哪
  • 给钱做任务的网站优化推广方案