Java JWT应该是一个独立的auth微服务,而不是与后端业务逻辑坐在一起吗?

Java JWT应该是一个独立的auth微服务,而不是与后端业务逻辑坐在一起吗?,java,spring-boot,jwt,microservices,Java,Spring Boot,Jwt,Microservices,我是微服务体系结构的新手,我正在使用SpringBoot构建应用程序,并希望为我的API添加JWT auth 参考链接: 我想知道是否应该将身份验证/授权代码从business micro service(BMS)中分离出来。因此,每次对BMS的RESTAPI调用都会依次调用auth微服务进行验证。这是一种很好的做法,还是会对网络流量造成很大影响 电话可能看起来像: 客户端->业务应用->授权->业务应用->客户端 将其分离出来的原因是,有些配置和代码与业务应用程序结合起来看起来不太好,但我不确

我是微服务体系结构的新手,我正在使用SpringBoot构建应用程序,并希望为我的API添加JWT auth

参考链接:

我想知道是否应该将身份验证/授权代码从business micro service(BMS)中分离出来。因此,每次对BMS的RESTAPI调用都会依次调用auth微服务进行验证。这是一种很好的做法,还是会对网络流量造成很大影响

电话可能看起来像:

客户端->业务应用->授权->业务应用->客户端

将其分离出来的原因是,有些配置和代码与业务应用程序结合起来看起来不太好,但我不确定每次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);
}
}
}
导入java.util.Objects;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.http.ResponseEntity;
导入org.springframework.security.authentication.AuthenticationManager;
导入org.springframework.security.authentication.BadCredentialsException;
导入org.springframework.security.authentication.DisabledException;
导入org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
导入org.springframework.security.core.userdetails.userdetails;
导入org.springframework.web.bind.annotation.CrossOrigin;
导入org.springframework.web.bind.annotation.RequestBody;
导入org.springframework.web.bind.annotation.RequestMapping;
导入org.springframework.web.bind.annotation.RequestMethod;
导入org.springframework.web.bind.annotation.RestController;
导入com.javainuse.service.JwtUserDetailsService;
导入com.javainuse.config.JwtTokenUtil;
导入com.javainuse.model.JwtRequest;
导入com.javainuse.model.JwtResponse;
@RestController
@交叉起源
公共类JwtAuthenticationController{
@自动连线
私人AuthenticationManager AuthenticationManager;
@自动连线
私有JwtTokenUtil JwtTokenUtil;
@自动连线
私有JWTUserDetails服务UserDetails服务;
@RequestMapping(value=“/authenticate”,method=RequestMethod.POST)
public ResponseEntity createAuthenticationToken(@RequestBody JwtRequest authenticationRequest)引发异常{
验证(authenticationRequest.getUsername(),authenticationRequest.getPassword());
最终用户详细信息用户详细信息=用户详细信息服务
.loadUserByUsername(authenticationRequest.getUsername());
最终字符串标记=jwtTokenUtil.generateToken(userDetails);
返回ResponseEntity.ok(新JwtResponse(令牌));
}
私有void身份验证(字符串用户名、字符串密码)引发异常{
试一试{
authenticationManager.authenticate(新用户名PasswordAuthenticationToken(用户名、密码));
}捕获(禁用异常e){
抛出新异常(“用户禁用”,e);
}捕获(BadCredentialsException e){
抛出新异常(“无效的_凭证”,e);
}
}
}

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

(1) 在每个请求中验证令牌 (2) 防止对服务的所有未经验证的请求


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

(1) 在每个请求中验证令牌 (2) 防止对服务的所有未经验证的请求


因此,我认为将其作为微服务进行分离是一种很好的做法,但我有一个疑问,网关如何了解传入的请求,因为它作为不同的微服务在不同的端口上运行此链接有它所有我尝试了与spring cloud api gateway zuul链接相同的示例,但我的请求在401中失败,你知道吗?我只是用springboot尝试zuul的一个基本示例,以了解通过gateway到其他服务的路由,thankswas坚持了3天,尝试了一切,通过清理项目解决了问题。因此,我认为将其作为微服务分离是一个很好的做法,但我有一个疑问,网关是如何知道传入的请求的,因为它作为不同的微服务在不同的端口上运行此链接所有我已经尝试了与spring cloud api网关zuul链接相同的示例,但我的请求因401而失败,未经授权,知道吗?我只是尝试一个基本的zuul和springboot示例,以了解通过gateway到其他服务的路由,thankswas卡住了3天,尝试了所有方法,通过清理项目修复了它。