我正在将Sping Boot 应用程序从Sping Boot 2.6.6迁移到3.1.2,其中http安全性中的大多数方法都被弃用或删除。
我在谷歌上搜索了很多地方,但我无法将此特定配置转换为Sping Boot 3. 1. X配置。
下面是使用扩展WebSecurityConfigurerAdapter的类编写的方法。注意:preAuthFilter()、accessDeniedFilter()和forbiddenEntryPoint()是在同一个类中定义的方法
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.antMatcher("/**").exceptionHandling().authenticationEntryPoint(forbiddenEntryPoint())
.and().authorizeRequests().antMatchers(
"/**/index*",
"/**/logout/",
"/**/logoutApp",
"/rest/getversion*",
"/rest/dologin/*",
"/rest/menu*",
"/rest/getScript*",
"/rest/**").permitAll().antMatchers("/rest/saveTodo*").hasRole("ADMIN")
.antMatchers("/**").denyAll()
.and()
.addFilterAt(preAuthFilter(), AbstractPreAuthenticatedProcessingFilter.class)
.exceptionHandling().accessDeniedHandler(accessDeniedHandler())
.and().csrf().disable()
.headers().disable();
}
@Override
public void configure(WebSecurity web) {
web
.ignoring()
.antMatchers("/**/*.png*","/**/*.js*","/**/*.jpg*","/**/*.svg*","/**/*.ico*",
"/**/*.css*","/**/login*","/**/*.woff*","/**/*.ttf*","/**/*.eot*");
}
我试着把上面的代码改成下面的代码:
@Bean
public SecurityFilterChain springFilterChain(HttpSecurity http) throws Exception {
return http.authorizeHttpRequests(req -> req.requestMatchers(
new AntPathRequestMatcher(new AntPathRequestMatcher("/**"))))
.exceptionHandling(request -> request.authenticationEntryPoint(forbiddenEntryPoint()))
.authorizeHttpRequests(request -> request
.requestMatchers(
new AntPathRequestMatcher("/**/index*"),
new AntPathRequestMatcher("/**/logout/"),
new AntPathRequestMatcher("/**/logoutApp"),
new AntPathRequestMatcher("/rest/getversion*"),
new AntPathRequestMatcher("/rest/dologin/*"),
new AntPathRequestMatcher("/rest/menu*"),
new AntPathRequestMatcher("/rest/getScript*"),
new AntPathRequestMatcher("/rest/**"),
new AntPathRequestMatcher("/waveinventoryservice/**"))
.permitAll()
.requestMatchers(new AntPathRequestMatcher("/rest/saveTodo*")).hasRole("ADMIN")
.requestMatchers(new AntPathRequestMatcher("/**"))
.denyAll())
.addFilterAt(preAuthFilter(), AbstractPreAuthenticatedProcessingFilter.class)
.exceptionHandling(handler -> handler.accessDeniedHandler(accessDeniedHandler()))
.csrf(CsrfConfigurer::disable).headers(HeadersConfigurer::disable).build();
}
@Bean
public WebSecurityCustomizer webSecurityCustomizer() {
return (web) -> web.ignoring().requestMatchers(new AntPathRequestMatcher("/**/*.png*"),
new AntPathRequestMatcher("/**/*.js*"),
new AntPathRequestMatcher("/**/*.jpg*"),
new AntPathRequestMatcher("/**/*.svg*"),
new AntPathRequestMatcher("/**/*.ico*"),
new AntPathRequestMatcher("/**/*.css*"),
new AntPathRequestMatcher("/**/login*"),
new AntPathRequestMatcher("/**/*.woff*"),
new AntPathRequestMatcher("/**/*.ttf*"),
new AntPathRequestMatcher("/**/*.eot*"));
}
这段代码似乎不起作用,因为我在调用方法时在控制器和过滤器中得到null:SecurityContextHolder.getContext().getAuthentication()
,即使在一个API中使用URL“rest/dologin/{userId}”设置它之后也是如此
以前用旧的配置它是工作,但现在不是,可以有人请尝试帮助我在改变这个配置?
3条答案
按热度按时间rqqzpn5f1#
我在Spring Boot 3做了安全配置,也许能帮到你
vdgimpew2#
Spring Security更新了。
WebSecurityConfigurerAdapter
不再支持。你可以检查here。这可以帮助你:wi3ka0sx3#
根据您的信息,在迁移到
3.1.2
版本之前,所有工作都很好,SecurityContextHolder.getContext().getAuthentication()
没有返回null
。问题出在最新的配置中。同样根据
2.6.6
的最旧配置,3.1.2
配置应该是这样的: