org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named ‘springSecurityFilter

SpringSecurity权限验证实验阶段操作。

配置类

package com.atguigu.crowd.mvc.config;

import org.springframework.context.annotation.Configuration;
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.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

// @EnableGlobalMethodSecurity(prePostEnabled=true)注解表示启用全局方法权限管理功能。
// 这个类有springmvc来扫描,加入到springmvc的容器中
@Configuration
@EnableWebSecurity
//@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebAppSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(AuthenticationManagerBuilder builder) throws Exception {
        // 临时使用内存版登陆的模式测试代码
        builder.inMemoryAuthentication().withUser("tom").password("123").roles("ADMIN");
    }

    @Override
    protected void configure(HttpSecurity security) throws Exception {
        security
                .authorizeRequests()  // 对请求进行授权 (针对登录页,静态资源无条件可以访问)
                .antMatchers("/admin/to/login/page.html","/bootstrap/**","/css/**","/fonts/**","/img/**","/jquery/**"," /layer/**","/script/**","/ztree/**")
                .permitAll()
                .anyRequest()                                   // 其他任意的请求
                .authenticated()                                // 认证后访问
                .and()
                .csrf()                                         // 之前已经写好的表单都没有带,也不想再回去改了,所以直接禁用了。
                .disable()                                      // 禁用csrf(跨站伪造功能)
                .formLogin()                                    // 开启表单登陆功能
                .loginPage("/admin/to/login/page.html")         // 设置登陆页面
                .permitAll()
                .loginProcessingUrl("/security/do/login.html")  //指定处理登陆请求页面的地址
                .permitAll()
                .usernameParameter("loginAcct")
                .passwordParameter("userPswd")
                .defaultSuccessUrl("/admin/to/main/page.html")  // 指定登陆成功后前往的地址。
                .permitAll()
                ;
    }
}

web.xml

<!--SpringSecurity-->
<filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

这个搞得我头痛,感觉就跟玄学一样

刚开始一致是这个类扫描不到容器中。加它跟每加没什么区别
后来再网上搜到的又加了一个配置类

package com.atguigu.crowd.mvc.config;

import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.security.web.context.AbstractSecurityWebApplicationInitializer;

import javax.servlet.ServletContext;

@Order(Ordered.HIGHEST_PRECEDENCE)
public class SecurityWebApplicationInitializer extends AbstractSecurityWebApplicationInitializer {

       /**
    spring或者springmvc中会自动将此类注入到父类中
    若当前环境没有使用Spring或SpringMVC,
    则需要将WebSecurityConfig(SpringSecurity配置类)传入超类,以确保获取配置,并创建springcontext
    public SecurityWebApplicationInitializer() {
        super(WebSecurityConfig.class);
    }
    */

}

这下能够扫描进来了。就开始遇到开头的问题
No bean named 'springSecurityFilter 异常

后来尝试了网上说的操作,把spring和springmvc的配置文件都扫描这个包

springmvc

<context:component-scan base-package="com.atguigu.crowd.mvc.config"/>

spring

<context:component-scan base-package="com.atguigu.crowd.mvc.config"/>

没用,还是这个问题。
后来又试了web.xml文件中删除ContextLoaderListener
spring和springmvc的配置文件都让 DispatcherServlet 来扫描

<servlet>
   <servlet-name>springDispatcherServlet</servlet-name>
   <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
   <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:spring-*.xml</param-value>
   </init-param>
   <load-on-startup>1</load-on-startup>
</servlet>

还是不行

最后又改了源码 还是不行。
然后又出来一些别的问题。
比如
not found for the web module.
没有META-INF
最后就调调调的。重启idea,清空浏览器,换各种浏览器

然后又把源码还原,恢复两个容器各自扫描

删除了 上面的SecurityWebApplicationInitializer 这个类

后来又回到第一步,再spring和springmvc两个配置文件中加入扫描
竟然又可以了。转了一圈,花了半天时间。又回到起点。

。。。。。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值