首页 > 解决方案 > 需要一个找不到的 'org.springframework.security.authentication.AuthenticationManager' 类型的 bean。春天安全的消息

问题描述

我正在尝试使用 Spring Boot 为 Spring Security 实现一个示例演示,以检查身份验证。我正在尝试为 Spring Security 实施基本锻炼并收到以下消息,

Description:

Parameter 0 of constructor in com.spacestudy.service.CustomAuthenticationProvider 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.web.AuthenticationEntryPoint' in your configuration.

我的安全配置类 SecurityConfig.java,

@EnableWebSecurity
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

@Autowired
private AuthenticationEntryPoint authEntryPoint;

 @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
        .csrf().disable()
        .authorizeRequests()
        .anyRequest().authenticated()
        .and()
        .addFilter(new ApplicationContextHeaderFilter((ApplicationContext) authenticationManager()));
    }
}

我的 BasicAuthenticationFilter 实现如下,

@Component
public class  CustomAuthenticationProvider  extends  BasicAuthenticationFilter    { 

public CustomAuthenticationProvider(AuthenticationManager authenticationManager) {
    super(authenticationManager);
    // TODO Auto-generated constructor stub
}
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws IOException, ServletException {
    UsernamePasswordAuthenticationToken authentication = getAuthentication(request);
    SecurityContextHolder.getContext().setAuthentication(authentication);
    filterChain.doFilter(request, response);
}
private UsernamePasswordAuthenticationToken getAuthentication(HttpServletRequest request) {
   String bearerToken = request.getHeader("accessToken");
String username = "test";
String password = "test";
    if (username != null && !username.isEmpty()) {
        return new UsernamePasswordAuthenticationToken(username, null, null);
      }
    return null;
    }
 }

我该如何解决这个问题?

标签: springspring-bootspring-mvcspring-security

解决方案


您的代码中存在很多问题。

  1. (ApplicationContext) authenticationManager()
    您不能将 AuthenticationManager 转换为 ApplicationContext

  2. .addFilter(new ApplicationContextHeaderFilter(...))
    我不知道您为什么要使用ApplicationContextHeaderFilter简单的演示应用程序。

    • 您应该有提供BasicAuthenticationFilter的首选甚至简单的默认HttpSecurity配置.httpBasic()

    • 你应该有首选UsernamePasswordAuthenticationFilter甚至是简单的默认HttpSecurity配置.formLogin()

  3. CustomAuthenticationProvider extends BasicAuthenticationFilter
    身份验证提供者是实现AuthenticationProvider接口的提供者。在您的情况下,命名应该是xxxAuthFilter.

  4. 您在下面的代码中什么也没做。(获取现有的身份验证对象并将其设置回来,而不创建有效的身份验证对象。)

UsernamePasswordAuthenticationToken authentication = getAuthentication(request);
SecurityContextHolder.getContext().setAuthentication(authentication);

从 AuthenticationManager 和 AuthFilters 实现的角度来看,对于添加过滤
器,您可以添加 Spring Security 提供的过滤器的任何实现,如下
所示authenticationManager 已配置)例如,如果您考虑或UsernamePasswordAuthenticationFilterBasicAuthenticationFilter

您应该注意设置AuthenticationManager您的身份验证管理器应该在哪里覆盖authenticate()方法并且它应该返回Authentication对象(其中Authentication对象将具有身份验证主体、凭据和授予权限列表)

或者

如果您不想实现身份验证管理器...
在您的过滤器中以简单的方式(OncePerRequestFilter 的实现)doFilterInternal() 方法在 `SecurityContext` 中设置 `Authentication` 对象
List<GrantedAuthority> authorityList = new ArrayList<>();
GrantedAuthority authority = new SimpleGrantedAuthority("ROLE_USER");
authorityList.add(authority);
UsernamePasswordAuthenticationToken authToken = new UsernamePasswordAuthenticationToken(username, password, authorityList);
//Note UsernamePasswordAuthenticationToken implements Authentication
SecurityContextHolder.getContext().setAuthentication(authToken);

任何身份验证过滤器的工作方式是,如果有一个有效的Authentication对象,那么过滤器链将继续而不尝试身份验证,否则它将attemptAuthentication通过覆盖的attemptAuthentication()方法。
但是你ApplicationContextHeaderFilter是一个OncePerRequestFilter没有它的实现,attemptAuthentication()我不知道ApplicationContextHeaderFilter它的顺序是否是在创建安全上下文之后,那么你可以将身份验证对象设置为安全上下文。


推荐阅读