https://github.com/yisaer/springboot-rest-login
https://github.com/yisaer/springboot-rest-login
Last synced: 10 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/yisaer/springboot-rest-login
- Owner: Yisaer
- License: mit
- Created: 2017-09-16T13:14:13.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2017-10-10T06:51:27.000Z (over 8 years ago)
- Last Synced: 2025-01-30T00:27:03.490Z (12 months ago)
- Language: Java
- Size: 15.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Springboot-REST-Login
This is a Simple application by Springboot which show you how Springboot REST application to have User authorization.
## Data Layer
I use Mysql for data persistence to save the information for users , and use Redis for cache to control the authorization .
## How to use ?
1. fulfill your configuration in application.porperties
for example
```properties
#MySQL
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/login?useUnicode=true&autoReconnect=true&failOverReadOnly=false&zeroDateTimeBehavior=round&autoReconnect=true
spring.datasource.username=root
spring.datasource.password=123456
#Redis
spring.redis.host=127.0.0.1
#spring.redis.password=123
spring.redis.port=6379
```
2. send `username` and password to `POST /tokens/login` and you will get a token.
For each Http request you want to contorl , add `Authorization` in HTTP header with token value , and also you should add `@Authorization` to your REST handler and add `@CurrentUser` in Param.
Obviously `logout` is a handler with authorization control , so it is a good example to show how to use it.
```java
@RequestMapping(method = RequestMethod.DELETE)
@Authorization
public ResponseEntity logout(@CurrentUser User user ){
if(user == null ){
System.out.println("Here");
}
tokenManager.deleteToken(user.getId());
return new ResponseEntity(ResultModel.ok(),HttpStatus.OK);
}
```