我正在尝试为我的控制器编写测试,但测试环境无法加载给定的 stackTrace。
@PreAuthorize("hasAuthority('my.scope')")
@GetMapping(value = "/path", produces = APPLICATION_JSON_VALUE)
public ResponseEntity<Map<String, Set<String>>> getPath() {
return myService.pathFunction()
.map(ResponseEntity::ok);
}
Run Code Online (Sandbox Code Playgroud)
以下是我配置安全配置的方式
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true)
public class WebConfig extends WebSecurityConfigurerAdapter {
private static final String PRINCIPAL = "sub";
@Override
protected void configure(HttpSecurity http) throws Exception {
JwtAuthenticationConverter authenticationConverter =
new JwtAuthenticationConverter();
authenticationConverter.setJwtGrantedAuthoritiesConverter(...);
authenticationConverter.setPrincipalClaimName(PRINCIPAL);
http.csrf().disable()
.cors()
.and()
.authorizeRequests().antMatchers("/actuator/**").permitAll()
.anyRequest().authenticated()
.and()
.oauth2ResourceServer()
.jwt()
.jwtAuthenticationConverter(authenticationConverter);
}
}
Run Code Online (Sandbox Code Playgroud)
控制器按需要工作,但我的测试因此错误而失败。我没有使用任何客户 JwtDecoder
@WebMvcTest(controllers = Controller.class)
class ControllerTest {
@MockBean
private MyService myService;
@Autowired
private …Run Code Online (Sandbox Code Playgroud)