必须指定 Spring Security 身份验证管理器 - 用于自定义过滤器

2024-05-02

我正在尝试创建自定义用户名密码身份验证过滤器,因为我需要验证来自两个不同来源的密码。我正在使用 Spring Boot 1.2.1 和 Java 配置。我在部署时遇到的错误是

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'customUsernamePasswordAuthenticationFilter' defined in file [/Users/rjmilitante/Documents/eclipse-workspace/login-service/bin/com/elsevier/eols/loginservice/CustomUsernamePasswordAuthenticationFilter.class]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: authenticationManager must be specified
…
Caused by: java.lang.IllegalArgumentException: authenticationManager must be specified

我不确定我错过了什么。我一直在尝试在我的SecurityConfig 中为此过滤器设置authenticationManager。我的代码看起来像

我的过滤器:

@Component
public class CustomUsernamePasswordAuthenticationFilter extends AbstractAuthenticationProcessingFilter {

    public CustomUsernamePasswordAuthenticationFilter(RequestMatcher requiresAuthenticationRequestMatcher) {
        super(requiresAuthenticationRequestMatcher);
        // TODO Auto-generated constructor stub
    }

    public CustomUsernamePasswordAuthenticationFilter() {
        super(new AntPathRequestMatcher("/login","POST"));
        // TODO Auto-generated constructor stub
    }

    public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response)
            throws AuthenticationException {
        // String dbValue = request.getParameter("dbParam");
        // request.getSession().setAttribute("dbValue", dbValue);
        System.out.println("attempting to authentificate");
        while (request.getAttributeNames().hasMoreElements()) {
            String e = (String) request.getAttributeNames().nextElement();
            System.out.println("param name : " + e + " and param value : " + request.getAttribute(e));
        }
        return null;
    }
}

我的安全配置:

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter{

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

    @Bean(name="loginService")
    public LoginService loginService(){
        return new LoginServiceImpl();
    }

    @Bean( name="myAuthenticationManager")
     @Override
     public AuthenticationManager authenticationManagerBean() throws Exception {
         return super.authenticationManagerBean();
     }

    @Bean
    CustomUsernamePasswordAuthenticationFilter customUsernamePasswordAuthenticationFilter() throws Exception {
        CustomUsernamePasswordAuthenticationFilter customUsernamePasswordAuthenticationFilter = new CustomUsernamePasswordAuthenticationFilter();
        customUsernamePasswordAuthenticationFilter.setAuthenticationManager(authenticationManagerBean());
      return customUsernamePasswordAuthenticationFilter;
    }


    @Autowired
    private myAuthenticationProvider myAuthenticationProvider;

    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable()
        .authorizeRequests()
        .anyRequest().authenticated()
        .and()
        /*.addFilterBefore(customUsernamePasswordAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class)*/;

    }

    public void configure(AuthenticationManagerBuilder auth)  throws Exception {

        auth.authenticationProvider(myAuthenticationProvider);
        }
}

有人可以看一下吗?不知道怎么了。


The 文档 https://docs.spring.io/spring-security/site/docs/current/api/org/springframework/security/web/authentication/AbstractAuthenticationProcessingFilter.html for AbstractAuthenticationProcessingFilter指出您必须设置一个AuthenticationManager.

我建议你尝试在你的程序中添加以下代码CustomUsernamePasswordAuthenticationFilter class:

