我在 Spring 安全性中为我的身份验证管理器设置了父项,但它不起作用
I set a parent to my Authentication Manager in Spring security but it didn't work
我正在尝试练习 spring 安全,这是我的 spring 安全配置
@Configuration
@EnableWebSecurity
public class ProjectConfig extends WebSecurityConfigurerAdapter {
@Autowired
AuthenticationProvider authenticationProvider;
@Autowired
AuthenticationManagerBuilder builder;
@Bean
public AuthenticationManager global() throws Exception {
builder
.inMemoryAuthentication()
.passwordEncoder(NoOpPasswordEncoder.getInstance())
.withUser("admin")
.password("123")
.authorities(() -> "ADMIN");
return builder.build();
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.antMatcher("/hello")
.authorizeRequests()
.anyRequest()
.authenticated();
}
@Bean
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(authenticationProvider);
auth.parentAuthenticationManager(global());
}
}
这是我的自定义身份验证提供程序:
@Component
public class CustomProvider implements AuthenticationProvider {
@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
return BadCredentialsException("error");
}
@Override
public boolean supports(Class<?> authentication) {
return true;
}
}
我了解了如何为身份验证管理器创建父项并尝试对其进行测试。每次我使用 Postman 发出请求时,我都会收到 403 错误。我的配置有什么问题?
Postman
首先 Spring 使用 ProviderManager
class 作为 AuthenticationManager
接口的实现,如果你看到 authenticate
方法的实现,你就会发现如果您的子结果是 null
而不是 exception
,它仅使用父身份验证管理器。
if (result == null && this.parent != null) {
// Allow the parent to try.
try {
parentResult = this.parent.authenticate(authentication);
result = parentResult;
}
// other stuff
}
所以将CustomProvider
中的以下代码改成returns null
@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
return AuthenticationException("error");
}
我还从图像中注意到您正在使用 http 基本身份验证,但您没有在配置中启用它。
http
.antMatcher("/hello")
.authorizeRequests()
.anyRequest()
.authenticated()
.and()
.httpBasic();
我正在尝试练习 spring 安全,这是我的 spring 安全配置
@Configuration
@EnableWebSecurity
public class ProjectConfig extends WebSecurityConfigurerAdapter {
@Autowired
AuthenticationProvider authenticationProvider;
@Autowired
AuthenticationManagerBuilder builder;
@Bean
public AuthenticationManager global() throws Exception {
builder
.inMemoryAuthentication()
.passwordEncoder(NoOpPasswordEncoder.getInstance())
.withUser("admin")
.password("123")
.authorities(() -> "ADMIN");
return builder.build();
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.antMatcher("/hello")
.authorizeRequests()
.anyRequest()
.authenticated();
}
@Bean
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(authenticationProvider);
auth.parentAuthenticationManager(global());
}
}
这是我的自定义身份验证提供程序:
@Component
public class CustomProvider implements AuthenticationProvider {
@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
return BadCredentialsException("error");
}
@Override
public boolean supports(Class<?> authentication) {
return true;
}
}
我了解了如何为身份验证管理器创建父项并尝试对其进行测试。每次我使用 Postman 发出请求时,我都会收到 403 错误。我的配置有什么问题? Postman
首先 Spring 使用 ProviderManager
class 作为 AuthenticationManager
接口的实现,如果你看到 authenticate
方法的实现,你就会发现如果您的子结果是 null
而不是 exception
,它仅使用父身份验证管理器。
if (result == null && this.parent != null) {
// Allow the parent to try.
try {
parentResult = this.parent.authenticate(authentication);
result = parentResult;
}
// other stuff
}
所以将CustomProvider
中的以下代码改成returns null
@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
return AuthenticationException("error");
}
我还从图像中注意到您正在使用 http 基本身份验证,但您没有在配置中启用它。
http
.antMatcher("/hello")
.authorizeRequests()
.anyRequest()
.authenticated()
.and()
.httpBasic();