如何将Angular ts Http(带凭据)与Spring Security集成

如果有一个文档或示例可供公众使用,以展示如何将Angular http(与凭据)与Spring Security集成,那将是最好的。

我有办法登录,我将在下面显示代码,但我认为必须有更好的方法。 也许在Http header withCredentials中有选项,但是你提供了你的凭据?

它保持idToken不受外部认证。 服务(Google+)和标题中的类型(确定身份validation服务的类型),因此您不需要将它们作为请求参数或路径变量传递。

然后在后端(Spring Java)中,有一个spring AOP,用于在validation后将用户保存到SecurityContext。

Angular Http Call

import { Http, Headers, RequestOptions } from '@angular/http'; ... constructor(private http: Http...){...} ... search(){ let options ; if (this.loginService.user) { let headers = new Headers({ 'idToken': this.loginService.user.idToken,'type':this.loginService.user.type}); options = new RequestOptions({ headers: headers }); } return this.http .get("searchurl",options) ... 

GooglePlusAuthService

 import java.util.Collections; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import com.google.api.client.googleapis.auth.oauth2.GoogleIdToken; import com.google.api.client.googleapis.auth.oauth2.GoogleIdToken.Payload; import com.google.api.client.googleapis.auth.oauth2.GoogleIdTokenVerifier; import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.client.json.jackson2.JacksonFactory; @Service public class GooglePlusAuthService implements AuthenticationService{ private Logger logger = LoggerFactory.getLogger(GooglePlusAuthService.class); private static String clientId; @Value("${client_id.google}") public void setClientId(String clientId){ GooglePlusAuthService.clientId=clientId; } @Override public void login(String token) { GoogleIdTokenVerifier verifier = new GoogleIdTokenVerifier.Builder(new NetHttpTransport(), new JacksonFactory())//.setIssuer(clientId) .setAudience(Collections.singletonList(clientId)) .build(); GoogleIdToken idToken; try { idToken = verifier.verify(token); if (idToken != null) { Payload payload = idToken.getPayload(); User user = new User(); user.setId(Authenticator.AUTH_TYPE_GOOGLE+"_"+payload.getSubject()); user.setUsername((String) payload.get("name")); user.setToken(token); AuthenticationUtils.setUser(user); } else { logger.info("Failed to login with Google plus. Invalid ID token."); } } catch (Exception e) { e.printStackTrace(); logger.error("Failed to login with Google plus." + e.getMessage()); } } } 

AuthenticationUtils

 import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.core.Authentication; import org.springframework.security.core.context.SecurityContextHolder; public class AuthenticationUtils { public static void setUser(User user){ Authentication authentication = new UsernamePasswordAuthenticationToken(user, null); SecurityContextHolder.getContext().setAuthentication(authentication); } public static User getUser(){ if(SecurityContextHolder.getContext().getAuthentication()!=null) return (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); return null; } } 

这段代码有一个我也试图找出的错误

当我没有提供任何凭据信息时,为什么AuthenticationUtils.getUser()给我最后一个登录用户。 我刚用私人浏览器打开了url,它让我在后端找到了最后一个用户。