gpt4 book ai didi

java - 如何模拟 ApplicationContext context = getContext(); HttpSecurity 以防止以下测试用例中的 nullPointer 异常

转载 作者:行者123 更新时间:2023-12-05 05:47:42 24 4
gpt4 key购买 nike

springboot版本从2.5.6升级到2.6.3

升级后为配置类编写的测试用例抛出 nullpointerException。

HttpSecurity.authorizeRequests() 在 configure() 方法中抛出 NullPointerException。

在最新版本中,ExpressionUrlAuthorizationConfigurer(ApplicationContext 上下文)的实现已更改,它试图从 ApplicationContext 中获取 getBeanNamesForType,这导致了 NullpointerException(在 HttpSecurity-> authorizeRequests 中调用的 getContext 方法在旧版本和最新版本中都返回 null)

测试用例

    @Mock
AuthenticationManagerBuilder amb;
@Mock
ObjectPostProcessor<Object> opp;
@Mock
Map<Class<? extends Object>, Object> sharedObjects;


@Test
public void configureTrue() throws Exception{
HttpSecurity http=new HttpSecurity(opp, amb, sharedObjects);
SecurityConfig securityConfig=new SecurityConfig();
securityConfig.requireHttps=true;
securityConfig.managementSecurityEnabled=true;
securityConfig.configure(http); //calls conigure method of configuration class(Failing here)
securityConfig.requireHttps=false;
securityConfig.managementSecurityEnabled=false;
assertNotNull(http);
}

App.java(有SecurityConfig.class的Main方法类)

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public static class SecurityConfig extends WebSecurityConfigurerAdapter {
@Value("${management.security.enabled}")
protected boolean managementSecurityEnabled;

@Value("${security.require_ssl}")
protected boolean requireHttps;

public boolean isManagementSecurityEnabled() {
return managementSecurityEnabled;
}

@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable();


if (isManagementSecurityEnabled()) {

// authorizeRequests() is the one throwing nullpointerException
http.antMatcher("/management/admin/v1/met/**").authorizeRequests().anyRequest()
.hasRole("ACTUATOR").and().httpBasic();

}
}
}

HttpSecurity.class


public ExpressionUrlAuthorizationConfigurer<HttpSecurity>.ExpressionInterceptUrlRegistry authorizeRequests()
throws Exception {
ApplicationContext context = getContext(); //getting null for context
return getOrApply(new ExpressionUrlAuthorizationConfigurer<>(context)).getRegistry();
}


ExpressionUrlAuthorizationConfigurer

/**
* Creates a new instance
* @see HttpSecurity#authorizeRequests()
*/
public ExpressionUrlAuthorizationConfigurer(ApplicationContext context) {

//Getting failed here(NullPointer Exception) since we are getting context as null
String[] grantedAuthorityDefaultsBeanNames = context.getBeanNamesForType(GrantedAuthorityDefaults.class);
if (grantedAuthorityDefaultsBeanNames.length == 1) {
GrantedAuthorityDefaults grantedAuthorityDefaults = context.getBean(grantedAuthorityDefaultsBeanNames[0],
GrantedAuthorityDefaults.class);
this.rolePrefix = grantedAuthorityDefaults.getRolePrefix();
}
else {
this.rolePrefix = "ROLE_";
}
this.REGISTRY = new ExpressionInterceptUrlRegistry(context);
}


最佳答案

以下代码有效:

private HashMap<Class<?>, Object> getSharedObjets(){
HashMap map = new HashMap<Class<?>, Object>();
GenericApplicationContext context= new GenericApplicationContext();
context.refresh();
map.put(ApplicationContext.class, context);
return map;
}

关于java - 如何模拟 ApplicationContext context = getContext(); HttpSecurity 以防止以下测试用例中的 nullPointer 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70940131/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com