应该为外部类指定spring启动类型参数

6jjcrrmo  于 2021-09-29  发布在  Java
关注(0)|答案(1)|浏览(299)

我通过创建一个配置类在我的应用程序中实现oauth2安全性,但我不知道我在哪里犯了错误,我得到了以下类型的编译时错误

Type arguments should be specified for an outer class 'ExpressionUrlAuthorizationConfigurer'. Use full class name to specify them

securityconfig.kt

package com.main.serviceproduct.config

import org.springframework.context.annotation.Configuration
import org.springframework.security.config.Customizer
import org.springframework.security.config.annotation.web.builders.HttpSecurity
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter
import org.springframework.security.config.annotation.web.configurers.ExpressionUrlAuthorizationConfigurer.ExpressionInterceptUrlRegistry
import org.springframework.security.config.annotation.web.configurers.oauth2.server.resource.OAuth2ResourceServerConfigurer

@Configuration
@EnableWebSecurity
class SecurityConfig : WebSecurityConfigurerAdapter() {
    @Throws(Exception::class)
    override fun configure(security: HttpSecurity){
        security.authorizeRequests(Customizer { authorize: ExpressionInterceptUrlRegistry ->
            authorize.anyRequest().authenticated()
        })
            .oauth2ResourceServer{obj: OAuth2ResourceServerConfigurer<HttpSecurity?>->obj.jwt()}
    }

}

security.authorizeRequests(Customizer { authorize: ExpressionInterceptUrlRegistry ->
            authorize.anyRequest().authenticated()

在这一点上,我在上面的一行上得到了错误授权:expressionIntercepturRegistry

a11xaf1n

a11xaf1n1#

因为 ExpressionInterceptUrlRegistry 是一个内部(非静态)类,因此它需要外部类的类型信息 ExpressionUrlAuthorizationConfigurer<T> ,这实际上是一个 HttpSecurity .
以下方法应该有效:

@Configuration
@EnableWebSecurity
open class SecurityConfig : WebSecurityConfigurerAdapter() {
    @Throws(Exception::class)
    override fun configure(security: HttpSecurity){
        security.authorizeRequests(Customizer { authorize: ExpressionUrlAuthorizationConfigurer<HttpSecurity>.ExpressionInterceptUrlRegistry ->
            authorize.anyRequest().authenticated()
        })
            .oauth2ResourceServer{obj: OAuth2ResourceServerConfigurer<HttpSecurity?> ->obj.jwt()}
    }

}

相关问题