@Override
@Autowired
public void setAuthenticationManager(AuthenticationManager authenticationManager) {
    super.setAuthenticationManager(authenticationManager);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

必须指定 Spring Security 身份验证管理器 - 用于自定义过滤器 的相关文章

随机推荐

  • 在react-native中将本地图像uri作为props传递

    我正在尝试将图像的 uri 作为 prop 传递 以便我可以在 React Native 上多次重复使用它 但是 当前的解决方案提示我 require 应该使用字符串文字 const ImageButton source gt
  • Twitter APi“代码”:215,“消息”:“错误的身份验证数据

    接收错误 code 215 message 错误的身份验证数据 in my page var dump page 我读到 Friends exist 不再适用于 1 1 版本 因此我需要使用 Friends lookup 来比较一个 Twi
  • Azure Pipelines 状态徽章未显示在 Markdown 中

    我已经为我的 github 存储库之一设置了 azure 管道 除了构建状态徽章之外 一切都工作正常 它没有正确显示 似乎无法加载图像 目前正在显示 Edit markdown 文件中使用的代码由 azure devops 自动生成 Bui
  • Oracle BLOB 与 VARCHAR

    我需要在表的一列中存储一个 大 SQL 查询 我想使用BLOB场地 需要明确的是 我想存储查询 而不是其结果 最好使用什么 BLOB or a VARCHAR 或者也许还有别的什么 另一种选择是 CLOB 对于文本数据 使用 CLOB 比使
  • git 存储库中的提交次数

    我的一个为期 5 个月的项目即将结束 作为毫无意义的统计数据的粉丝 我想知道自存储库启动以来已经进行了多少次提交 我怎样才能发现这一点 Notes 我知道没有one存储库 我只对本地版本感兴趣 这在颠覆中是微不足道的 因为修订标识符 似乎是
  • 回发后我的 JavaScript 函数在 ASP.NET 中不起作用

    我有共同的功能 我把它折叠起来CommonFunctions js在脚本文件夹中 我将它包含在我的主页上并在我的页面上使用它 当我在页面上进行任何回发时 我的功能不起作用 My CommonFunctions js function gf
  • 使用 List.Sort(Comparison Comparison 在 C# 中对列表进行排序

    我创建了一个类 如下所示 public class StringMatch public int line num public int num of words 我创建了一个列表 List
  • 共享 Google 地图或拍摄 Android 手机屏幕截图

    我正在使用 android google map api v2 开发 android 应用程序 到目前为止我已经取得了以下成绩 打开谷歌地图显示用户的位置 当他行走时 他可以在地图上添加标记 标记他经过的地方 他可以删除标记或拖动它们 我现
  • Kotlin:乐趣与 val

    Kotlin 支持计算属性但我不确定何时使用它们 假设我有一堂课 class Car val color String 并有这个返回的函数true如果汽车是白色的 fun isWhite car Car Boolean return car
  • 各种 Android 设备的应用程序背景大小

    我正在为所有 Android 设备的应用程序设计背景 我在想图像的大小 以像素为单位 是多少 从开发者网站我发现了以下等式 px dp dpi 160 那么 px 取决于两个变量 首先 dp 我们有 xlarge screens are a
  • Ember-cli:导入毯子.js 导致测试运行程序挂起

    我目前正在使用 ember cli 和 ember qUnit 进行测试 我还想将代码覆盖率结果添加到测试输出中 因此经过一些研究后 blanketjs 似乎是可行的方法 我使用以下方法安装了毯子 npm 安装毯子 并将毯子文件夹移至 em
  • 我可以使用 Google Maps API v3 操作 KML 吗?

    我正在 Google Maps API v3 中使用 KMLLayer 加载 KML 是否可以引用地图上的多边形并执行诸如更改颜色或透明度之类的操作 不 你不能那样做 因为 kmllayer 中没有像对象一样的多边形 来自谷歌文档 http
  • 排除“解析错误,意外的‘>’”错误[关闭]

    这个问题不太可能对任何未来的访客有帮助 它只与一个较小的地理区域 一个特定的时间点或一个非常狭窄的情况相关 通常不适用于全世界的互联网受众 为了帮助使这个问题更广泛地适用 访问帮助中心 help reopen questions 我收到这个
  • SQS - 通过 ID 获取消息

    我是否可以使用 Amazon PHP SDK 根据消息 ID 从 SQS 队列获取消息 我是否必须获取队列中的所有消息 然后在服务器上对其进行过滤 我的服务器收到带有队列消息 ID 的 SNS 发起请求 我必须从来自 SQS 的消息数组中过
  • Swift 5 / Xcode 11 更新后模拟器在动画块处冻结

    我在 Xcode 11 中将项目更新为 Swift 5 现在程序在 iPhone 11 模拟器中的动画块处冻结 当我在动画之后设置断点时 它永远不会命中它 重新启动 Xcode 和模拟器并没有解决问题 如果我在设备上运行该程序 它可以正常工
  • 随着索引和文档数量恒定,elasticsearch 批量索引会随着时间的推移而变慢

    我遇到了使用 NET NEST 客户端和 ElasticSearch 进行批量索引的性能随着时间的推移 索引数量和文档数量恒定而降低的情况 我们正在奔跑ElasticSearch Version 0 19 11 JVM 23 5 b02在具
  • 如果 useAsync 为 true,FileStream.ReadAsync 会阻止 UI,但如果为 false,则不会阻止 UI

    我读到了关于useAsync参数在这个FileStream构造函数 FileStream String FileMode FileAccess FileShare Int32 Boolean https learn microsoft co
  • R 笔记本:opts_chunk 没有效果

    我正在开发我的第一台 R 笔记本 除了一个问题之外 它运行得很好 我想成为我内联输出的数字 r realbignumber 以逗号作为分隔符且最多 2 位小数 123 456 789 12 为了实现这一目标 我在文档的开头添加了一个块 其中
  • 智能感知永远加载

    Recently installed 16 5 and Intellisense never ends to load on a fairly simple project 我不得不说我运行的是 Ryzen 3990 和 SSD 所以很惊讶
  • 必须指定 Spring Security 身份验证管理器 - 用于自定义过滤器

    我正在尝试创建自定义用户名密码身份验证过滤器 因为我需要验证来自两个不同来源的密码 我正在使用 Spring Boot 1 2 1 和 Java 配置 我在部署时遇到的错误是 Caused by org springframework be