Loading [MathJax]/jax/input/TeX/config.js
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >如何在自定义筛选器中使用Java配置注入AuthenticationManager

如何在自定义筛选器中使用Java配置注入AuthenticationManager
EN

Stack Overflow用户
提问于 2014-02-07 16:58:02
回答 2查看 123.7K关注 0票数 86

我使用的是Spring Security 3.2和Spring 4.0.1

我正在将xml配置转换为Java配置。当我在过滤器中使用@Autowired注释AuthenticationManager时,我得到了一个异常

代码语言:javascript
运行
AI代码解释
复制
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.security.authentication.AuthenticationManager] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}

我尝试过注入AuthenticationManagerFactoryBean,但也失败了,出现了一个类似的异常。

下面是我正在处理的XML配置

代码语言:javascript
运行
AI代码解释
复制
<?xml version="1.0" encoding="UTF-8"?> <beans ...>
    <security:authentication-manager id="authenticationManager">
        <security:authentication-provider user-service-ref="userDao">
            <security:password-encoder ref="passwordEncoder"/>
        </security:authentication-provider>
    </security:authentication-manager>

    <security:http
            realm="Protected API"
            use-expressions="true"
            auto-config="false"
            create-session="stateless"
            entry-point-ref="unauthorizedEntryPoint"
            authentication-manager-ref="authenticationManager">
        <security:access-denied-handler ref="accessDeniedHandler"/>
        <security:custom-filter ref="tokenAuthenticationProcessingFilter" position="FORM_LOGIN_FILTER"/>
        <security:custom-filter ref="tokenFilter" position="REMEMBER_ME_FILTER"/>
        <security:intercept-url method="GET" pattern="/rest/news/**" access="hasRole('user')"/>
        <security:intercept-url method="PUT" pattern="/rest/news/**" access="hasRole('admin')"/>
        <security:intercept-url method="POST" pattern="/rest/news/**" access="hasRole('admin')"/>
        <security:intercept-url method="DELETE" pattern="/rest/news/**" access="hasRole('admin')"/>
    </security:http>

    <bean class="com.unsubcentral.security.TokenAuthenticationProcessingFilter"
          id="tokenAuthenticationProcessingFilter">
        <constructor-arg value="/rest/user/authenticate"/>
        <property name="authenticationManager" ref="authenticationManager"/>
        <property name="authenticationSuccessHandler" ref="authenticationSuccessHandler"/>
        <property name="authenticationFailureHandler" ref="authenticationFailureHandler"/>
    </bean>

</beans>

下面是我正在尝试的Java配置

代码语言:javascript
运行
AI代码解释
复制
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private UserDetailsService userDetailsService;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private AuthenticationEntryPoint authenticationEntryPoint;

    @Autowired
    private AccessDeniedHandler accessDeniedHandler;

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth
                .userDetailsService(userDetailsService).passwordEncoder(passwordEncoder);
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .sessionManagement()
                    .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                    .and()
                .exceptionHandling()
                    .authenticationEntryPoint(authenticationEntryPoint)
                    .accessDeniedHandler(accessDeniedHandler)
                    .and();
        //TODO: Custom Filters
    }
}

这是Custom Filter类。给我带来麻烦的那条线路是AuthenticationManager的setter

代码语言:javascript
运行
AI代码解释
复制
@Component
public class TokenAuthenticationProcessingFilter extends AbstractAuthenticationProcessingFilter {


    @Autowired
    public TokenAuthenticationProcessingFilter(@Value("/rest/useAuthenticationManagerr/authenticate") String defaultFilterProcessesUrl) {
        super(defaultFilterProcessesUrl);
    }


    @Override
    public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException, IOException, ServletException {
      ...
    }

    private String obtainPassword(HttpServletRequest request) {
        return request.getParameter("password");
    }

    private String obtainUsername(HttpServletRequest request) {
        return request.getParameter("username");
    }

    @Autowired
    @Override
    public void setAuthenticationManager(AuthenticationManager authenticationManager) {
        super.setAuthenticationManager(authenticationManager);
    }

    @Autowired
    @Override
    public void setAuthenticationSuccessHandler(AuthenticationSuccessHandler successHandler) {
        super.setAuthenticationSuccessHandler(successHandler);
    }

