Spring boot Kotlin中的导入批注中需要的名称

Spring boot Kotlin中的导入批注中需要的名称,spring-boot,kotlin,compiler-errors,annotations,Spring Boot,Kotlin,Compiler Errors,Annotations,我有一个用Kotlin实现的Spring Boot应用程序2.1.6。是一个RESTAPI,它希望oAuth 2带有keyClope。 我用Java编写了以下代码,可以编译: package com.talleres.paco.mako.config.security; import com.fasterxml.jackson.databind.ObjectMapper; import org.springframework.beans.factory.annotation.Autowired

我有一个用Kotlin实现的Spring Boot应用程序2.1.6。是一个RESTAPI,它希望oAuth 2带有keyClope。 我用Java编写了以下代码,可以编译:

package com.talleres.paco.mako.config.security;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.security.oauth2.resource.ResourceServerProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.oauth2.client.OAuth2RestTemplate;
import org.springframework.security.oauth2.client.resource.OAuth2ProtectedResourceDetails;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;
import org.springframework.web.cors.CorsConfigurationSource;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;

@Configuration
@EnableWebSecurity
@EnableResourceServer
@EnableGlobalMethodSecurity(prePostEnabled = true)
@ConditionalOnProperty(prefix = "rest.security", value = "enabled", havingValue = "true")
@Import({SecurityProperties.class})
public class SecurityConfigurer extends ResourceServerConfigurerAdapter {

  @Autowired
  private ResourceServerProperties resourceServerProperties;

  @Autowired
  private SecurityProperties securityProperties;

  @Override
  public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
    resources.resourceId(resourceServerProperties.getResourceId());
  }


  @Override
  public void configure(final HttpSecurity http) throws Exception {

    http.cors()
        .configurationSource(corsConfigurationSource())
        .and()
        .headers()
        .frameOptions()
        .disable()
        .and()
        .csrf()
        .disable()
        .authorizeRequests()
        .antMatchers(securityProperties.getApiMatcher())
        .authenticated();

  }

  @Bean
  public CorsConfigurationSource corsConfigurationSource() {
    UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
    if (null != securityProperties.getCorsConfiguration()) {
      source.registerCorsConfiguration("/**", securityProperties.getCorsConfiguration());
    }
    return source;
  }

  @Bean
  public JwtAccessTokenCustomizer jwtAccessTokenCustomizer(ObjectMapper mapper) {
    return new JwtAccessTokenCustomizer(mapper);
  }

  @Bean
  public OAuth2RestTemplate oauth2RestTemplate(OAuth2ProtectedResourceDetails details) {
    OAuth2RestTemplate oAuth2RestTemplate = new OAuth2RestTemplate(details);

    //Prepare by getting access token once
    oAuth2RestTemplate.getAccessToken();
    return oAuth2RestTemplate;
  }
}
当我转换为Kotlin时,我得到一个语法错误:

package com.talleres.paco.mako.config.security

import com.fasterxml.jackson.databind.ObjectMapper
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
import org.springframework.boot.autoconfigure.security.oauth2.resource.ResourceServerProperties
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.context.annotation.Import
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity
import org.springframework.security.config.annotation.web.builders.HttpSecurity
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity
import org.springframework.security.oauth2.client.OAuth2RestTemplate
import org.springframework.security.oauth2.client.resource.OAuth2ProtectedResourceDetails
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter
import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer
import org.springframework.web.cors.CorsConfigurationSource
import org.springframework.web.cors.UrlBasedCorsConfigurationSource

@Configuration
@EnableWebSecurity
@EnableResourceServer
@EnableGlobalMethodSecurity(prePostEnabled = true)
@ConditionalOnProperty(prefix = "rest.security", value = ["enabled"], havingValue = "true")
@Import({SecurityProperties.class})
class SecurityConfigurer : ResourceServerConfigurerAdapter() {
    @Autowired
    private val resourceServerProperties: ResourceServerProperties? = null
    @Autowired
    private val securityProperties: SecurityProperties? = null

