Commit 378e977b authored by Jie Yuan's avatar Jie Yuan
Browse files

add api token validation time and get method for /api/auth/api-token

parent 41e35bae
......@@ -14,6 +14,7 @@ import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
......@@ -27,7 +28,9 @@ import io.jsonwebtoken.Jwts;
import javax.validation.Valid;
import java.net.URI;
import java.security.Principal;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.Iterator;
import java.util.OptionalDouble;
import java.util.Random;
......@@ -109,7 +112,7 @@ public class AuthController {
if ( currentUser.getName() == null ) {
return null;
}
System.out.print(currentUser.getName() + "....................**..\n");
//System.out.print(currentUser.getName() + "....................**..\n");
UserInfo tmp = this.userRepository.findByUsername(currentUser.getName());
Long tmpId = tmp.getId();
UserInfo user = new UserInfo();
......@@ -149,6 +152,7 @@ public class AuthController {
UserCreatedToken newtoken = new UserCreatedToken();
newtoken.setId(tmpId);
newtoken.setToken(jwt);
newtoken.setValidationDate(calendarToExpireDate());
tokenRepo.save(newtoken);
} else {
......@@ -158,8 +162,9 @@ public class AuthController {
.getBody();
String id = claims.getSubject();
userRepository.deleteById(Long.valueOf(id));
System.out.print(id + "ppppppppp\n");
// System.out.print(id + "ppppppppp\n");
atoken.setToken(jwt);
atoken.setValidationDate(calendarToExpireDate());
tokenRepo.saveAndFlush(atoken);
userRepository.flush();
}
......@@ -168,6 +173,31 @@ public class AuthController {
return ResponseEntity.ok(new JwtAuthenticationResponse(jwt));
}
@GetMapping("/api-token")
@PreAuthorize("hasAuthority('ADMIN') or hasAuthority('USER') ")
public ResponseEntity<UserCreatedToken> getAPIToken(Principal currentUser ) {
if ( currentUser.getName() == null ) {
return null;
}
//System.out.print(currentUser.getName() + "....................**..\n");
UserInfo tmp = this.userRepository.findByUsername(currentUser.getName());
Long uid = tmp.getId();
UserCreatedToken atoken = tokenRepo.findOne(uid);
if ( atoken == null ) {
return null;
}
return ResponseEntity.ok(atoken);
}
@PostMapping("/signup")
@PreAuthorize("hasAuthority('ADMIN')")
......@@ -215,4 +245,14 @@ public class AuthController {
}
return sb.toString();
}
private Date calendarToExpireDate() {
Calendar calendar = Calendar.getInstance();
int year=calendar.get(Calendar.YEAR);
calendar.set(Calendar.YEAR, year+10);
Date date = calendar.getTime();
return date;
}
}
package svmon.model;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
......@@ -26,5 +28,7 @@ public class UserCreatedToken {
@NotEmpty()
@Length(max=200)
private String token;
private Date validationDate;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment