Code Monkey home page Code Monkey logo

ieti---lab8's Introduction

2.2 Spring Boot Secure API

Goals

  • Implement a secure API using the JSON Web Token open standard (RFC 7519).
  • Consume the API from a React JS application.

Part 1: Implement the API

  1. Clone this repository. Compile and run the Spring Boot project.

  2. Verify that authentication works by issuing the following command from the console:

    curl -H "Content-Type: application/json" -X POST -d '{"username":"xyz","password":"password"}' http://localhost:8080/user/login
    

If you don't have the curl utility installed, try using the Postman application.

  1. Open the following url and try to understand the message you get from the server:

http://localhost:8080/api/test

Then try to locate the code fragment where the exception is thrown.

The Exception is a ''Missing or invalid Authorization header', because we did not send the authorization header with the get request in the browser

The @Bean annotation will register the filter in the api/* path, once the application is run

  1. Comment the line 13 @Bean on the SpringBootSecureApiApplication class. Re-run the project and go to the endpoint again:

http://localhost:8080/api/test

Why do you think it works now? Discuss your findings with your classmates.

The solution for this can be handled with a new get request using curl and sending the auth header

  1. Implement the logic to verify the user's credentials on the UserController and the UserServiceImpl classes.

  2. Add the TaskController and services implemented on the previous lab. Ensure the endpoints are exposed under the /api path.

  3. Verify that your implementation secures all the exposed methods under the /api path in your API.

  4. Add the following annotation on top of your @RequestMapping annotation on your REST controllers (to avoid the Cross-origin access restriction )

    @CrossOrigin(origins = "http://localhost:3000")
    

Part 2: Consume the API from ReactJS project

  1. Open the Task Planner App React JS project.

  2. Install the axios node package

     npm install axios --save
    
Implement an API call in the Login View
  1. Make a first request to the API to authenticate on the method componentDidMount():

           axios.post('http://localhost:8080/user/login', {
                 username: 'xyz',
                 password: 'password'
             })
                 .then(function (response) {
                     console.log(response.data);
                 })
                 .catch(function (error) {
                     console.log(error);
                 });
  2. Save the returned authentication token into the local storage.

  3. Implement the logic to validate if the user has a valid token. Then redirect to the Main View.

Make other calls to your API
  1. Create an instance of the Axios client that contains the token inside the Authorization header

        this.axios = axios.create({
                baseURL: 'http://localhost:8080/api/',
                timeout: 1000,
                headers: {'Authorization': 'Bearer ' + token}
            });
  2. Make a request to the API with the Axios client instance for retrieving the Tasks List.

  3. Create an additional call to Post new Tasks to the API.

ieti---lab8's People

Contributors

alejovasquero avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.