【问题标题】:Unable to secure flow step in Grails with Spring Security无法使用 Spring Security 保护 Grails 中的流程步骤
【发布时间】:2014-01-30 01:44:57
【问题描述】:

我正在使用 Grails 2.2.2、Spring WebFlow 2.0.0 和 Spring-Security-Core 2.0-RC2 插件。我想确保流程中的最后一步,但未能成功。

grails.plugin.springsecurity.interceptUrlMap = [
  '/myController/connect?execution=e*s3': ["isFullyAuthenticated()"]
]

当用户尝试进入流程的最后一步时,如果他们没有完全通过身份验证,我想将他们重定向到登录页面。这适用于我项目中的其他操作,但不适用于 webflows。

我发现一些文档表明 Spring Web Flows 和 Spring Security 可以一起使用:http://docs.spring.io/spring-webflow/docs/2.0.x/reference/html/ch07s04.html

有什么想法吗?这在 Grails 中可行吗?

【问题讨论】:

    标签: grails spring-security spring-webflow


    【解决方案1】:

    查询字符串在安全检查之前从 url 中被剥离,所以我认为你需要自己明确地进行检查。

    isFullyAuthenticated()SecurityExpressionRoot中的实现是

    public final boolean isFullyAuthenticated() {
        return !trustResolver.isAnonymous(authentication) && !trustResolver.isRememberMe(authentication);
    }
    

    因此您可以依赖注入 authenticationTrustResolver bean 并执行相同的检查,例如

    class MyController {
       def authenticationTrustResolver
       def springSecurityService
    
       def action() {
    
          if (params.execution == 'e*s3' && !isFullyAuthenticated()) {
             response.sendError 401
             return
          }
    
          ...
       }
    
       private boolean isFullyAuthenticated() {
          def authentication = springSecurityService.authentication
          !authenticationTrustResolver.isAnonymous(authentication) && !authenticationTrustResolver.isRememberMe(authentication)
       }
    }
    

    【讨论】:

    • 那么使用这种方法我可以将用户发送到登录页面,然后让他们重定向回流程中的这一步吗?
    【解决方案2】:

    或者,您可以执行以下操作:

    grails.plugin.springsecurity.interceptUrlMap = [
      '/myController/connect': ["(request.getParameter('execute')?:'').matches('^e.*?s3\$') ? fullyAuthenticated : permitAll"]
    ]
    

    这个想法是为表达式公开 HttpServletRequest,因此我们可以决定是返回fullyAuthenticated 还是返回permitAll。

    【讨论】:

    • 很酷的东西 - SpEL 为 Spring Security 增加了大量的功能
    • @RobWinch 谢谢,这增加了流程步骤的安全性,但是一旦用户进行身份验证,它会将他们重定向回流程的开头。有没有办法重定向到流程中的当前步骤?
    【解决方案3】:

    你不能只导入这个类并将这个带有 matchin 角色的注释添加到你的步骤方法中吗?

    import grails.plugins.springsecurity.Secured
    
    class MyClass() {
    
        @Secured(['ROLE_USER'])
        def lastStep() {
            // do things
        }
    
    }
    

    未通过身份验证的用户将被重定向到登录页面

    【讨论】:

    • 请注意,webflow 操作是闭包而不是方法。
    猜你喜欢
    • 2014-02-09
    • 2013-11-29
    • 1970-01-01
    • 2013-01-26
    • 2014-12-06
    • 1970-01-01
    • 2014-10-10
    • 1970-01-01
    • 2019-04-02
    相关资源
    最近更新 更多