MehmetAran / spring-boot-mongodb-jwt

Base for Spring Boot projects with MongoDB and JWT authentication

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Spring-Boot-MongoDB-JWT

Build Status

Spring Boot base for projects with MongoDB and JWT based security.


This is a quick-start base for java projects with Spring Boot, MongoDB and configured JWT security.

Running

  • Download this base
  • Start the MongoDB service/daemon in your system
  • Run project by Application.class or by mvn clean install, java -jar target/*.jar, or by mvn spring-boot:run

JWT security

Page http://localhost:8080/api/hello is secured. To access this page, you need to do the following:

  • POST request to http://localhost:8080/api/signup with body
  username: "user",
  password: "12345"
  • POST request to http://localhost:8080/api/auth, then take token from responce and use it in header to access secured page
  • GET request to http://localhost:8080/api/hello with header:
  x-auth-token: <your token here>

Security is based on AuthenticationTokenFilter:

@Override 
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) 
             throws IOException, ServletException { 
  HttpServletRequest httpRequest = (HttpServletRequest) request; 
  Authentication authentication = authenticationService.authenticate(httpRequest); 
  SecurityContextHolder.getContext().setAuthentication(authentication); 
  filterChain.doFilter(request, response); 
  SecurityContextHolder.getContext().setAuthentication(null); 
} 

And some services for Token creation and Token verification.

About

Base for Spring Boot projects with MongoDB and JWT authentication

License:Apache License 2.0


Languages

Language:Java 100.0%