grails - 如何在单元测试中针对Spring Security验证用户身份

标签 grails spring-security

我正在grails 2.0中使用Spring Security Core插件(1.2.7)

假设我的 Controller 具有使用@Secured批注的方法。

class ArticleController {
    def springSecurityService

    @Secured(['ROLE_PREMIUM_USER'])
    def listPremium() { 
        render 'premium content'
    }
}

在我的单元测试中,我想测试角色为“ROLE_PREMIUM_USER”的用户是否可以看到listPremium方法的内容。我怎样才能做到这一点?

我知道它应该开始如下:
@TestFor(ArticleController)
@Mock([SpringSecurityService])
class ArticleControllerTests {
    void testListPremium() {
    defineBeans {
        springSecurityService(SpringSecurityService)
    }
         //but how to login the user here in order to see premium content?

        controller.listPremium()
        assert response.text() == 'premium content'
    }
}

我不确定如何验证用于检查ROLE_PREMIUM_USER的用户或模拟操作。有什么帮助吗?

最佳答案

您可能可以使用

SpringSecurityUtils.reauthenticate username, null

关于grails - 如何在单元测试中针对Spring Security验证用户身份,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10507854/

相关文章:

grails - 如何编写Grails过滤器的集成测试

grails - 使用Gradle构建Grails应用

spring - 使用 Spring 运行 JUnit 测试时为空服务

java - Spring 安全: Unable to autowired DAO in the UserDetailsService class

java - hibernate/GORM : collection was not processed by flush()

grails - 如何使用 grails 分类插件向分类单元添加层次结构?

java - Grails 多对多和一对多冲突

java - Spring Security 记住我身份验证

mysql - Spring 启动安全 : Encoded password does not look like BCrypt

spring - Grails Ldap记住我IllegalArgumentException