ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

springsecurity HttpSecurity类中的一些方法

2019-02-01 14:55:35  阅读:541  来源: 互联网

标签:requestMatcher return matches request springsecurity requestMatchers 类中 HttpSecu


1.HttpSecurity中的requestMatcher,antMatcher ,mvcMatcher,regexMatcher,requestMatchers方法

首先FilterChainProxy中

private List<Filter> getFilters(HttpServletRequest request) {
        for (SecurityFilterChain chain : filterChains) {
            if (chain.matches(request)) {
                return chain.getFilters();
            }
        }

        return null;
    }

DefaultSecurityFilterChain中的matches方法

public boolean matches(HttpServletRequest request) {
        return requestMatcher.matches(request);
    }

也就是说在filterchains中依靠每个filterchain的requestMatcher来匹配request,之前提到的那些Matcher就是用来设置requestMatcher的

public HttpSecurity requestMatcher(RequestMatcher requestMatcher) {
        this.requestMatcher = requestMatcher;
        return this;
    }

这个是用户可以自己实现一个RequestMatcher,然后调用这个requestMatcher方法设置。

antMatcher ,mvcMatcher,regexMatcher,参数都是字符串,然后利用字符串根据相应规则进行匹配,可查看相关代码


这里说说requestMatchers,他用了实现匹配多个路径,因为其他那些也就能设置一个路径。


public RequestMatcherConfigurer requestMatchers() {
        return requestMatcherConfigurer;
    }

它是返回一个requestMatcherConfigurer,然后我们可以通过这个对象中的方法来设置匹配路径 比如RequestMatcherConfigurer中的mvcMatchers AbstractRequestMatcherRegistry的一些antMatchers regexMatchers 这些方法最后都会调用到HttpSecurity中的

private void setMatchers(List<? extends RequestMatcher> requestMatchers) {
            this.matchers.addAll(requestMatchers);
            requestMatcher(new OrRequestMatcher(this.matchers));
        }

OrRequestMatcher比较简单,就是逐个遍历里面的路径设置,看看有没有匹配的。


如果我们不去调用这些方法,那requestMatcher是一个默认的AnyRequestMatcher,那是匹配所有路径,那如果在filterchains中的后面的filterchain就无法被调用了,因为这个filterchain截获了所有request。


authorizeRequests

标签:requestMatcher,return,matches,request,springsecurity,requestMatchers,类中,HttpSecu
来源: http://blog.51cto.com/2839840/2348426

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有