> For the complete documentation index, see [llms.txt](https://docs.mosip.io/1.2.0/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.mosip.io/1.2.0/mosip-docs-1.1.5/contribute/coding-standards/auth-springboot-user-guide.md).

# Auth SpringBoot User Guide

This document lists out the instructions on how to use the [Auth Adapter](/1.2.0/mosip-docs-1.1.5/modules/kernel/auth-adapter.md) in a Spring Boot application.

* Step 1: [Inject required libraries](#Inject-required-libraries)
* Step 2: [Attach annotations to authorize endpoints](#Attach-annotations-to-authorize-endpoints)
* Step 3: [Use restTemplate for Http calls](#Use-restTemplate-for-Http-calls)

## Inject required libraries

* Add the Auth Adapter module to your project as a maven dependency

```xml
<dependency>
    <groupId>io.mosip.kernel</groupId>
    <artifactId>kernel-auth-adapter</artifactId>
    <version>Kernel Parent Version</version>
</dependency>
```

* Add ComponentScan annotation as shown below to your project. This is to create auth adapter bean.

```java
@SpringBootApplication
@ComponentScan(basePackages = "io.mosip.*")
```

## Attach annotations to authorize endpoints

To restrict access to your endpoints, you need to add the **@PreAuthorize** annotation. Look at the below example for reference.

```java
@PreAuthorize("hasAnyRole('DIVISION_ADMIN', 'SUPERVISOR', 'AGENT')")
@RequestMapping(value = "/api/reference", method = RequestMethod.GET)
```

There are few more methods available apart from hasAnyRole like hasRole. Look in to the [@PreAuthorize](https://docs.spring.io/spring-security/site/docs/3.0.x/reference/el-access.html) documentation for more details.

**Note:** Now we support only hasRole and hasAnyRole methods.

## Use restTemplate for Http calls

To make any kind of HTTP or HTTPS calls to a mosip's micro service that also injected the Auth Adapter, use the standard RestTemplate capabilities as shown below.

* Intially autowire the RestTemplate in the class where you are going to make an API call.

```java
@Autowired
private RestTemplate restTemplate;
```

* Now make the call using the autowired restTemplate as shown in the sample below:

```java
final String uri = "http://localhost:3001/api/location";
LocationDao response = restTemplate.getForObject(uri, LocationDao.class);
```

**Note:** Do not create a new instance of the RestTemplate instead use the autowired one.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.mosip.io/1.2.0/mosip-docs-1.1.5/contribute/coding-standards/auth-springboot-user-guide.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
