spring-boot - WebSecurityConfigurerAdapter 实现在库中不起作用
问题描述
亲爱的,
在以下问题中需要帮助。
我正在尝试创建一个供多个 MFE 应用程序使用的通用身份验证库。这个库依赖于 Spring Web Security。如果 WebSecurityConfigurerAdapter 实现在库中,则不会应用它的问题,但是如果它直接用于应用程序,它将起作用。
错误:选项方法失败
在两种情况下(在库内,直接在应用程序内)启动应用程序时都会调用更多的 Configure() 方法
下面是成功请求的屏幕截图(直接在应用程序内) 直接在应用程序内成功
下面是失败请求的屏幕截图(在库内) 库内 失败
下面是 WebSecurityConfigurerAdapter 的实现:
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class JWTWebSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private JwtUnAuthorizedResponseAuthenticationEntryPoint jwtUnAuthorizedResponseAuthenticationEntryPoint;
@Autowired
private UserDetailsService jwtInMemoryUserDetailsService;
@Autowired
private JwtTokenAuthorizationOncePerRequestFilter jwtAuthenticationTokenFilter;
@Value("${jwt.get.token.uri}")
private String authenticationPath;
@Bean
public PasswordEncoder passwordEncoderBean() {
return new BCryptPasswordEncoder();
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth
.userDetailsService(jwtInMemoryUserDetailsService)
.passwordEncoder(passwordEncoderBean());
}
@Bean
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
@Override
protected void configure(HttpSecurity httpSecurity) throws Exception {
httpSecurity
.csrf().disable()
.exceptionHandling().authenticationEntryPoint(jwtUnAuthorizedResponseAuthenticationEntryPoint).and()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
.authorizeRequests()
.anyRequest().authenticated();
httpSecurity
.addFilterBefore(jwtAuthenticationTokenFilter, UsernamePasswordAuthenticationFilter.class);
httpSecurity
.headers()
.frameOptions().sameOrigin() //H2 Console Needs this setting
.cacheControl(); //disable caching
}
@Override
public void configure(WebSecurity webSecurity) throws Exception {
webSecurity
.ignoring()
.antMatchers(
HttpMethod.POST,
authenticationPath
)
.antMatchers(HttpMethod.OPTIONS, "/**")
.and()
.ignoring()
.antMatchers(
HttpMethod.GET,
"/" //Other Stuff You want to Ignore
)
.and()
.ignoring()
.antMatchers("/h2-console/**/**");//Should not be in Production!
}
}
结论:不,我正在创建一个库,该库在该库中使用 Spring Security WebSecurityConfigurerAdapter 的实现。将此库添加为另一个应用程序的依赖项,然后它不起作用。虽然如果直接在应用程序本身上使用此实现将正常工作。注意:此库具有过滤器和与 JWT 实现相关的所有内容,这将是多个应用程序的可重用库。
解决方案
推荐阅读
- symfony - "make:entity --regenerate" 创建了一个不正确的 (?) 函数
- c# - Birthday dosen't appear on frm_Load
- javascript - Scraping an AJAX web page using python and requests
- python - 对非常大的熊猫数据框上的每一列进行排序
- asp.net - 如何根据 ID 列出模型?
- reporting-services - SSRS-报告服务配置:使链接(包含在电子邮件订阅中)指向网站,而不是内部
- r - GGPlot2:按条件分组的误差线
- python - 在函数中传递列名
- mathematical-optimization - CBC 求解器返回的解是否准确?
- postgresql - PostgreSQL => ALTER TABLE without timezone -> with timezone, using select for TZ