【问题标题】:Securing REST API in Grails and Spring Security在 Grails 和 Spring Security 中保护 REST API
【发布时间】:2013-01-26 23:16:56
【问题描述】:

我已将我的 Grails 应用程序拆分为两个应用程序 - 一个面向客户的 Web 应用程序和一个托管 REST api 的单独应用程序。我这样做是因为我正在构建一个 iOS 应用程序来配合我的网络应用程序。我的应用程序使用 Spring Security,我想保护 REST api。我惊讶地发现关于正确方法的信息很少。我应该使用 Spring Security 实现 oauth,从而使我的 API 应用程序成为 oauth 提供者吗?

任何建议都会很棒。

【问题讨论】:

标签: rest grails


【解决方案1】:

是的,我只是为另一个应用程序这样做。您必须告诉 spring security 在访问 REST URL 时采取不同的行为。

将此添加到您的config.groovy

现在您将拥有应用程序的两个部分,它们通过以下方式进行身份验证

a) URL 中带有 /api 的任何内容(假设您就是这样设置 REST),获取基本身份验证

b) 其他一切,通过登录页面。

// making the application more secured by intercepting all URLs 
grails.plugins.springsecurity.useBasicAuth = true
grails.plugins.springsecurity.basic.realmName = " REST API realm"
grails.plugins.springsecurity.securityConfigType = SecurityConfigType.InterceptUrlMap




//Exclude normal controllers from basic auth filter. Just the JSON API is included
grails.plugins.springsecurity.filterChain.chainMap = [
'/api/**': 'JOINED_FILTERS,-exceptionTranslationFilter',
'/**': 'JOINED_FILTERS,-basicAuthenticationFilter,-basicExceptionTranslationFilter'
]

【讨论】:

    【解决方案2】:

    在过去的几周里,我一直在开发一个插件,该插件完全涵盖了您想要做的事情:

    http://grails.org/plugin/spring-security-rest

    看看它,如果你有任何问题,请告诉我。

    【讨论】:

    • 虽然理论上可以回答这个问题,it would be preferable 在这里包含答案的基本部分,并提供链接以供参考。
    猜你喜欢
    • 2014-12-06
    • 2013-11-29
    • 2014-02-21
    • 2015-12-09
    • 2015-03-21
    • 2014-11-03
    • 2014-07-29
    • 2018-08-11
    • 2013-11-29
    相关资源
    最近更新 更多