小编Uts*_*v10的帖子

错误没有“org.springframework.security.oauth2.jwt.JwtDecoder”类型的合格bean可用

我正在尝试为我的控制器编写测试,但测试环境无法加载给定的 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)

java spring spring-security spring-test spring-boot

13
推荐指数
1
解决办法
1万
查看次数

标签 统计

java ×1

spring ×1

spring-boot ×1

spring-security ×1

spring-test ×1