作者:THOMAS KENDALL
轉載自公眾號:stackgc
微服務安全是架構的重要部分之一。具體來說,就是認證和授權模式。
微服務認證和授權處理方式有幾種選擇,但本文只介紹 JSON Web Token 的使用。
JSON Web Token
JSON Web Token(JWT)本質上是一個獨立的身份驗證令牌,可以包含使用者標識、使用者角色和許可權等資訊,以及您可以儲存任何其他資訊。任何人都可以輕鬆讀取和解析,並使用金鑰來驗證真實性。有關 JSON Web Token 的簡要介紹,請檢視此頁面(jwt.io/introductio…
微服務使用 JSON Web Token 的一個優點是,我們可以配置它以便包含使用者擁有的任何許可權。這意味著每個服務不需要與授權服務互動才能授權使用者。
JWT 的另外一個優點是它們是可序列化的,足夠短的長度使得它可放置在請求頭中。
工作原理
JWT 的工作流程相當簡單。第一次請求是一個帶有使用者名稱和密碼的無身份驗證端點的 POST。
認證成功後,響應將包含 JWT。之後所有的請求都附帶一個 HTTP 頭,其包含了 JWT 令牌:Authorization: xxxxx.yyyyy.zzzzz
。
所有服務間的請求都要通過該頭,以便其他服務可以應用授權。
開始編碼
我們需要做的第一件事是弄清楚如何生成 JWT。幸運的是,我們不是第一個踩坑的人,有幾個現成的 JWT 類庫。
我選擇了 Java JWT(github.com/jwtk/jjwt)。…
public class JsonWebTokenUtility {
private SignatureAlgorithm signatureAlgorithm;
private Key secretKey;
public JsonWebTokenUtility() {
// 這不是一個安全的實踐
// 為了簡化,我儲存了一個靜態的 key 在這裡
// 實際上,在微服務環境中,key 是由配置伺服器持有的
signatureAlgorithm = SignatureAlgorithm.HS512;
String encodedKey = "L7A/6zARSkK1j7Vd5SDD9pSSqZlqF7mAhiOgRbgv9Smce6tf4cJnvKOjtKPxNNnWQj+2lQEScm3XIUjhW+YVZg==";
secretKey = deserializeKey(encodedKey);
}
public String createJsonWebToken(AuthTokenDetailsDTO authTokenDetailsDTO) {
String token = Jwts.builder().setSubject(authTokenDetailsDTO.userId).claim("email", authTokenDetailsDTO.email)
.claim("roles", authTokenDetailsDTO.roleNames).setExpiration(authTokenDetailsDTO.expirationDate)
.signWith(getSignatureAlgorithm(), getSecretKey()).compact();
return token;
}
private Key deserializeKey(String encodedKey) {
byte[] decodedKey = Base64.getDecoder().decode(encodedKey);
Key key = new SecretKeySpec(decodedKey, getSignatureAlgorithm().getJcaName());
return key;
}
private Key getSecretKey() {
return secretKey;
}
public SignatureAlgorithm getSignatureAlgorithm() {
return signatureAlgorithm;
}
public AuthTokenDetailsDTO parseAndValidate(String token) {
AuthTokenDetailsDTO authTokenDetailsDTO = null;
try {
Claims claims = Jwts.parser().setSigningKey(getSecretKey()).parseClaimsJws(token).getBody();
String userId = claims.getSubject();
String email = (String) claims.get("email");
List roleNames = (List) claims.get("roles");
Date expirationDate = claims.getExpiration();
authTokenDetailsDTO = new AuthTokenDetailsDTO();
authTokenDetailsDTO.userId = userId;
authTokenDetailsDTO.email = email;
authTokenDetailsDTO.roleNames = roleNames;
authTokenDetailsDTO.expirationDate = expirationDate;
} catch (JwtException ex) {
System.out.println(ex);
}
return authTokenDetailsDTO;
}
private String serializeKey(Key key) {
String encodedKey = Base64.getEncoder().encodeToString(key.getEncoded());
return encodedKey;
}
}
複製程式碼
我們有了這個工具類,之後需要在每個微服務中配置 Spring Security。
為此,我們需要自定義一個驗證過濾器,如果存在請求頭,則讀取它。Spring 有一個認證過濾器 RequestHeaderAuthenticationFilter
,我們可以繼承它。
public class JsonWebTokenAuthenticationFilter extends RequestHeaderAuthenticationFilter {
public JsonWebTokenAuthenticationFilter() {
// Don't throw exceptions if the header is missing
this.setExceptionIfHeaderMissing(false);
// This is the request header it will look for
this.setPrincipalRequestHeader("Authorization");
}
@Override
@Autowired
public void setAuthenticationManager(AuthenticationManager authenticationManager) {
super.setAuthenticationManager(authenticationManager);
}
}
複製程式碼
此時,頭已經以 PreAuthenticatedAuthenticationToken
的形式轉換為 Spring Authentication 物件。
我們現在需要一個 AuthenticationProvider 用於讀取令牌,從而進行身份驗證,並將其轉換為我們自己自定義的 Authentication 物件。
public class JsonWebTokenAuthenticationProvider implements AuthenticationProvider {
private JsonWebTokenUtility tokenService = new JsonWebTokenUtility();
@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
Authentication authenticatedUser = null;
// Only process the PreAuthenticatedAuthenticationToken
if (authentication.getClass().isAssignableFrom(PreAuthenticatedAuthenticationToken.class)
&& authentication.getPrincipal() != null) {
String tokenHeader = (String) authentication.getPrincipal();
UserDetails userDetails = parseToken(tokenHeader);
if (userDetails != null) {
authenticatedUser = new JsonWebTokenAuthentication(userDetails, tokenHeader);
}
} else {
// It is already a JsonWebTokenAuthentication
authenticatedUser = authentication;
}
return authenticatedUser;
}
private UserDetails parseToken(String tokenHeader) {
UserDetails principal = null;
AuthTokenDetailsDTO authTokenDetails = tokenService.parseAndValidate(tokenHeader);
if (authTokenDetails != null) {
List<GrantedAuthority> authorities = authTokenDetails.roleNames.stream()
.map(roleName -> new SimpleGrantedAuthority(roleName)).collect(Collectors.toList());
principal = new User(authTokenDetails.email, "", authorities);
}
return principal;
}
@Override
public boolean supports(Class<?> authentication) {
return authentication.isAssignableFrom(PreAuthenticatedAuthenticationToken.class)
|| authentication.isAssignableFrom(JsonWebTokenAuthentication.class);
}
}
複製程式碼
有了這些元件,我們可以在 Spring Security 中使用 JWT 了。在進行服務間通訊時,我們需要傳遞 JWT。
我使用了一個 Feign 客戶端,把 JWT 作為引數。
@FeignClient("user-management-service")
public interface UserManagementServiceAPI {
@RequestMapping(value = "/authenticate", method = RequestMethod.POST)
AuthTokenDTO authenticateUser(@RequestBody AuthenticationDTO authenticationDTO);
@RequestMapping(method = RequestMethod.POST, value = "/roles")
RoleDTO createRole(@RequestHeader("Authorization") String authorizationToken, @RequestBody RoleDTO roleDTO);
@RequestMapping(method = RequestMethod.POST, value = "/users")
UserDTO createUser(@RequestHeader("Authorization") String authorizationToken, @RequestBody UserDTO userDTO);
@RequestMapping(method = RequestMethod.DELETE, value = "/roles/{id}")
void deleteRole(@RequestHeader("Authorization") String authorizationToken, @PathVariable("id") int id);
@RequestMapping(method = RequestMethod.DELETE, value = "/users/{id}")
void deleteUser(@RequestHeader("Authorization") String authorizationToken, @PathVariable("id") int id);
@RequestMapping(method = RequestMethod.GET, value = "/roles")
Collection<RoleDTO> findAllRoles(@RequestHeader("Authorization") String authorizationToken);
@RequestMapping(method = RequestMethod.GET, value = "/users")
Collection<UserDTO> findAllUsers(@RequestHeader("Authorization") String authorizationToken);
@RequestMapping(method = RequestMethod.GET, value = "/roles/{id}", produces = "application/json", consumes = "application/json")
RoleDTO findRoleById(@RequestHeader("Authorization") String authorizationToken, @PathVariable("id") int id);
@RequestMapping(method = RequestMethod.GET, value = "/users/{id}", produces = "application/json", consumes = "application/json")
UserDTO findUserById(@RequestHeader("Authorization") String authorizationToken, @PathVariable("id") int id);
@RequestMapping(method = RequestMethod.GET, value = "/users/{id}/roles")
Collection<RoleDTO> findUserRoles(@RequestHeader("Authorization") String authorizationToken,
@PathVariable("id") int id);
@RequestMapping(method = RequestMethod.PUT, value = "/roles/{id}")
void updateRole(@RequestHeader("Authorization") String authorizationToken, @PathVariable("id") int id,
@RequestBody RoleDTO roleDTO);
@RequestMapping(method = RequestMethod.PUT, value = "/users/{id}")
void updateUser(@RequestHeader("Authorization") String authorizationToken, @PathVariable("id") int id,
@RequestBody UserDTO userDTO);
}
複製程式碼
為了傳遞 JWT,我在控制器的 Spring Security 中獲取它:
private String getAuthorizationToken() {
String token = null;
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication != null && authentication.getClass().isAssignableFrom(JsonWebTokenAuthentication.class)) {
JsonWebTokenAuthentication jwtAuthentication = (JsonWebTokenAuthentication) authentication;
token = jwtAuthentication.getJsonWebToken();
}
return token;
}
複製程式碼
JWT 可以很好地適應分散式微服務環境,並提供了大量功能。如果您正想為下一個微服務專案設計一個安全架構,請考慮使用 JSON Web Token。