spingboot handlerinterceptor实现用户认证和页面拦截跳转之二 spring配置

本文介绍如何使用Spring MVC配置权限拦截器,通过@autowired自动装配AuthorizationInterceptor,并设置特定目录下请求需经过权限检查,同时排除登录注册等路径。

1、

@autowired自动装配:

对类成员变量、方法、构造函数进行标注,完成自动装配。

 消除set、get方法,在IoC容器中查找对应类型的bean装配

 2、

使用装配好的authorizationIntercetor
addPathPatterns("/pro/**")是/pro目录下的任何链接都要进入authorizationIntercetor执行。
excludePathPatterns(excludeUrl)是除了这个列表中的路径无需进入权限拦截器执行

package com.pro.interceptorsconfig;

import java.util.LinkedList;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import com.pro.sign.interceptors.AuthorizationInterceptor;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
/*autowired自动装配;对类成员变量、方法、构造函数进行标注,完成自动装配。
 * 消除set、get方法,在IoC容器中查找对应类型的bean装配
 * 权限拦截sign-rest中的interceptor中*/
  @Autowired
  private AuthorizationInterceptor authorizationInterceptor;
/*使用模板字符串替换进行赋值
 * resource/application.yml 中获取响应的数值,实现最少的代码改动量
 * */

  @Value("${proconfig.is-open-authorization-interceptor}")
  private boolean isOpenAuthorizationInterceptor;

  @Override
  public void addInterceptors(InterceptorRegistry registry) {
    if (isOpenAuthorizationInterceptor) {
    	/*设置一个列表,用来存放不需要拦截的路径*/
      LinkedList<String> excludeUrl = new LinkedList<>();
      excludeUrl.add("/pro/login");
      excludeUrl.add("/pro/register");
      excludeUrl.add("/initdata");
      /*使用装配好的authorizationIntercetor
       * addPathPatterns("/pro/**")是/pro目录下的任何链接都要进入authorizationIntercetor执行。
       * excludePathPatterns(excludeUrl)是除了这个列表中的路径无需进入权限拦截器执行*/
      registry.addInterceptor(authorizationInterceptor).addPathPatterns("/pro/**").excludePathPatterns(excludeUrl);
    }
  }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值