This is a quick-start base for java projects with Spring Boot, MongoDB and configured JWT security.
- Download this base
- Start the MongoDB service/daemon in your system
- Run project by
Application.class
or bymvn clean install
,java -jar target/*.jar
, or bymvn spring-boot:run
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.