简体   繁体   English

Spring Boot 和 Spring Data MongoDB:在 ResponseBody 中隐藏字段

[英]Spring Boot and Spring Data MongoDB: hiding fields in a ResponseBody

I'm using Spring Boot together with the spring-boot-starter-data-mongodb package.我将 Spring Boot 与 spring-boot-starter-data-mongodb 包一起使用。

I have successfully created a method to return account information for my app:我已经成功创建了一个方法来返回我的应用程序的帐户信息:

public Account queryAccountInfo(String userId) {
    Query query = new Query();
    query.addCriteria(Criteria.where("userId").is(userId));
    return mongoTemplate.findOne(query, Account.class);
}

Now, this returns all the information in the Account class which is a custom class I have created:现在,这将返回 Account 类中的所有信息,这是我创建的自定义类:

    public class Account {

        public Account(){}


        @Id
        private String id;
        private String email;
        private String token; 
}

Now, Spring will return the entire Account object in a ResponseBody object so it will create a JSON similar to this:现在,Spring 将在 ResponseBody 对象中返回整个 Account 对象,因此它将创建一个类似于以下内容的 JSON:

{
  "id": "1234567890",
  "email": "Google@google.com",
  "token": "XXX-XXX-XXX"}

I don't want it to return the "token" as that is sensitive information.我不希望它返回“令牌”,因为这是敏感信息。 How can I get Spring to not return the "token" field but return everything else?我怎样才能让 Spring 不返回“令牌”字段而是返回其他所有内容?

I'm sure there is a Spring annotation that will allow me to do this but I cannot seem to find it: http://docs.spring.io/spring-data/commons/docs/current/api/org/springframework/data/annotation/package-frame.html我确定有一个 Spring 注释可以让我这样做,但我似乎找不到它: http : //docs.spring.io/spring-data/commons/docs/current/api/org/springframework/数据/注释/package-frame.html

方法应该是:

query.fields().exclude("token");

Your actual problem is the lack of separation of mongodb bean objects and objects used in your API.您的实际问题是缺乏 mongodb bean 对象和 API 中使用的对象的分离。 While this is not necessary during the PoC phase of your project, when you bump into issues like this, you should add a second set of beans used for communication and map between the objects used for spring-data-mongo.虽然在项目的 PoC 阶段这不是必需的,但是当您遇到这样的问题时,您应该添加第二组 bean,用于在用于 spring-data-mongo 的对象之间进行通信和映射。

Equivalente Mongo Query等价的Mongo 查询

db.account.aggregate({$project:{email:1}}) db.account.aggregate({$project:{email:1}})

Código Kotlin:科迪戈·科特林:

package com.integracion.controller
import com.integracion.modelo.Account

import org.springframework.data.mongodb.core.*
import org.springframework.data.mongodb.core.aggregation.Aggregation
import org.springframework.data.mongodb.core.aggregation.ProjectionOperation

import org.springframework.data.mongodb.core.query.Criteria
import org.springframework.data.mongodb.core.query.Query
import org.springframework.data.mongodb.core.query.Query.query

@RestController
@EnableReactiveMongoRepositories(basePackageClasses = arrayOf(AccountRepository::class))

class InicioController {
    @Autowired
    private lateinit var accountRepository: AccountRepository
    @Autowired
    private lateinit var query: MongoTemplate

@GetMapping("v2/filtro")
    //Query es la libreria import org.springframework.data.mongodb.core.query.Query.query
    fun filtro (q: Query): Document {
        /*
           // Equivalente en MongoDB //
              db.account.find({},{email:1})
           //Manera simple pero devulve los excluidos con campos vacios null ""
        q.fields().include("email").exclude("token");
        var consulta : List<Account> = query.find(q,Account::class.java)*/


// =) 

        /* //Equivalente en MongoDB//
        db.account.aggregate({$project:{email:1}}) */


        var p: ProjectionOperation = Aggregation.project("email")
        var a: Aggregation = Aggregation.newAggregation(p)
        var resultado: Document = query.aggregate(a,"account",Account::class.java).rawResults

        return resultado
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM