spring - 需要一个找不到的 '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;
}
}
我该如何解决这个问题?
解决方案
您的代码中存在很多问题。
(ApplicationContext) authenticationManager()
您不能将 AuthenticationManager 转换为 ApplicationContext.addFilter(new ApplicationContextHeaderFilter(...))
我不知道您为什么要使用ApplicationContextHeaderFilter
简单的演示应用程序。您应该有提供
BasicAuthenticationFilter
的首选甚至简单的默认HttpSecurity
配置.httpBasic()
你应该有首选
UsernamePasswordAuthenticationFilter
甚至是简单的默认HttpSecurity
配置.formLogin()
CustomAuthenticationProvider extends BasicAuthenticationFilter
身份验证提供者是实现AuthenticationProvider
接口的提供者。在您的情况下,命名应该是xxxAuthFilter
.您在下面的代码中什么也没做。(获取现有的身份验证对象并将其设置回来,而不创建有效的身份验证对象。)
UsernamePasswordAuthenticationToken authentication = getAuthentication(request);
SecurityContextHolder.getContext().setAuthentication(authentication);
从 AuthenticationManager 和 AuthFilters 实现的角度来看,对于添加过滤
器,您可以添加 Spring Security 提供的过滤器的任何实现,如下
所示authenticationManager 已配置)例如,如果您考虑或UsernamePasswordAuthenticationFilter
BasicAuthenticationFilter
您应该注意设置
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
它的顺序是否是在创建安全上下文之后,那么你可以将身份验证对象设置为安全上下文。
推荐阅读
- python - Odoo KeyError:mail.alias.alias_domain
- javascript - 固定水平滚动表内的行
- java - 滚动列表视图时文本视图中的数据切换
- android-studio - 很多工具都不见了
- docker - 如何从本地 dhcp 服务器发布的 IP 访问托管在 Openshift 集群中的网站
- java - 如何在 mongodb 中持久化 JsonNode 对象
- python - k-means ++ 适合大数据吗?
- android - 将 Android 应用程序从 SQLiteOpenHelper 迁移到 Room
- android - 在 Android 上检索 Facebook 个人资料帖子(状态)
- javascript - 为什么我不能从本地存储中取出解析的 JSON 对象?