Java拦截器---简单的请求认证

该博客介绍了如何在SpringBoot项目中定义并使用一个拦截器进行请求头的鉴权认证。拦截器`AuthorizationInterceptor`读取配置文件中的签名和应用编码,对比请求头中的信息,如果匹配则放行,否则返回403错误。配置类`MyWebConfig`注册了拦截器,并指定了拦截和排除的请求路径。
摘要由CSDN通过智能技术生成

spring boot项目

1.定义拦截器(对请求头信息进行鉴权认证)

import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

@Component
@Slf4j
@PropertySource(value = {"classpath:config/authorSetting.properties"})
public class AuthorizationInterceptor implements HandlerInterceptor {

    private @Value("#{'${author.sign}'.split(',')}")
    List<String> signs;

    private @Value("#{'${author.appcode}'.split(',')}")
    List<String> appcodes;


    public final boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws ServletException, IOException {
//        log.info("sign====" + sign.toString());
//        log.info("appcode====" + appcode.toString());
        String reqSign = request.getHeader("sign");
        String resAppCode = request.getHeader("appCode");

        for (int i = 0; i < appcodes.size(); i++) {
            if (signs.get(i).equals(reqSign) && appcodes.get(i).equals(resAppCode)) {
//            log.info("鉴权成功!!");
                return true;
            }
        }

        handleNotAuthorized(request, response, handler);
        return false;
    }

    protected void handleNotAuthorized(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws ServletException, IOException {
        // 403表示资源不可用。服务器理解用户的请求,但是拒绝处理它,通常是由于权限的问题
        response.sendError(403);
    }

}

2.配置类中注册定义的拦截器

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

@Configuration
public class MyWebConfig extends WebMvcConfigurerAdapter {

    @Autowired
    private AuthorizationInterceptor authorizationInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(authorizationInterceptor)
                .addPathPatterns("/grid/query")       //拦截项目中的哪些请求
                .addPathPatterns("/beehive/noticeMessage")       //拦截项目中的哪些请求
                .addPathPatterns("/beehive/pushQueryResult")       //拦截项目中的哪些请求
                .excludePathPatterns("");  //对项目中的哪些请求不拦截
    }
}

认证信息存到这里
认证信息存放位置

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值