springboot 自定义注解 登录拦截验证

springboot 自定义注解 登录拦截验证

自定义注解

import java.lang.annotation.*;

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited                              
public @interface VerifyLogin {
    boolean required() default true;
}

拦截器

import com.lideru.customannotation.web.configuration.VerifyLogin;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //1.判断是否存在注解
        if(!(handler instanceof HandlerMethod)){
            System.out.println("不是HandlerMethod类型,不用检查");
            return true;
        }
        HandlerMethod method = (HandlerMethod)handler;
        boolean hasLoginAnnotation=method.getMethod().isAnnotationPresent(VerifyLogin.class);
        if(!hasLoginAnnotation){
            //不存在LoginRequired注解,则直接通过
            System.out.println("不存在LoginRequired注解,则直接通过");
            return true;
        }
        VerifyLogin loginRequired=method.getMethod().getAnnotation(VerifyLogin.class);
        //2.required=false,则无需检查是否登录
        if(!loginRequired.required()){
            System.out.println("required=false,不用检查是否登录");
            return true;
        }
        //3.登录状态检查,使用response返回指定信息
        System.out.println("required=true,需检查是否登录");
        response.getWriter().append("你需要就行登录才可以访问!");
        return false;
    }
}

配置类

import com.lideru.customannotation.web.interceptor.LoginInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class LoginConfiguration implements WebMvcConfigurer {
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new LoginInterceptor());
    }
}

测试控制器


import com.lideru.customannotation.web.configuration.VerifyLogin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class LoginController {

    /**
     * 不给LoginRequired注解,不需要验证是否登录
     * @return
     */
    @RequestMapping("index0")
    public String index0(){
        return "index0";
    }

    /**
     * 给LoginRequired注解(默认是true),需要验证是否登录
     * @return
     */
    @VerifyLogin()
    @RequestMapping("index1")
    public String index1(){
        return "index1";
    }
    /**
     * 给LoginRequired注解(required = false),不需要验证是否登录
     * @return
     */
    @VerifyLogin( required = false)
    @RequestMapping("index2")
    public String index2(){
        return "index2";
    }

    /**
     * 给LoginRequired注解(required = true),需要验证是否登录
     * @return
     */
    @VerifyLogin(required = true)
    @RequestMapping("index3")
    public String index3(){
        return "index3";
    }
}

调用
http://127.0.0.1:8080/index0
http://127.0.0.1:8080/index1
http://127.0.0.1:8080/index2
http://127.0.0.1:8080/index3
结果如下:

不存在LoginRequired注解,则直接通过
required=true,需检查是否登录
required=false,不用检查是否登录
required=true,需检查是否登录

证明成功了!

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SpringBoot可以通过实现拦截器或者过滤器来实现登录拦截功能。你可以使用SpringBoot和Thymeleaf来实现这个功能。具体的实现步骤如下: 1. 创建一个拦截器类,在该类中实现HandlerInterceptor接口,并重写preHandle方法。在preHandle方法中,可以编写登录验证的逻辑。如果验证失败,则可以进行跳转到登录页面的操作。 2. 在拦截器类上加上@Component注解,将其作为一个Bean进行注册。 3. 在配置类中,继承WebMvcConfigurerAdapter类,并重写addInterceptors方法,在该方法中添加自定义的拦截器。 4. 在配置类上加上@Configuration注解,将其作为一个配置类。 5. 在登录页面的Controller中,编写登录验证的逻辑。如果验证通过,则可以进行跳转到主页面。 通过以上步骤,你可以实现使用拦截器或者过滤器对登录信息进行验证,并跳转到登录页的功能。参考的资料包括。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Java SpringBoot实现的过滤器(和拦截器)控制登录页面跳转](https://download.csdn.net/download/myycsdn/10346742)[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: 33.333333333333336%"] - *2* [SpringBoot实现拦截器、过滤器、监听器过程解析](https://download.csdn.net/download/weixin_38697063/12744532)[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: 33.333333333333336%"] - *3* [详谈springboot过滤器和拦截器的实现及区别](https://download.csdn.net/download/weixin_38539053/12756667)[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: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值