我有一個簡單的Java REST API。我通過郵差測試這個API,並且一切正常。但是現在我想用這個API學習Angular2。我嘗試登錄到應用程序,我有問題,因爲我不知道如何在Angular2中創建請求。在郵差我這樣做。角2和令牌認證
Postman screen shot 這是我的代碼,後端配置。
package org.mroczek.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;
@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter{
@Autowired
private AuthenticationManager authenticationManager;
@Override
public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
security.checkTokenAccess("isAuthenticated()");
}
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients.inMemory().withClient("my-trusted-client")
.authorizedGrantTypes("client_credentials", "password")
.authorities("ROLE_CLIENT","ROLE_TRUSTED_CLIENT").scopes("read","write","trust")
.resourceIds("oauth2-resource").accessTokenValiditySeconds(5000).secret("secret");
}
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
endpoints.authenticationManager(authenticationManager);
}
}
這是我autentication.service.ts
import { Injectable } from '@angular/core';
import { Http, Headers, Response } from '@angular/http';
import 'rxjs/add/operator/map'
import {map} from 'rxjs/operator/map';
@Injectable()
export class AuthenticationService {
constructor(private http: Http) { }
login(username: string, password: string) {
var headers = new Headers();
headers.append('Content-Type', 'application/json;charset=UTF-8');
let body = JSON.stringify({ username: username, password: password });
return this.http.post('http://localhost:8080/',JSON.stringify({username, password }),{headers})
.map((response: Response) => {
console.log(response)
// login successful if there's a jwt token in the response
let user = response.json();
if (user && user.token) {
// store user details and jwt token in local storage to keep user logged in between page refreshes
localStorage.setItem('currentUser', JSON.stringify(user));
}
console.log(user);
return user;
});
}
logout() {
// remove user from local storage to log user out
localStorage.removeItem('currentUser');
}
}
當我發送請求時,在後臺日志中我看到,頁面沒有找到。但我不知道什麼是正確的網址。
你讀過此頁:https://angular.io/guide/http和https://angular.io/tutorial/toh-pt6#providing-http-services。學習時他們是一個很好的資源 – Vega
我讀了這個頁面,但我仍然不知道這是怎麼做的。 –
到目前爲止你寫過什麼嗎?如果這樣向我們展示! – Vega