仿牛客网社区项目(十)检查登录状态
检查登录状态
- 使用拦截器
- 在方法前标注自定义注解
- 拦截所有请求,只处理带有该注解的方法
- 自定义注解
- 常用的元注解:
@Target、@Retention、@Document、@Inherited
- 常用的元注解:
- 如何读取注解:
Method.getDeclaredAnnotations ()
Method.getAnnotation (ClassannotationClass)
需要解决的问题:防止直接在浏览器输入网址进入“登录信息设置”的网页。
解决方案:设置拦截器
,拦截所有请求,且在指定的方法上进行拦截。那么可以自定义注解,让拦截器只拦截带有这个自定义注解的方法。
自定义注解:
- 如何自己定义:常用的元注解有:@Target、@Retention、@Document、@Inherited(前两个必须要写)
- 如何读取注解:
Method.getDeclaredAnnotations()
Method.getAnnotation(Class annotationClass)
一、自定义注解,是否登录:#
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface LoginRequired {//打了这个标记,登录时才能访问
}
使用注解:#
算是一个权限控制,这两个请求必须登录
@LoginRequired
@RequestMapping(path = "/setting", method = RequestMethod.GET)
public String getSettingPage() {
return "/site/setting";
}
@LoginRequired
@RequestMapping(path = "/upload", method = RequestMethod.POST)
public String uploadHeader(MultipartFile headerImage, Model model) {
if (headerImage == null) {
model.addAttribute("error", "您还没有选择图片!");
return "/site/setting";
}
二、拦截器#
看拦截目标的类型是不是方法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");
}
1234567
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· DeepSeek在M芯片Mac上本地化部署