牛客网项目8:登录检查

1. 大体思路

防止用户直接输入:http://localhost:8080/community/user/setting,从而进入某些页面。因此进入某些页面时,需要检查用户的登录状态。

由于众多页面都需要进行登录状态的判定,因此写在拦截器中。

不过一个一个添加要拦截与放行的路径十分麻烦,我们可以自定义注解。让被注解标识的方法,被拦截。

2. 自定义注解

         - 常用的元注解:

                @Target、@Retention、@Document、@Inherited

         - 读取注解:

                Method.getDeclaredAnnotations()

                Method.getAnnotation(Class<T> annotationClass)

在com.nowcoder.mycommunity目录下新建包:annotation,里面用于存放自定义注解。

在该目录下新建Annotation:LoginRequired

package com.nowcoder.mycommunity.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 {
    //只起到标识的作用
    //被该注解标识的方法,需要进行登录验证
}

3. 控制器方法上添加注解

UserService中有三个方法:

       进入账号设置: getSettingPage()

       上传头像:getSettingPage()

        获取头像:getHeader()

其中前两个方法需要添加自定义的注解LoginRequired,来接受拦截,进行登录检查。

获取头像的方法则不需要,因为用户在不登录时,依然能查看他人的头像。

4. 拦截器

在controller.interceptor下新建类:LoginRequiredInterceptor

该拦截器拦截了所有带LoginRequired注解的方法。

package com.nowcoder.mycommunity.controller.interceptor;

import com.nowcoder.mycommunity.annotation.LoginRequired;
import com.nowcoder.mycommunity.util.HostHolder;
import org.springframework.beans.Mergeable;
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 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 false;
            }
        }

        return true;
    }
}

5. 配置拦截器

在WebMvcConfig类下,配置登陆检查拦截器LoginRequiredInterceptor跳过静态资源。

这样使得在访问静态资源以为的路径时,都会通过该拦截器。该拦截器判断方法是否携带注解LoginRequired,如果带、且为未登录状态,就进行拦截。

    @Autowired
    private LoginRequiredInterceptor loginRequiredInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(loginTicketInterceptor)
                .excludePathPatterns("/**/*.css","/**/*.js","/**/*.png","/**/*.jpg","/**/*.jpeg");
        //  /**:static目录下的所有目录
        //  /**/.css:static目录下所有的.css文件都排除掉

        //登陆检查拦截器,跳过所有的静态资源
        registry.addInterceptor(loginRequiredInterceptor)
                .excludePathPatterns("/**/*.css","/**/*.js","/**/*.png","/**/*.jpg","/**/*.jpeg");

    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
牛客网的课程《Linux高并发服务器开发》中包含了项目笔记,其中涉及到了WebServer的开发。根据引用的信息,这门课程的学习进度可能由于个人原因而拖延了一段时间。在第五章的项目学习中,可能包含了关于多进程服务器开发的学习内容。不过具体的学习笔记可能还没有迁移到pad上,暂时无法获取详细的内容[2]。根据引用,在服务器开发中,使用单Reactor和多线程的方法可以提高效率,并充分利用多核CPU的性能优势。然而,使用多线程开发可能需要在各自的线程上加锁保护临界区数据,相对较为复杂。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [牛客网c++web服务器项目学习笔记-第0章 课程介绍](https://blog.csdn.net/weixin_45139984/article/details/132205586)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [【牛客网C++服务器项目学习】Day12-网络编程的两种事件处理模式](https://blog.csdn.net/qq_42518941/article/details/122283291)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值