项目作者: xiaomalover

项目描述 :
Spring boot security jwt project.
高级语言: Java
项目地址: git://github.com/xiaomalover/spring-boot-security-jwt.git
创建时间: 2018-08-22T13:34:42Z
项目社区:https://github.com/xiaomalover/spring-boot-security-jwt

开源协议:MIT License

下载


[中文版]

  1. _ _ _ _ _ _ _
  2. ___ _ __ _ __(_)_ __ __ _| |__ ___ ___ | |_ (_)_ _| |_ ___| |_ __ _ _ __| |_ ___ _ __
  3. / __| '_ \| '__| | '_ \ / _` | '_ \ / _ \ / _ \| __| | \ \ /\ / / __| / __| __/ _` | '__| __/ _ \ '__|
  4. \__ \ |_) | | | | | | | (_| | |_) | (_) | (_) | |_ | |\ V V /| |_ \__ \ || (_| | | | || __/ |
  5. |___/ .__/|_| |_|_| |_|\__, |_.__/ \___/ \___/ \__| _/ | \_/\_/ \__| |___/\__\__,_|_| \__\___|_|
  6. |_| |___/ |__/

npm
Build Status
License MIT

A Springboot token-based security starter kit featuring AngularJS and Springboot (JSON Web Token)

If you’re looking for using Angular 4 for frontend implementation, please checkout angular-spring-starter, a fullstack starter kit featuring Angular 4, Router, Forms,
Http,
Spring boot,
Json Web Token

Authentication is the most common scenario for using JWT. Once the user is logged in, each subsequent request will include the JWT, allowing the user to access routes, services, and resources that are permitted with that token. Single Sign On is a feature that widely uses JWT nowadays, because of its small overhead and its ability to be easily used across different domains.

Auth0

Quick start

Make sure you have Maven and Java 1.7 or greater

  1. # clone our repo
  2. # --depth 1 removes all but one .git commit history
  3. git clone --depth 1 https://github.com/bfwg/springboot-jwt-starter.git
  4. # change directory to our repo
  5. cd springboot-jwt-starter
  6. # install the repo with mvn
  7. mvn install
  8. # start the server
  9. mvn spring-boot:run
  10. # the app will be running on port 8080
  11. # there are two built-in user accounts to demonstrate the differing levels of access to the endpoints:
  12. # - User - user:123
  13. # - Admin - admin:123

File Structure

  1. springboot-jwt-starter/
  2. ├──src/ * our source files
  3. ├──main
  4. ├──java.com.weison
  5. ├──config
  6. └──WebSecurityConfig.java * config file for filter, custom userSerivce etc.
  7. ├──model
  8. ├──Authority.java
  9. ├──UserTokenState.java * JWT model
  10. └──User.java * our main User model.
  11. ├──repository * repositories folder for accessing database
  12. └──UserRepository.java
  13. ├──rest * rest endpoint folder
  14. ├──AuthenticationController.java * auth related REST controller, refresh token endpoint etc.
  15. └──UserController.java * REST controller to handle User related requests
  16. ├──security * Security related folder(JWT, filters)
  17. ├──auth
  18. ├──JwtAuthenticationRequest.java * login request object, contains username and password
  19. ├──RestAuthenticationEntryPoint.java * handle auth exceptions, like invalid token etc.
  20. ├──TokenAuthenticationFilter.java * the JWT token filter, configured in WebSecurityConfig
  21. └──TokenBasedAuthentication.java * this is our custom Authentication class and it extends AbstractAuthenticationToken.
  22. └──TokenHelper.java * token helper class
  23. ├──service
  24. ├──impl
  25. ├──CustomUserDetailsService.java * custom UserDatilsService implementataion, tells formLogin() where to check username/password
  26. └──UserServiceImpl.java
  27. └──UserService.java
  28. └──Application.java * Application main enterance
  29. └──recources
  30. ├──static * static assets are served here(Angular and html templates)
  31. ├──application.yml * application variables are configured here
  32. └──import.sql * h2 database query(table creation)
  33. └──test * Junit test folder
  34. └──pom.xml * what maven uses to manage it's dependencies

Table of Contents

Configuration

  • WebSecurityConfig.java: The server-side authentication configurations.
  • application.yml: Application level properties i.e the token expire time, token secret etc. You can find a reference of all application properties here.
  • JWT token TTL: JWT Tokens are configured to expire after 10 minutes, you can get a new token by signing in again.
  • Using a different database: This Starter kit is using an embedded H2 database that is automatically configured by Spring Boot. If you want to connect to another database you have to specify the connection in the application.yml in the resource directory. Here is an example for a MySQL DB:
  1. spring:
  2. jpa:
  3. hibernate:
  4. # possible values: validate | update | create | create-drop
  5. ddl-auto: create-drop
  6. datasource:
  7. url: jdbc:mysql://localhost/myDatabase
  8. username: myUser
  9. password: myPassword
  10. driver-class-name: com.mysql.jdbc.Driver

Hint: For other databases like MySQL sequences don’t work for ID generation. So you have to change the GenerationType in the entity beans to ‘AUTO’ or ‘IDENTITY’.

JSON Web Token

JSON Web Tokens are an open, industry standard RFC 7519 method for representing claims securely between two parties.
for more info, checkout https://jwt.io/

Contributing

I’ll accept pretty much everything so feel free to open a Pull-Request

This project is inspried by


License

MIT