我的应用程序使用基本身份验证进行spring-rest-rest访问(在“ / api / **”下的所有内容),并使用面向用户的API的UserDetailsService进行JWT身份验证(在本示例中,在“ / component”下的所有内容)。在升级到Spring Boot 2之前,我的工作如下:
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter
{
private UserDetailsService userDetailsService;
private BCryptPasswordEncoder passwordEncoder;
public SecurityConfig( UserDetailsService userDetailsService, BCryptPasswordEncoder passwordEncoder )
{
this.userDetailsService = userDetailsService;
this.passwordEncoder = passwordEncoder;
}
@Override
public void configure( AuthenticationManagerBuilder authenticationManagerBuilder ) throws Exception
{
authenticationManagerBuilder
.userDetailsService( userDetailsService ).passwordEncoder( passwordEncoder )
.and()
.inMemoryAuthentication()
.withUser( "rest" ).password( "something" ).authorities( "REST" ).roles( "REST" );
}
@Override
protected void configure( HttpSecurity http ) throws Exception
{
http
.cors()
.and()
.csrf().disable()
.authorizeRequests()
.antMatchers( HttpMethod.POST, SecurityProperties.SIGN_UP_URL, SecurityProperties.LOGIN_URL ).permitAll()
.antMatchers( HttpMethod.GET, "/component1/**", "/component2/**", "/component3/**", "/component4/**" ).authenticated()
.antMatchers( HttpMethod.POST, "/component1/**", "/component2/**", "/component3/**", "/component4/**" ).authenticated()
.antMatchers( HttpMethod.GET, "/api/**" ).hasRole( "REST" )
.antMatchers( HttpMethod.OPTIONS, "/api/**" ).hasRole( "REST" )
.antMatchers( HttpMethod.PUT, "/api/**" ).hasRole( "REST" )
.antMatchers( HttpMethod.PATCH, "/api/**" ).hasRole( "REST" )
.antMatchers( HttpMethod.DELETE, "/api/**" ).hasRole( "REST" )
.anyRequest().authenticated()
.and()
.httpBasic()
.and()
.addFilter( new JWTAuthenticationFilter( authenticationManager() ) )
.addFilter( new JWTAuthorizationFilter( authenticationManager() ) )
.addFilter( new BasicAuthenticationFilter( authenticationManager() ) );
}
}
对于CORS,有一个单独的配置,我现在不关心,因为它以前运行良好,现在也不是任何麻烦的根源。
因此,在升级到Spring Boot Security 2时,我尝试将每种安全性中的所有内容放入2个不同的WebSecurityConfigurerAdapter中,这将使它们中的一种起作用,无论哪个@Order(1)编号都较小。我在某种程度上遵循了此处的示例:https : //github.com/spring-projects/spring-boot/wiki/Spring-Boot-Security-2.0
我想知道现在是否应该使用AuthenticationManagerBuilder摆脱此配置覆盖?还是将它们合并为一个文件?我不确定该怎么做,有人对此有经验吗?
我今天回到这个。我放弃了整个多个WebSecurityConfigurerAdapter的东西,只是回到使用其中一个。使用过的Spring Security日志记录级别= DEBUG显示了一些容易修复的问题,因为我所做的事情不再支持Spring 5。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句