【问题标题】:EL1004E: Method call: Method isPermitted(java.lang.String) cannot be found on type MethodSecurityExpressionRootEL1004E: 方法调用:在 MethodSecurityExpressionRoot 类型上找不到方法 isPermitted(java.lang.String)
【发布时间】:2020-09-04 23:51:56
【问题描述】:

大家好,当我在 Intellij 上运行我的项目时,我遇到了以下问题,一切正常,但是当我使用 maven install 进行构建并运行我的项目时,出现了这个问题。

[无法评估表达式'isAuthenticated()

&& isPermitted('domain:read:*')'][1] [1]:https://i.stack.imgur.com/LkaY6.png

我的代码

 @PreAuthorize("isAuthenticated() && isPermitted('domain:read:*')")
    @GetMapping(produces = [(MediaType.APPLICATION_JSON_VALUE)])
    fun search(query: DomainQuery): ResponseEntity<ArpiaPage<DomainOutput>> {
        val retval = service.search(query)
        return ResponseEntity(
                retval.map { domain -> converter.convert(domain, DomainOutput::class.java) },
                HttpStatus.OK
        )
    }
    @Configuration
@EnableGlobalMethodSecurity(prePostEnabled=true)
class ShiroMethoSecurityConfig : GlobalMethodSecurityConfiguration() {
    override fun createExpressionHandler(): MethodSecurityExpressionHandler {
        return ShiroMethodSecurityExpressionHandler()
    }
}




   class ShiroMethodSecurityExpressionHandler : DefaultMethodSecurityExpressionHandler() {
    private val trustResolver = AuthenticationTrustResolverImpl()

    override fun createSecurityExpressionRoot(authentication: Authentication, invocation: MethodInvocation): MethodSecurityExpressionOperations {
        return ShiroMethodSecurityExpressionRoot(authentication).apply {
            setPermissionEvaluator(permissionEvaluator)
            setTrustResolver(trustResolver)
            setRoleHierarchy(roleHierarchy)
        }
    }
}

open class ShiroMethodSecurityExpressionRoot(authentication: Authentication) : SecurityExpressionRoot(authentication), MethodSecurityExpressionOperations {
    private var filterObject: Any? = null
    private var returnObject: Any? = null
    private var target: Any? = null

    private val LOG = LoggerFactory.getLogger(ShiroMethodSecurityExpressionRoot::class.java)

    override fun setFilterObject(filterObject: Any) {
        this.filterObject = filterObject
    }

    override fun getFilterObject(): Any? {
        return filterObject
    }

    override fun setReturnObject(returnObject: Any) {
        this.returnObject = returnObject
    }

    override fun getReturnObject(): Any? {
        return returnObject
    }

    /**
     * Sets the "this" property for use in expressions. Typically this will be the "this"
     * property of the `JoinPoint` representing the method invocation which is being
     * protected.
     *
     * @param target the target object on which the method in is being invoked.
     */
    fun setThis(target: Any) {
        this.target = target
    }

    override fun getThis(): Any? {
        return target
    }


    @Bean
    open fun isPermitted(permission: String?): Boolean {
        return true
    }


    @Bean
    fun isPermitted(vararg permissions: String): Boolean {
        return try {
            val permissionObjects = permissions.map { permission -> WildcardPermission(permission) }

            val user = this.principal as AuthAccount

            //true
            permissionObjects.all {
                user.permissions.any { permission ->

                    permission.implies(it)
                }
            }
        } catch (e: Exception) {
            e.printStackTrace()
            LOG.debug("", e)
            throw (e)
        }
    }


}

【问题讨论】:

  • 显而易见的原因是它使用了一个或多个 Spring 库的不同版本。你是如何运行这个项目的?依赖项是否编译到 jar/war 中——如果没有,它从哪里获取它们?
  • 感谢您的回复,将依赖项编译成 jar 并使用 java -jar 运行我的项目,我使用的是 spring boot 2.3.3
  • 此外,如果我运行命令 mvn spring-boot:run 一切正常,但我运行 mvn package 或 mvn install 然后 java -jar mybundle.jar 应用程序抛出错误

标签: java spring-boot kotlin


【解决方案1】:

尝试使用文本“and”代替逻辑运算符“&&”。我相信@PreAuthorize注解,你需要给文本组合多个检查

【讨论】:

  • 不幸的是不起作用,如果我在 intellij 中运行我的应用程序,则在创建包时会出现问题,但是当我使用以下命令创建包时 mvn clean install 并运行我的应用程序java -jar my_bundle.jar 立即出现错误
猜你喜欢
  • 2014-06-02
  • 2021-12-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-25
  • 1970-01-01
相关资源
最近更新 更多