java - JWT 应该是一个单独的身份验证微服务,而不是与后端业务逻辑一起使用吗?

标签 java spring-boot jwt microservices

我是微服务架构的新手,我正在使用 SpringBoot 构建应用程序,并希望为我的 API 添加 JWT 身份验证。

引用链接:https://dzone.com/articles/spring-boot-security-json-web-tokenjwt-hello-world

我想知道是否应该将身份验证/授权代码与业务微服务(BMS)分开。因此,每次对 BMS 的 REST API 调用都会依次调用 auth-microservice 进行验证。这是一个好的做法还是会占用大量网络流量?

调用可能如下所示:

客户端 -> 业务应用程序 -> AuthMS -> 业务应用程序 -> 客户端

将其分离出来的原因是,有一些配置和代码与业务应用程序结合在一起看起来不太好,但我不确定每个 API 调用所需的网络成本。

JWT 应用程序中的示例代码对于在不同的服务/服务器中运行有意义吗? :

import java.util.Objects;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.DisabledException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.javainuse.service.JwtUserDetailsService;
import com.javainuse.config.JwtTokenUtil;
import com.javainuse.model.JwtRequest;
import com.javainuse.model.JwtResponse;
@RestController
@CrossOrigin
public class JwtAuthenticationController {
@Autowired
private AuthenticationManager authenticationManager;
@Autowired
private JwtTokenUtil jwtTokenUtil;
@Autowired
private JwtUserDetailsService userDetailsService;
@RequestMapping(value = "/authenticate", method = RequestMethod.POST)
public ResponseEntity<?> createAuthenticationToken(@RequestBody JwtRequest authenticationRequest) throws Exception {
authenticate(authenticationRequest.getUsername(), authenticationRequest.getPassword());
final UserDetails userDetails = userDetailsService
.loadUserByUsername(authenticationRequest.getUsername());
final String token = jwtTokenUtil.generateToken(userDetails);
return ResponseEntity.ok(new JwtResponse(token));
}
private void authenticate(String username, String password) throws Exception {
try {
authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(username, password));
} catch (DisabledException e) {
throw new Exception("USER_DISABLED", e);
} catch (BadCredentialsException e) {
throw new Exception("INVALID_CREDENTIALS", e);
}
}
}

最佳答案

让您的 API 网关处理所有授权请求是一个很好的做法。 请求将通过 api 网关进行验证,然后才能访问微服务(业务逻辑所在的位置)。让您的网关负责以下工作:

(1) validate tokens with every request (2) prevent all unauthenticated requests to the services

Check this out for more details

关于java - JWT 应该是一个单独的身份验证微服务,而不是与后端业务逻辑一起使用吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58948497/

相关文章:

java - 具有相同模式 url 的多个路由

java - Android N Stream API 是否向后移植到较低版本?

使用 Spring Boot 时未初始化 java.lang.IllegalStateException LifecycleProcessor 和 ApplicationEventMulticaster

node.js - 使用 Node.js 和 jsonwebtoken 在哪里做 JWT 到期?

java - 错误java。我不知道如何修复它

java - 测试中在xml资源之前读取spring属性源

java - 如何在多对多关联表上编写查询

Java Rest api 需要等待才能处理

java - 是否有可能创建一个具有一定长度的 jwt 身份验证 token

jwt - 后端 api 的 OpenID Connect JWT token 验证和使用策略 - jwks 还是 session ?