You are asking Spring Security to ignore Ant(xx) This is not recommended, please use permitAll... 处理

Spring Security之ignore not recommended 警告的处理

启动springboot项目时,出现一个warning:
You are asking Spring Security to ignore Ant(xxx) This is not recommended – please use permitAll via HttpSecurity#authorizeHttpRequests instead.

经查,是在Spring Security configuration类中,写了如下代码:

    @Override
    public void configure(WebSecurity web) throws Exception {
       web
                .ignoring().antMatchers("/js/**","/css/**","/jQuery/**","/images/**","/icon/**","/file/**");  
    }

此段代码是一开始配置spring security时,为了解决静态资源(js/css/图片等)被拦截器拦截的问题写的,也是网上搜索出来的标准答案。由warning提示可见,当前版本,这种配置形式已经不再推荐了。(deprecated?)

当前推荐的,应当写在HttpSecurity的配置里,即:

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .addFilterBefore(normalAuthenticationProcessingFilter(), AbstractPreAuthenticatedProcessingFilter.class) //注册用于普通登录请求的过滤器
                .authorizeRequests()
                .antMatchers(
                        "/about","/login/**","/login","/error",                                 //排除不需spring security验证的页面
                        "/js/**","/css/**","/jQuery/**","/images/**","/icon/**","/file/**").permitAll()    //解决静态资源被拦截的问题(新,写在这里)
                .anyRequest().fullyAuthenticated()  //若要给应用程序发送请求,则发送请求的用户必须先通过认证

                .and()
                ...

重新运行项目,静态资源正常加载,warning消失,问题解决!

–Written by 957lzy Victor

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值