1 回答
TA贡献1871条经验 获得超13个赞
您可能需要做一些事情:
编写一个安全过滤器来调用 FirebaseAuth 对 Bearer Token 进行身份验证。令牌经过身份验证后,将其放入 SecurityContext 中。类似于:
public class FirebaseFilter extends OncePerRequestFilter {
private static String AUTH_HEADER = "Authorization";
@Override
protected void doFilterInternal(HttpServletRequest request,
HttpServletResponse response,
FilterChain filterChain) throws ServletException, IOException {
String authToken = request.getHeader(AUTH_HEADER).substring(7);
if (!StringUtils.isEmpty(authToken)) {
Authentication auth = getAuthentication(authToken);
SecurityContextHolder.getContext().setAuthentication(auth);
logger.debug("Successfully Authenticated");
}
filterChain.doFilter(request, response);
}
private FirebaseToken verifyIdToken(String idToken) {
if (StringUtils.isEmpty(idToken)) {
throw new IllegalArgumentException("idToken is blank");
}
return FirebaseAuth.getInstance().verifyIdToken(idToken);
}
private Authentication getAuthentication(String idToken) {
FirebaseToken token = verifyIdToken(idToken);
assert token != null;
return new FirebaseAuthenticationToken(token.getUid(), token);
}
}
您将需要 UserDetailsService 的实现,我相信您已经有了。
您将需要一个安全提供程序,它从安全上下文中获取身份验证,然后调用 UserDetailsService 来获取应用程序可能需要的任何信息。然后更新认证对象。类似于:
@Component
public class FirebaseAuthenticationProvider implements AuthenticationProvider {
private UserService userService;
@Autowired
public FirebaseAuthenticationProvider(UserService userService) {
this.userService = userService;
}
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
if (!supports(authentication.getClass())) {
return null;
}
UserDetails details = userService.loadUserByUsername(authentication.getPrincipal()
.toString());
FirebaseToken token = (FirebaseToken) authentication.getCredentials();
if (details == null) {
details = userService.registerUser(token);
}
return new FirebaseAuthenticationToken(details, token, details.getAuthorities());
}
public boolean supports(Class<?> authentication) {
return (FirebaseAuthenticationToken.class.isAssignableFrom(authentication));
}
}
添加回答
举报