使用Spring Security在Grails App中进行未经授权的访问

时间:2016-08-17 13:00:05

标签: grails spring-security

应用信息:

  • Grails版本:3.1.8
  • Groovy版本:2.4.6
  • JVM版本:1.8.0_60
  • Spring Security:spring-security-core:3.1.1

我有一名学校管理员。这包含各种方法。 Admin用户可以访问此方法。

@Secured(['ROLE_ADMIN'])
def saveSchool(School newSchool) {
    def theSchool = schoolService.saveSchool(newSchool)

    if (theSchool) {
        render theSchool as JSON
    } else {
        render newSchool as JSON
    }
}

我试图使用AJAX保存学校,好像有任何错误消息我希望它们出现在同一个表单上。

<g:javascript>

    $('#insertSchool').submit(function () {

        $.ajax({
            type: 'POST',
            url: '<g:createLink controller="school" action="saveSchool"/>',
            data: $("#insertSchool").serialize(),
            success: function(theSchool) {
           '<g:createLink controller="school" view="saveSchool" model="theSchool"/>'
            }
        })

    });

</g:javascript>

数据一直存在于数据库中,我被重定向到saveSchool视图但是我得到了403.

1 个答案:

答案 0 :(得分:0)

我不明白为什么(或何时)你收到错误403,但我认为你需要重新组织你的行动和你的观点/ js。

@Secured(['ROLE_ADMIN'])
def save(School newSchool) {

    //First, validate the new school
    if(!newSchool.validate){
        ...
        //send a proper error code and send errors to the response
    }

    //save the school
    schoolService.saveSchool(newSchool)

    //render the school
    render newSchool as JSON
}

-

<g:javascript>

    $('#insertSchool').submit(function () {

        $.ajax({
            type: 'POST',
            url: '${g.createLink( controller:'school', action:'saveSchool')}',
            data: $("#insertSchool").serialize(),
            success: function(theSchool) {
                //You received the school as JSON, use JS to show it
                ...
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                //Handle errors
            }
        })

    });

</g:javascript>

我希望这会对你有所帮助。