I am working with JWT and it is necessary to handle the claims from a class, for this create the following AuthContextHolder.java
:
package com.payulatam.ppp4.secure.api.controllers;
import javax.servlet.http.HttpServletRequest;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import com.payulatam.ppp4.secure.api.model.commons.SecureApiResources;
import io.jsonwebtoken.Claims;
public class AuthContextHolder {
public Integer getWebUserId(){
return getIntegerClaims(SecureApiResources.WEB_USER_ID_PARAM);
}
public Integer getMerchantId(){
return getIntegerClaims(SecureApiResources.MERCHANT_ID_PARAM);
}
public Integer getDefaultAccountId(){
return getIntegerClaims(SecureApiResources.DEFAULT_ACCOUNT_ID_PARAM);
}
public Integer getIntegerClaims(String claimParameter){
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
final Claims claims = (Claims) request.getAttribute(SecureApiResources.CLAIMS_ATTR);
return (Integer) claims.get(claimParameter);
}
}
Perso, I wonder if this class can be improved through the use of a different pattern or approach.
Here is an example of the implementation of this class:
private AuthContextHolder authContextHolder = new AuthContextHolder();
@RequestMapping(value = SecureApiResources.Asobancaria.URL_ASOBANCARIA_MERCHANT_ACCOUNTS, method = RequestMethod.GET, produces = "application/json")
public @ResponseBody List<AccountAsobancariaBank> getAccountsAsobancaria() throws Exception {
List<AccountAsobancariaBank> asobancariaData = asobancariaService.getAccounts(authContextHolder.getMerchantId());
if ((asobancariaData.get(0) == null)) {
HttpErrors httpErrors = new HttpErrors(
SecureApiResources.ERROR_ACCOUNTS_NOT_FOUND);
throw new EntityNotFoundException(httpErrors);
}
return asobancariaData;
}
I appreciate your advice.