### Instruction:
Develop a Java application that analyzes a given text document and generates word frequency statistics. Store the results in a MongoDB collection and provide a RESTful API to query the statistics by word or top N frequent words. Integrate authentication and authorization using JWT tokens.

### Response:

To develop this Java application, we'll use the following technologies:

1. Spring Boot: To create a RESTful API and manage dependencies.
2. Maven: As a build tool for our project.
3. MongoDB: For storing word frequency statistics.
4. JWT: For authentication and authorization.

Here's a step-by-step guide to developing the application:

Step 1: Set up the project

Create a new Spring Boot project using https://start.spring.io/ or your favorite IDE with the following dependencies:

- Web
- Security
- MongoDB
- Lombok (optional)

Step 2: Configure MongoDB

Add the following properties in `application.properties` file:

```
spring.data.mongodb.uri=mongodb://localhost:27017/word_statistics_db
```

Replace the URI with your own MongoDB instance details if needed.

Step 3: Create WordFrequency model

Create a new class called `WordFrequency.java`:

```java
import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

@Data
@Document(collection = "word_frequencies")
public class WordFrequency {
    @Id
    private String id;
    private String word;
    private int frequency;
}
```

Step 4: Create UserRepository

Create a new interface called `WordFrequencyRepository.java`:

```java
import org.springframework.data.mongodb.repository.MongoRepository;

public interface WordFrequencyRepository extends MongoRepository<WordFrequency, String> {
    WordFrequency findByWord(String word);
}
```

Step 5: Implement text analysis service

Create a new class called `TextAnalysisService.java`:

```java
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Service
public class TextAnalysisService {

    @Autowired
    private WordFrequencyRepository wordFrequencyRepository;

    public void analyzeText(String text) {
        Map<String, Integer> wordFrequencyMap = new HashMap<>();

        Pattern pattern = Pattern.compile("\\w+");
        Matcher matcher = pattern.matcher(text.toLowerCase());

        while (matcher.find()) {
            String word = matcher.group();
            wordFrequencyMap.put(word, wordFrequencyMap.getOrDefault(word, 0) + 1);
        }

        for (Map.Entry<String, Integer> entry : wordFrequencyMap.entrySet()) {
            WordFrequency wordFrequency = wordFrequencyRepository.findByWord(entry.getKey());
            if (wordFrequency == null) {
                wordFrequency = new WordFrequency();
                wordFrequency.setWord(entry.getKey());
            }
            wordFrequency.setFrequency(wordFrequency.getFrequency() + entry.getValue());
            wordFrequencyRepository.save(wordFrequency);
        }
    }
}
```

Step 6: Implement JWT authentication and authorization

Follow this tutorial to implement JWT authentication and authorization in your Spring Boot application: https://auth0.com/blog/implementing-jwt-authentication-on-spring-boot/

Step 7: Create RESTful API endpoints

Create a new class called `TextAnalysisController.java`:

```java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api")
public class TextAnalysisController {

    @Autowired
    private TextAnalysisService textAnalysisService;

    @PostMapping("/analyze-text")
    @PreAuthorize("hasRole('USER')")
    public void analyzeText(@RequestBody String text) {
        textAnalysisService.analyzeText(text);
    }

    @GetMapping("/word-frequency/{word}")
    @PreAuthorize("hasRole('USER')")
    public WordFrequency getWordFrequency(@PathVariable String word) {
        return wordFrequencyRepository.findByWord(word);
    }

    @GetMapping("/top-words/{n}")
    @PreAuthorize("hasRole('USER')")
    public List<WordFrequency> getTopWords(@PathVariable int n) {
        return wordFrequencyRepository.findAll(Sort.by(Sort.Direction.DESC, "frequency")).stream().limit(n).collect(Collectors.toList());
    }
}
```

Now you have a Java application that analyzes text documents and generates word frequency statistics. The results are stored in a MongoDB collection, and the RESTful API allows querying the statistics by word or top N frequent words. JWT tokens are used for authentication and authorization.