    @Autowired
    @Override
    public void setAuthenticationFailureHandler(AuthenticationFailureHandler failureHandler) {
        super.setAuthenticationFailureHandler(failureHandler);
    }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-02-07 23:19:26

重写WebSecurityConfigurerAdapter中的authenticationManagerBean方法以将使用configure(AuthenticationManagerBuilder)构建的AuthenticationManager公开为Spring bean:

例如:

代码语言:javascript
运行
AI代码解释
复制
   @Bean(name = BeanIds.AUTHENTICATION_MANAGER)
   @Override
   public AuthenticationManager authenticationManagerBean() throws Exception {
       return super.authenticationManagerBean();
   }
票数 204
EN

Stack Overflow用户

发布于 2022-02-02 02:05:25

当我在同一个类中@Bean和@Autowired时,需要激活循环引用,但这与AuthenticationManager不同。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21633555

复制
相关文章
Spring Security无法注入authenticationManager
Description: Field authenticationManager in xxx.auth.config.MyAuthorizationConfig required a bean of type 'org.springframework.security.authentication.AuthenticationManager' that could not be found. Action: Consider defining a bean of type 'org.springframework.security.authentication.AuthenticationManager' in your configuration.
似水的流年
2019/12/05
5.5K0
java 自定义类加载器_JAVA中如何使用应用自定义类加载器「建议收藏」
最近在研究java CLASS LOADING技术,已实现了一个自定义的加载器。对目前自定义加载器的应用,还在探讨中。下面是自定义的CLASSLOADER在JAVA加密解密方面的一些研究。
全栈程序员站长
2022/10/04
9950
如何在 Spring 中使用依赖注入
每个开始学习 Spring 框架的人都应该听说过依赖注入,但到底这意味着什么?好吧,不就是去源码吗,让我们看看Spring的文档:
用户4235284
2023/10/14
3500
pycharm如何配置anaconda解释器_如何在pycharm中配置anaconda
python解释器有好多版本,Anaconda里面包含了python解释器,并且包含了很多其他的工具包,所以我们只安装1个Anaconda即可。
全栈程序员站长
2022/11/16
1.4K0
如何在列表,字典、集合中筛选数据——进阶学习
我们先生成一个字典,比如生成班上学上的成绩,班上有10个人,我们要进行筛选分数及格的同学
Gorit
2021/12/09
2.4K0
如何在列表,字典、集合中筛选数据——进阶学习
使用Gradle自定义配置构建Java程序
某些情况下默认的源代码路径等可能不符合我们项目的结构,这时除了修改项目结构外,我们还可以自定义源代码路径等配置。
三产
2021/01/12
9200
Spring使用注解配置依赖注入
大部分情况下,使用Spring配置依赖注入时,都是使用注解来进行配置,因为注解比xml要方便和简单。不过类似于数据源对象这种配置信息容易变更的对象除外,这种对象使用xml文件来进行配置会更适合,方便于在外部进行修改,而不需要打开代码来进行修改。
端碗吹水
2020/09/23
9650
【译】Spring 官方教程:Spring Security 架构
原文:Spring Security Architecture 译者:徐靖峰 校对:马超君 专题指南 本文是 Spring Security 的入门指南,并对 Spring Security 的框架设计和基础组件进行深度解析。我们仅涉及应用程序安全性的基础知识,但这已足够消除开发人员在使用 Spring Security 时遇到的一些困惑。要做到这一点,我们需要了解如何使用过滤器和方法注解来保障Web应用程序的安全性。如果你需要了解高级别安全应用程序的工作方式,以及如何定制安全应用程序,或只需要学习如何思考
程序猿DD
2018/03/26
1.9K0
【译】Spring 官方教程:Spring Security 架构
Power Pivot中筛选条件的使用
在Power Pivot中,在大部分时间里,筛选是作为一个主要的功能运用到各个地方,筛选上下文,行上下文都和筛选相关。
逍遥之
2020/03/23
5K0
Spring Security 实战干货:AuthenticationManager的初始化细节
今天有个同学告诉我,在Security Learning项目的day11分支中出现了一个问题,验证码登录和其它登录不兼容了,出现了No Provider异常。还有这事?我赶紧跑了一遍还真是,看来我大意了,不过最终找到了原因,问题就出在AuthenticationManager的初始化上。自定义了一个UseDetailService和AuthenticationProvider之后AuthenticationManager的默认初始化出问题了。
码农小胖哥
2021/02/01
2.4K0
mybatis-plus自定义sql注入器
虽然mybats-plus的BaseMapper提供了非常好用,非常多的方法,但是如果我们需要扩充BaseMapper中的方法又该如何实现呢
许喜朝
2020/08/11
2.1K0
1.3 筛选器
数据透视表是Excel历史上最伟大的发明,然其本质上是个很简单的原理,就是一个漏斗,即筛选器。按照不同的角度筛选输出分析结果。
公众号PowerBI大师
2019/08/07
1.6K0
1.3 筛选器
了解vSphere中的BPDU筛选器功能
本文介绍并提供了有关vSphere 5.1 Distributed Switch中新BPDU筛选器功能的示例。 一、什么是bpdu 桥接协议数据单元(BPDU)是在物理交换机之间交换的帧,作为生成树协议(STP)的一部分。STP用于防止网络中的环路,通常在物理交换机上启用。当物理交换机端口上的链路上升时,STP协议开始计算和BPDU交换以确定端口是否应处于转发或阻塞状态。桥接协议数据单元(BPDU)帧跨物理交换机端×××换以识别根网桥并形成树形拓扑。VMware的vSwitch不支持STP,也不参与BPDU交换。如果在vSwitch上行链路上接收到BPDU帧,则丢弃该帧。同样,VMware vSwitch不会生成BPDU帧。 注意:VMware vSwitches(标准和分布式)无法形成循环,因为无法在OSI层的第2层将两个虚拟交换机连接在一起。因此,没有生成树协议功能已合并到虚拟交换机中。
孙杰
2019/10/29
2.4K0
聊聊自定义SPI如何使用自定义标签注入到spring容器中
之前我们聊过自定义的SPI如何与spring进行整合,今天我们就来聊下如何通过自定义标签将spi对象注入到spring容器中
lyb-geek
2022/01/07
6050
聊聊自定义SPI如何使用自定义标签注入到spring容器中
之前我们聊过自定义的SPI如何与spring进行整合,今天我们就来聊下如何通过自定义标签将spi对象注入到spring容器中
lyb-geek
2021/11/25
6720
聊聊自定义SPI如何使用自定义标签注入到spring容器中
如何在CentOS中自定义Nginx服务器的名称
本教程可帮助您自定义主机上的服务器名称。通常,出于安全考虑,各公司会修改服务器名称。自定义nginx服务器的名称需要修改源代码。
葡萄
2018/07/25
2.4K0
Spring Security 案例实现和执行流程剖析
Spring Security 是 Spring 社区的一个顶级项目,也是 Spring Boot 官方推荐使用的安全框架。除了常规的认证(Authentication)和授权(Authorization)之外,Spring Security还提供了诸如ACLs,LDAP,JAAS,CAS等高级特性以满足复杂场景下的安全需求。
朝雨忆轻尘
2019/06/19
2.1K0
Spring Security 案例实现和执行流程剖析
Spring Security 的核心组件AuthenticationManager
Spring Security是一个非常流行的安全框架,它提供了一系列的安全功能,包括身份认证、授权、攻击防护等。其中,身份认证是Spring Security的核心功能之一,而AuthenticationManager就是Spring Security中负责身份认证的核心组件之一。
堕落飞鸟
2023/04/14
5590
SpringCloud Alibaba微服务实战十八 - Oauth2.0 自定义授权模式
那么如何新增一个自定义的授权模式,比如像下面这样根据手机号和短信验证码进行登录呢?
JAVA日知录
2020/07/31
2.6K0
SpringCloud Alibaba微服务实战十八 - Oauth2.0 自定义授权模式
点击加载更多

相似问题

如何在自定义筛选器中使用Java配置注入AuthenticationManager

31

Spring :使用自定义AuthenticationManager配置UserDetailsService

113

必须指定Spring Security authenticationmanager -用于自定义筛选器

25

防止spring注入默认AuthenticationManager

21

spring安全自定义在AuthenticationManager中注入AuthenticationProvider

10
添加站长 进交流群

领取专属 10元无门槛券

AI混元助手 在线答疑

扫码加入开发者社群
关注 腾讯云开发者公众号

洞察 腾讯核心技术

剖析业界实践案例

扫码关注腾讯云开发者公众号
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档