这是我们的项目结构
[INFO] hh [pom]
[INFO] common-service [jar]
[INFO] account-service [jar]
[INFO] product-service [jar]
[INFO] auth-service [jar]
[INFO] application-service [war]
安全服务和配置在
auth-service
中提供,因此在配置它们之后所有API都是安全的,并且在响应中给出了身份验证错误401
。
{
"timestamp": "2023-04-10T04:29:05.509+00:00",
"status": 401,
"error": "Unauthorized",
"message": "Unauthorized",
"path": "/v1/product"
}
但是我们想将这个 URL 列入白名单(跳过安全)
/v1/product
,所以从这个reference我们更新了我们的配置。
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/v1/product/**");
}
但是 API 响应仍然是
401
,当我们给出 username
和生成的 password
时,我们得到了正确的响应。我们甚至尝试在此方法中添加记录器,但这些记录器在启动期间或 API 被访问时并未出现。这是配置类
@Configuration
@EnableWebSecurity
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {
@Autowired
private UserDetailsService userDetailsService;
@Bean
AuthenticationProvider authenticationProvider() {
DaoAuthenticationProvider daoAuthenticationProvider = new DaoAuthenticationProvider();
daoAuthenticationProvider.setUserDetailsService(userDetailsService);
daoAuthenticationProvider.setPasswordEncoder(new BCryptPasswordEncoder());
return daoAuthenticationProvider;
}
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/v1/product/**");
}
}
转为单模块项目后的变化
当我们将所有模块合并到一个项目中时,上述 API 会被
spring-security
忽略,但是当我们将这些注释 @ComponentScan
、@EntityScan
和 @EnableJpaRepositories
添加到主应用程序类时,相同的请求失败了401
!
那么多模块项目设置的spring security配置有什么变化吗?