2.8仿牛客社区项目——检查登录状态(自定义注解、拦截器)

需要解决的问题:防止直接在浏览器输入网址进入“登录信息设置”的网页。
解决方案:设置拦截器,拦截所有请求,且在指定的方法上进行拦截。那么可以自定义注解,让拦截器只拦截带有这个自定义注解的方法。

自定义注解:

  • 如何自己定义:常用的元注解有:@Target、@Retention、@Document、@Inherited(前两个必须要写)
  • 如何读取注解:
    Method.getDeclaredAnnotations​()
    Method.getAnnotation​(Class annotationClass)
    在这里插入图片描述

自定义注解,是否登录:

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface LoginRequired {//打了这个标记,登录时才能访问
    
}

使用注解:

在这里插入图片描述

拦截器

看拦截目标的类型是不是方法HandlerMethod,如果是转型成handlerMethod,获取method对象,获取注解,注解不为空loginRequired != null即带有这个注解(LoginRequired.class),即需要登录,若此时hostHolder.getUser()==null没登录,就要重定向到登录页面(即项目路径+/login),返回false,不能继续执行该方法。

@Component
public class LoginRequiredInterceptor implements HandlerInterceptor {

    @Autowired
    private HostHolder hostHolder;

    @Override
    public 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) {
                response.sendRedirect(request.getContextPath() + "/login");//return ”redirect“底层其实也是这个,可以通过配置文件注入路径参数,也可以从请求中直接取到路径
                return false;
            }
        }
        return true;
    }
}

拦截器配置和之前一样,要注入拦截器、排除静态路径。

WebMvcConfig

    @Autowired
    private LoginRequiredInterceptor loginRequiredInterceptor;
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(loginRequiredInterceptor)
                .excludePathPatterns("/**/*.css", "/**/*.js", "/**/*.png", "/**/*.jpg", "/**/*.jpeg");
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值