我有一个简单的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