SpringSecurity过滤指定url【.antMatchers(***).permitAll()】失效问题

这篇具有很好参考价值的文章主要介绍了SpringSecurity过滤指定url【.antMatchers(***).permitAll()】失效问题。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

SpringSecurity过滤指定url【.antMatchers(***).permitAll()】失效问题

问题描述

在使用SpringSecurity作为后端验证框架时,遇到配置一些接口不需要token验证,直接放行,但是配置之后没有生效,一直究其原因。文章来源地址https://www.toymoban.com/news/detail-487996.html

项目配置

  1. 因为要进行登录认证,就放行了一部分url无需认证权限控制。
  2. 然后其他的所有url都需要进行认证权限控制。
  3. 配置代码如下:
    配置的忽略验证的路径,下面是已经修改后的配置,startWith配置的忽略路径url去除配置的请求前缀/todo
server:
  tomcat:
    uri-encoding: UTF-8
    max-threads: 1000
    min-spare-threads: 30
    connection-timeout: 5000ms
  port: 8088
  servlet:
    context-path: /todo
security:
  oauth2:
    ignore:
      authentication:
        startWith: /oauth/**,/rsa/**,/open/**,/druid/**,/redis/deduct/**,/todoUser/singleLogin,/todoUser/mingleLogin
package com.cn.sharedframework.Config.oauth;

import com.cn.sharedframework.Oauth2.OauthUserDetailsService;
import com.cn.sharedframework.Oauth2.granter.MobileAuthenticationProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

import java.util.ArrayList;
import java.util.List;

/**
 - spring security配置类
 -  3. @author qianshijiang
 - @date 2021-12-01 16:06
 - @description:
 */
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
public class WebServerSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private OauthUserDetailsService userDetailsService;

    @Autowired
    private SecurityOauth2Properties securityOauth2Properties;

    // 忽略一些静态资源
    @Override
    public void configure(WebSecurity web) throws Exception {
        List<String> ignoreUrlList = new ArrayList<>();
        ignoreUrlList.add("/**/*.js");
        ignoreUrlList.add("/**/*.css");
        ignoreUrlList.add("/**/*.jpg");
        ignoreUrlList.add("/**/*.png");
        ignoreUrlList.add("/**/*.ico");
        ignoreUrlList.add("/**/*.gif");
        ignoreUrlList.add("/swagger-ui.html");
        ignoreUrlList.add("/webjars/**");
        ignoreUrlList.add("/v2/**");
        ignoreUrlList.add("/swagger-resources/**");
        String[] ignoreUrls = ignoreUrlList.toArray(new String[ignoreUrlList.size()]);
        web.ignoring().antMatchers(ignoreUrls);
    }


    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().antMatchers(securityOauth2Properties.getStartWith()).permitAll()
            .anyRequest().authenticated() // 这里将其他所有的请求都放行,交给Oauth去处理
            .and().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
            .and().csrf().disable();
    }

    /**
     * 注入自定义的userDetailsService实现,获取用户信息,设置密码加密方式
     *
     * @param authenticationManagerBuilder
     * @throws Exception
     */
    @Override
    protected void configure(AuthenticationManagerBuilder authenticationManagerBuilder) throws Exception {
        authenticationManagerBuilder
                .userDetailsService(userDetailsService)
                .passwordEncoder(passwordEncoder());
        // 设置手机验证码登陆的AuthenticationProvider
        authenticationManagerBuilder.authenticationProvider(mobileAuthenticationProvider());
    }

    /**
     * 将 AuthenticationManager 注册为 bean , 方便配置 oauth server 的时候使用
     */
    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    /**
     * 创建手机验证码登陆的AuthenticationProvider
     *
     * @return mobileAuthenticationProvider
     */
    @Bean
    public MobileAuthenticationProvider mobileAuthenticationProvider() {
        MobileAuthenticationProvider mobileAuthenticationProvider = new MobileAuthenticationProvider(this.userDetailsService);
        mobileAuthenticationProvider.setPasswordEncoder(passwordEncoder());
        return mobileAuthenticationProvider;
    }

    public static void main(String[] args){
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        System.out.println(bCryptPasswordEncoder.encode("todo-server-secret-key"));
    }
}
  • Postmain测试接口发现如下提示
    SpringSecurity过滤指定url【.antMatchers(***).permitAll()】失效问题

发现问题一直困扰,最终找到解决方案。

  • 在网上找了大量资料发现,SpringSecurity认证忽略的url是不能包含context前缀的,否则匹配不上。
  • SpringSecurity认证忽略的url是不能包含context前缀的,否则匹配不上。
  • SpringSecurity认证忽略的url是不能包含context前缀的,否则匹配不上。

