【发布时间】:2019-09-04 13:59:06
【问题描述】:
我的问题是关于基于 jwt 令牌的身份验证,在后端使用 spring boot,在前端使用 angular,为此,我在 spring 安全配置中开发了两个过滤器,一个用于发布令牌,另一个用于验证它们,在过滤器内发出令牌:对“/login”端点的请求将包含用户详细信息,如密码和用户名,因此过滤器可以验证用户身份并作为回报发出令牌,这是我的问题,我看不到任何其他方式让身份验证操作,直到我们到达我必须制作特定端点的控制器。
@Override
public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response)
throws AuthenticationException {
try {
~~// 1. Get credentials from request
~~UserCredentials creds = new ~~ObjectMapper().readValue(request.getInputStream(), ~~UserCredentials.class);
// 2. Create auth object (contains credentials) which will be used by auth manager
~~UsernamePasswordAuthenticationToken authToken = new UsernamePasswordAuthenticationToken(creds.getUsername(),creds.getPassword(), Collections.emptyList());
// 3. Authentication manager authenticate the user, and use ~~UserDetialsServiceImpl::loadUserByUsername() method to load the user.
return authManager.authenticate(authToken);
~~} catch (IOException e) {
~~~~~~throw new RuntimeException(e);
}
}
我的朋友们,我希望你告诉我我已经做的事情是否好,以及如何在控制器内或直接在过滤器内执行该操作的最佳方式? 谢谢(请原谅我的英语)
预期结果:
@PostMapping("/login")
public void method(@RequestBody MyUser myUser){
// perform authentication and issue token for my front app
}
【问题讨论】:
标签: java angular spring-boot login jwt