我正在使用 Grails 3.2.4 和 chalice Spring 安全插件 核心与休息。当向 发出请求时用户#index , 我用
params.max = Math.min(max ?: 10, 100)
respond User.list(params), model: [userCount: User.count()]
响应类似于:
[
{
"id": 3,
"accountExpired": false,
"accountLocked": false,
"enabled": true,
"password": "$2a$10$fdWi7i48Kw5tnpzsjKMUMOQDx7nhglp9tRtDaJHTAi5qOTdIL0t3u",
"passwordExpired": false,
"username": "me"
},
{
"id": 4,
"accountExpired": false,
"accountLocked": false,
"enabled": true,
"password": "$2a$10$3uFrDjJ8AwMsdMbKhExece6cJtQ4DS2e1/jFMIdDHrmqgDGpBgkS2",
"passwordExpired": false,
"username": "master"
},
// ...
例如,如何自定义此响应并消除密码字段?
最佳答案
有几种方法可以实现这一点,但最简单的方法是注册一个类型为 JsonRenderer
的 bean。在 resources.groovy
如下:
import grails.rest.render.json.JsonRenderer
beans = {
userRenderer(JsonRenderer, User) {
excludes = ['password']
}
}
引用 https://docs.grails.org/latest/guide/webServices.html#renderers其他方式。
关于json - 使用 Grails Spring Security Plugin Core 和 REST 从用户响应中省略密码字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41841455/