7_检测登录状态以及自定义注解的使用

对于前面已经实现的功能中,对于项目存在一定的安全隐患

比如在没有登录的情况下可以直接通过 localhost:8081/community/user/setting 访问只有登录之后才允许访问的页面。

image-20221007020008369

下面将通过自定义注解和拦截器搭配的方式实现没登录时不能访问一些请求的功能。

image-20221007014733541

@Target用于描述该注解可以作用的目标类型
@Retention用于描述该注解被保留的时间
@Document用于描述该注解是否可以生成到文档里	
如果某个类使用@Inherited修饰,则该类的子类将自动使用@Inherited修饰。
  1. 自定义注解
package com.wjiangquan.community.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * @author weijiangquan
 * @date 2022/10/7 -2:04
 * @Description
 */
@Target(ElementType.METHOD)    //在方法中起作用
@Retention(RetentionPolicy.RUNTIME)  //运行时起作用
public @interface LoginRequired {
    
}

  1. 书写拦截器,确定需要拦截的请求
package com.wjiangquan.community.controller.interceptor;

import com.wjiangquan.community.annotation.LoginRequired;
import com.wjiangquan.community.util.HostHolder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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;

/**
 * @author weijiangquan
 * @date 2022/10/7 -2:07
 * @Description 用户避免用户没有访问只有登录之后才可以访问的请求 比如 /user/setting
 */
@Component
public class LoginRequiredInterceptor implements HandlerInterceptor {

    private static final Logger logger = LoggerFactory.getLogger(LoginRequiredInterceptor.class);
    @Autowired
    private HostHolder hostHolder;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        logger.debug("handler"+handler);
        if (handler instanceof HandlerMethod) {
            HandlerMethod handlerMethod = (HandlerMethod) handler;
            Method method = handlerMethod.getMethod();
            LoginRequired annotation = method.getAnnotation(LoginRequired.class);
            if (annotation != null && hostHolder.getUser() == null) {
                response.sendRedirect(request.getContextPath() + "/login");
                return false;
            }
        }
        return true;
    }
}

  1. 在需要拦截的请求上加上注解,通过注解的方式获取需要拦截的请求

当页面中通过路径直接访问时,直接重定向到登录界面

在本项目中目前需要拦截的请求为 下面中的setting 和 上传图片,后面可以根据实际的需要进行添加

image-20221007023037186

在本项目中目前需要拦截的请求为 下面中的setting 和 上传图片,后面可以根据实际的需要进行添加

image-20221007023114706

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

莪假裝堅強

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值