    @Throws(Exception::class)
    override fun configure(resources: ResourceServerSecurityConfigurer) {
        resources.resourceId(resourceServerProperties!!.resourceId)
    }

    @Throws(Exception::class)
    override fun configure(http: HttpSecurity) {
        http.cors()
                .configurationSource(corsConfigurationSource())
                .and()
                .headers()
                .frameOptions()
                .disable()
                .and()
                .csrf()
                .disable()
                .authorizeRequests()
                .antMatchers(securityProperties!!.apiMatcher)
                .authenticated()
    }

    @Bean
    fun corsConfigurationSource(): CorsConfigurationSource {
        val source = UrlBasedCorsConfigurationSource()
        if (securityProperties?.corsConfiguration != null) {
            source.registerCorsConfiguration("/**", securityProperties.corsConfiguration);
        }
        return source
    }

    @Bean
    fun jwtAccessTokenCustomizer(mapper: ObjectMapper): JwtAccessTokenCustomizer {
        return JwtAccessTokenCustomizer(mapper)
    }

    @Bean
    fun oauth2RestTemplate(details: OAuth2ProtectedResourceDetails): OAuth2RestTemplate {
        val oAuth2RestTemplate = OAuth2RestTemplate(details)
        oAuth2RestTemplate.accessToken
        return oAuth2RestTemplate
    }
}
错误在注释导入的行中:

@Import({SecurityProperties.class})
我使用IntelliJ CE将代码从Java转换为Kotlin。信息是:

> Task :compileKotlin
e: D:\Workspaces\CleanArchitecture\mako\src\main\customized\kotlin\com\talleres\paco\mako\config\security\SecurityConfigurer.kt: (26, 34): Name expected

提前感谢。

我想将Java代码转换为Kotlin的工具并不总是有效的。在这种情况下,@Import是在Java中定义的,它需要一个类数组,您可以在Kotlin中使用它,方法是传递一个实际上只对注释的值字段有效的vararg KClass,否则您需要传递一个适当的数组。更多信息

换句话说,您需要将代码更改为:@ImportSecurityProperties::class

编辑:此问题似乎是多年前报告并修复的:。我还尝试在我的机器上使用Kotlin 1.3.41将Java代码转换为Kotlin,并且正确生成了@Import语句

然而,发生了一些有趣的事情。@ConditionalOnProperty行已转换为:

@ConditionalOnProperty(prefix = "rest.security", value = "enabled", havingValue = "true")

它不会编译,因为禁止以命名形式将单个元素分配给varargs。我很想知道这是否是一个回归,因为它在您的代码片段中看起来是正确的。

我想将Java代码转换为Kotlin的工具并不总是有效的。在这种情况下,@Import是在Java中定义的,它需要一个类数组,您可以在Kotlin中使用它,方法是传递一个实际上只对注释的值字段有效的vararg KClass,否则您需要传递一个适当的数组。更多信息

换句话说,您需要将代码更改为:@ImportSecurityProperties::class

编辑:此问题似乎是多年前报告并修复的:。我还尝试在我的机器上使用Kotlin 1.3.41将Java代码转换为Kotlin,并且正确生成了@Import语句

然而,发生了一些有趣的事情。@ConditionalOnProperty行已转换为:

@ConditionalOnProperty(prefix = "rest.security", value = "enabled", havingValue = "true")

它不会编译,因为禁止以命名形式将单个元素分配给varargs。我很想知道这是否是一个回归,因为它在您的代码片段中看起来是正确的。

谢谢,您的建议解决了这个问题。但现在我想,如果我删除这行,我也可以解决这个问题。我不明白为什么我们需要导入一个带有注释的类。谢谢,这个问题已经用你的建议解决了。但现在我想,如果我删除这行,我也可以解决这个问题。我不明白为什么我们需要导入带有注释的类。