upgrade spring, add javadoc, formatting
This commit is contained in:
@@ -21,6 +21,7 @@
|
||||
<dependency>
|
||||
<groupId>com.nimbusds</groupId>
|
||||
<artifactId>nimbus-jose-jwt</artifactId>
|
||||
<version>9.21</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
|
||||
@@ -59,7 +59,7 @@ public class JwtKeyManager {
|
||||
/**
|
||||
* Gets the jwk set.
|
||||
*
|
||||
* @param name the name
|
||||
* @param name the name
|
||||
* @param createDefault the create default
|
||||
* @return the jwk set
|
||||
*/
|
||||
@@ -91,14 +91,14 @@ public class JwtKeyManager {
|
||||
/**
|
||||
* Gets the latest.
|
||||
*
|
||||
* @param name the name
|
||||
* @param name the name
|
||||
* @param createDefault the create default
|
||||
* @return the latest
|
||||
*/
|
||||
@Transactional
|
||||
public JwtKey getLatest(String name, boolean createDefault) {
|
||||
List<JwtKey> jwtKeys = Lists.newArrayList(jwtKeyRepository.findAll(qJwtKey.name.eq(name),
|
||||
PageRequest.of(0, 1, Sort.by(Order.desc("created")))));
|
||||
List<JwtKey> jwtKeys = Lists.newArrayList(
|
||||
jwtKeyRepository.findAll(qJwtKey.name.eq(name), PageRequest.of(0, 1, Sort.by(Order.desc("created")))));
|
||||
|
||||
JwtKey jwtKey = null;
|
||||
|
||||
@@ -107,8 +107,8 @@ public class JwtKeyManager {
|
||||
}
|
||||
|
||||
if (jwtKey != null) {
|
||||
if (jwtKey.getLifetime() == -1L || jwtKey.getExpires()
|
||||
.isAfter(Instant.now().plus(jwtKey.getLifetime(), ChronoUnit.SECONDS))) {
|
||||
if (jwtKey.getLifetime() == -1L
|
||||
|| jwtKey.getExpires().isAfter(Instant.now().plus(jwtKey.getLifetime(), ChronoUnit.SECONDS))) {
|
||||
return jwtKey;
|
||||
}
|
||||
|
||||
@@ -143,16 +143,17 @@ public class JwtKeyManager {
|
||||
/**
|
||||
* Gets the by created.
|
||||
*
|
||||
* @param name the name
|
||||
* @param name the name
|
||||
* @param created the created
|
||||
* @return the by created
|
||||
*/
|
||||
@Transactional
|
||||
public JwtKey getByCreated(String name, Instant created) {
|
||||
return jwtKeyRepository
|
||||
.findOne(qJwtKey.name.eq(name)
|
||||
.and(qJwtKey.created.before(created)
|
||||
.and(qJwtKey.lifetime.eq(-1L).or(qJwtKey.expires.after(created)))))
|
||||
.findOne(
|
||||
qJwtKey.name.eq(name)
|
||||
.and(qJwtKey.created.before(created)
|
||||
.and(qJwtKey.lifetime.eq(-1L).or(qJwtKey.expires.after(created)))))
|
||||
.orElse(null);
|
||||
}
|
||||
|
||||
@@ -171,8 +172,8 @@ public class JwtKeyManager {
|
||||
try {
|
||||
KeyType keyType = KeyType.parse(jwtKey.getKeyType());
|
||||
if (KeyType.EC.equals(keyType)) {
|
||||
jwk = new ECKeyGenerator(Curve.parse(jwtKey.getKeyParameter()))
|
||||
.keyUse(KeyUse.parse(jwtKey.getKeyUse())).keyID(keyID).generate();
|
||||
jwk = new ECKeyGenerator(Curve.parse(jwtKey.getKeyParameter())).keyUse(KeyUse.parse(jwtKey.getKeyUse()))
|
||||
.keyID(keyID).generate();
|
||||
} else if (KeyType.OCT.equals(keyType)) {
|
||||
jwk = new OctetKeyPairGenerator(Curve.parse(jwtKey.getKeyParameter()))
|
||||
.keyUse(KeyUse.parse(jwtKey.getKeyUse())).keyID(keyID).generate();
|
||||
@@ -236,7 +237,7 @@ public class JwtKeyManager {
|
||||
/**
|
||||
* Gets the jws algorithm.
|
||||
*
|
||||
* @param name the name
|
||||
* @param name the name
|
||||
* @param createDefault the create default
|
||||
* @return the jws algorithm
|
||||
*/
|
||||
|
||||
@@ -39,15 +39,14 @@ public class JwtUserManager {
|
||||
/**
|
||||
* Creates the signed jwt.
|
||||
*
|
||||
* @param userId the user id
|
||||
* @param data the data
|
||||
* @param issuer the issuer
|
||||
* @param userId the user id
|
||||
* @param data the data
|
||||
* @param issuer the issuer
|
||||
* @param lifetime the lifetime
|
||||
* @return the signed JWT
|
||||
* @throws JOSEException the JOSE exception
|
||||
*/
|
||||
public SignedJWT createSignedJwt(Long userId, Object data, String issuer, Long lifetime)
|
||||
throws JOSEException {
|
||||
public SignedJWT createSignedJwt(Long userId, Object data, String issuer, Long lifetime) throws JOSEException {
|
||||
User user = userManager.get(userId);
|
||||
|
||||
if (user == null) {
|
||||
@@ -61,8 +60,7 @@ public class JwtUserManager {
|
||||
claimsSetBuilder.issueTime(new Date());
|
||||
|
||||
if (lifetime != null && lifetime > 0) {
|
||||
claimsSetBuilder
|
||||
.expirationTime(Date.from(Instant.now().plus(lifetime, ChronoUnit.SECONDS)));
|
||||
claimsSetBuilder.expirationTime(Date.from(Instant.now().plus(lifetime, ChronoUnit.SECONDS)));
|
||||
}
|
||||
|
||||
if (data != null) {
|
||||
@@ -71,8 +69,7 @@ public class JwtUserManager {
|
||||
|
||||
JwtKey jwtKey = jwtKeyManager.getLatest(JWT_USER_DATA_KEY_NAME, true);
|
||||
|
||||
JWSHeader.Builder headerBuilder = new JWSHeader.Builder(
|
||||
jwtKeyManager.getJwsAlgorithm(jwtKey));
|
||||
JWSHeader.Builder headerBuilder = new JWSHeader.Builder(jwtKeyManager.getJwsAlgorithm(jwtKey));
|
||||
headerBuilder.keyID(jwtKey.getKeyID());
|
||||
headerBuilder.type(JOSEObjectType.JWT);
|
||||
|
||||
@@ -86,7 +83,7 @@ public class JwtUserManager {
|
||||
*
|
||||
* @param jwt the jwt
|
||||
* @return true, if successful
|
||||
* @throws JOSEException the JOSE exception
|
||||
* @throws JOSEException the JOSE exception
|
||||
* @throws ParseException the parse exception
|
||||
*/
|
||||
public boolean verify(SignedJWT jwt) throws JOSEException, ParseException {
|
||||
|
||||
@@ -50,17 +50,13 @@ public class JwtUserDataController extends BaseController {
|
||||
public String createSignedJwtAuth(HttpServletRequest request) {
|
||||
String issuer = jwtUserIssuer;
|
||||
if (!StringUtils.hasText(issuer)) {
|
||||
issuer = request.getScheme()
|
||||
+ "://"
|
||||
+ request.getServerName();
|
||||
issuer = request.getScheme() + "://" + request.getServerName();
|
||||
if (request.getServerPort() != 443 && request.getServerPort() != 80) {
|
||||
issuer += ":"
|
||||
+ request.getServerPort();
|
||||
issuer += ":" + request.getServerPort();
|
||||
}
|
||||
}
|
||||
try {
|
||||
return jwtUserDataManager.createSignedJwt(getCurrentUserId(), null, issuer, 120L)
|
||||
.serialize();
|
||||
return jwtUserDataManager.createSignedJwt(getCurrentUserId(), null, issuer, 120L).serialize();
|
||||
} catch (JOSEException e) {
|
||||
e.printStackTrace();
|
||||
throw new EntityResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
@@ -70,7 +66,7 @@ public class JwtUserDataController extends BaseController {
|
||||
/**
|
||||
* Creates the signed jwt.
|
||||
*
|
||||
* @param data the data
|
||||
* @param data the data
|
||||
* @param request the request
|
||||
* @return the string
|
||||
*/
|
||||
@@ -79,17 +75,13 @@ public class JwtUserDataController extends BaseController {
|
||||
public String createSignedJwt(@RequestBody Object data, HttpServletRequest request) {
|
||||
String issuer = jwtUserIssuer;
|
||||
if (!StringUtils.hasText(issuer)) {
|
||||
issuer = request.getScheme()
|
||||
+ "://"
|
||||
+ request.getServerName();
|
||||
issuer = request.getScheme() + "://" + request.getServerName();
|
||||
if (request.getServerPort() != 443 && request.getServerPort() != 80) {
|
||||
issuer += ":"
|
||||
+ request.getServerPort();
|
||||
issuer += ":" + request.getServerPort();
|
||||
}
|
||||
}
|
||||
try {
|
||||
return jwtUserDataManager.createSignedJwt(getCurrentUserId(), data, issuer, -1L)
|
||||
.serialize();
|
||||
return jwtUserDataManager.createSignedJwt(getCurrentUserId(), data, issuer, -1L).serialize();
|
||||
} catch (JOSEException e) {
|
||||
e.printStackTrace();
|
||||
throw new EntityResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
@@ -100,13 +92,12 @@ public class JwtUserDataController extends BaseController {
|
||||
* Verfiy.
|
||||
*
|
||||
* @param serialized the serialized
|
||||
* @param request the request
|
||||
* @param response the response
|
||||
* @param request the request
|
||||
* @param response the response
|
||||
* @return the object
|
||||
*/
|
||||
@PostMapping("verify")
|
||||
public Object verfiy(@RequestBody String serialized, HttpServletRequest request,
|
||||
HttpServletResponse response) {
|
||||
public Object verfiy(@RequestBody String serialized, HttpServletRequest request, HttpServletResponse response) {
|
||||
try {
|
||||
SignedJWT signedJwt = SignedJWT.parse(serialized);
|
||||
if (jwtUserDataManager.verify(signedJwt)) {
|
||||
|
||||
@@ -13,6 +13,5 @@ import de.bstly.we.jwt.model.JwtKey;
|
||||
* The Interface JwtKeyRepository.
|
||||
*/
|
||||
@Repository
|
||||
public interface JwtKeyRepository
|
||||
extends JpaRepository<JwtKey, Long>, QuerydslPredicateExecutor<JwtKey> {
|
||||
public interface JwtKeyRepository extends JpaRepository<JwtKey, Long>, QuerydslPredicateExecutor<JwtKey> {
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user