到了这里,关于SpringSecurity过滤指定url【.antMatchers(***).permitAll()】失效问题的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Django后台和微信小程序之间使用session方法,出现小程序访问404,Django后台找不到指定的URL问题解决

    在Django后台开启session中间件,小程序端请求Django后台的session属性,在Django中执行session会话操作,并响应应答给小程序,在小程序端执行cookie的缓存和读取操作。 在上述的功能完成后,小程序端访问时出现404错误,Django后台找不到指定的URL路径。 1、排查Django后台的URL路径是

    2024年02月06日
    浏览(34)
  • SpringBoot在使用SpringSecurity时,配置跨域过滤器CorsFilter不生效分析解决

    此文中代码只粘贴部分代码,完整版请自行查看 请求一般为重启debug服务再次请求 一般配置方法(适用于没有SpringSecurity配置时) SpringSecurity配置时配置方法 首先说明Spring中过滤器加载使用的是FilterRegistrationBean配置过滤器,FilterRegistrationBean实现了Ordered接口,order排序值默认值为

    2024年01月20日
    浏览(29)
  • [CSCCTF 2019 Qual]FlaskLight 过滤 url_for globals 绕过globals过滤

    目录 subprocess.Popen FILE warnings.catch_warnings site._Printer 这题很明显就是 SSTI了 源代码 我们试试看 {{7*7}} 然后我们就开始吧 原本我的想法是直接{{url_for.__globals__}} 但是回显是直接500 猜测过滤 我们正常来吧 这里我们需要 os 来调用 但是这里存在一个类 可以不需要os Python3 subprocess

    2024年02月07日
    浏览(25)
  • nginx拦截指定的url

    在配置nginx规则拦截的时候我们要清楚自己的需求 需求如下: 外网地址: 不能被访问 内网地址: 能访问 外网地址如下:外网ip+端口/pac/pul/check     (这里域名ip都可以) 内网地址如下:内网ip+端口/pac/pul/check               (内网ip不做拦截) nginx配置如下: 上面的配置和

    2024年01月23日
    浏览(23)
  • 腾达(Tenda)FH451路由器通过设置URL过滤限制网页访问

    适用路由器型号:F450/F451/F453/Ff455/F456/FH450/FH451 通过设置URL过滤来限制连接到该路由器下打开的网页,通过进入到路由器管理界面中(在地址栏中输入默认IP地址:192.168.0.1),在安全设置-URL过滤,中进行设置。 登陆管理界面 你需要登录到路由器管理界面来进行设置,此时,

    2024年02月06日
    浏览(49)
  • js跳转到指定url

    js怎么跳转到指定url方法如下: 需求:页面上点击按钮 需要调用设备提供的地址

    2024年02月11日
    浏览(43)
  • @FeignClient指定多个url实现负载均衡

    C知道回答的如下: 在使用 FeignClient 调用多个 URL 实现负载均衡时,可以使用 Spring Cloud Ribbon 提供的功能来实现。下面是一个示例代码:  首先,在Spring Boot主类上添加@EnableFeignClients注解启用Feign Client功能。 然后,在Spring Boot 项目的 Maven 配置文件中,添加以下依赖: 接下来

    2024年02月14日
    浏览(21)
  • 自定义TypeFilter 指定@ComponentScan注解的过滤规则

    在使用@ComponentScan注解实现包扫描时,可以使用@Filter指定过滤规则,在@Filter中,通过type来指定过滤的类型。而@Filter注解中的type属性是一个FilterType枚举,其源码如下。 例如,使用@ComponentScan注解进行包扫描时,如果要想按照注解只包含标注了@Controller注解的组件,那么就需要

    2023年04月24日
    浏览(30)
  • SpringBoot缓存注解@Cacheable之自定义key策略及缓存失效时间指定

    1. 项目依赖 本项目借助 SpringBoot 2.2.1.RELEASE  +  maven 3.5.3  +  IDEA  +  redis5.0 进行开发 开一个 web 服务用于测试 1. key 生成策略 对于 @Cacheable 注解,有两个参数用于组装缓存的 key cacheNames/value: 类似于缓存前缀 key: SpEL 表达式,通常根据传参来生成最终的缓存 key 默认的 redisK

    2024年02月19日
    浏览(30)
  • Linux :: 内容过滤指令【3】:grep 指令【详解】:在指定文件中过滤搜索信息、(模糊)查找包含指定字符串的内容!(如:系统故障时,查看操作日志信息等情景)

    前言:本篇是 Linux 基本操作篇章的内容! 笔者使用的环境是基于腾讯云服务器:CentOS 7.6 64bit。 学习集: C++ 入门到入土!!!学习合集 Linux 从命令到网络再到内核!学习合集 注:find 指令常与 grep 指令在面试中被提及,需让你回答异同! 目录索引: 1. 基本语法、功能及使

    2024年02月09日
    浏览(45)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包