javascript
带有Spring Cloud Microservices的JSON Web令牌
在Keyhole,我們已經發布了幾個有關微服務的博客 。 我們已經討論了微服務環境中使用的架構模式,例如服務發現和斷路器 。 我們甚至在平臺和工具上發布了博客,例如最近關于Service Fabric的博客 。
我們已經介紹過的架構的重要組成部分是圍繞微服務的安全性。 具體來說,是身份驗證和授權模式。
在考慮微服務中的身份驗證時,有多種選擇,但是此博客將特別關注使用JSON Web令牌。
JSON Web令牌
本質上,JSON Web令牌(JWT)是一個獨立的身份驗證令牌,可以包含諸如用戶標識符,用戶的角色和權限以及您可能希望存儲在其中的任何其他信息。 任何人都可以輕松讀取和解析它,并可以使用密鑰驗證其真實性。 有關JSON Web令牌的簡要介紹,請查看此頁面 。
將JSON Web令牌與微服務一起使用的一個優勢是,我們可以對其進行設置,使其已經包含用戶擁有的所有權限。 這意味著每個服務都無需伸手我們的授權服務即可對用戶進行授權。
JWT的另一個優點是它們是可序列化的,并且足夠小以適合請求標頭。
怎么運行的
工作流程非常簡單。 第一個請求是使用用戶名和密碼的POST到不受保護的身份驗證端點。
成功認證后,響應將包含JWT。 所有其他請求都帶有一個HTTP標頭,其中包含以Authorization: xxxxx.yyyyy.zzzzz形式的JWT令牌Authorization: xxxxx.yyyyy.zzzzz 。
任何服務到服務的請求都將傳遞此標頭,以便任何服務都可以沿途應用授權。
現在,到代碼!
我們需要做的第一件事是弄清楚如何生成這些JWT。 幸運的是,我們不是第一個嘗試此操作的人,并且有多個庫可供選擇。
我選擇了Java JWT 。 這是我的實現:
public class JsonWebTokenUtility {private SignatureAlgorithm signatureAlgorithm;private Key secretKey;public JsonWebTokenUtility() {// THIS IS NOT A SECURE PRACTICE!// For simplicity, we are storing a static key here.// Ideally, in a microservices environment, this key would kept on a// config server.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 missingthis.setExceptionIfHeaderMissing(false);// This is the request header it will look forthis.setPrincipalRequestHeader("Authorization");}@Override@Autowiredpublic void setAuthenticationManager(AuthenticationManager authenticationManager) {super.setAuthenticationManager(authenticationManager);} }此時,標頭已以PreAuthenticatedAuthenticationToken的形式轉換為Spring Authentication對象。
現在,我們需要一個身份驗證提供程序,它將讀取此令牌,對其進行身份驗證并將其轉換為我們自己的自定義Authentication對象。
public class JsonWebTokenAuthenticationProvider implements AuthenticationProvider {private JsonWebTokenUtility tokenService = new JsonWebTokenUtility();@Overridepublic Authentication authenticate(Authentication authentication) throws AuthenticationException {Authentication authenticatedUser = null;// Only process the PreAuthenticatedAuthenticationTokenif (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 JsonWebTokenAuthenticationauthenticatedUser = 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;}@Overridepublic 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令牌。
翻譯自: https://www.javacodegeeks.com/2016/06/json-web-tokens-spring-cloud-microservices.html
總結
以上是生活随笔為你收集整理的带有Spring Cloud Microservices的JSON Web令牌的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 安卓单机游戏好玩的手游(安卓单机游戏好玩
- 下一篇: 怎么重装系统如何重新做电脑系统