我想使用post将角度6的单个字符串传递给springboot服务器控制器,但是它始终会因httpErrorResponse 403而失败,并且错误消息“禁止”,我是角度不熟悉的,希望有人可以帮助我,非常感谢。我的Java版本是10。 以下是我的相关代码:
component.html:
<p-button label="Stop" styleClass="ui-button-danger" (onClick)="send()"></p-
button>
{{result}}
component.ts:
export class TestComponent implements OnInit {
selectedFile: string;
selectedFile = "20180812";
result: string;
constructor(private cityService: CityService) {}
send() {
//pass a string selectedFile
this.cityService.send(this.selectedFile).subscribe(data=> this.result =
data);
}
}
service.ts:
import { Injectable } from '@angular/core';
import {HttpClient, HttpHeaders} from "@angular/common/http";
import {Observable} from "rxjs/internal/Observable";
@Injectable({
providedIn: 'root'
})
export class CityService {
private testUrl = '//localhost:8080/sendString';
constructor(private http: HttpClient) { }
send(s: string): Observable<any> {
return this.http.post(this.testUrl, s, {
headers: new HttpHeaders({
'Content-Type': 'text/plain'
})
});
}
}
controller.java:
@CrossOrigin(origins = "http://localhost:4200")
@RestController
public class TestController {
@RequestMapping(value = "/sendString", method = RequestMethod.POST,consumes =
"text/plain")
public String pass(@RequestBody String test1) {
System.out.println(test1);
return test1;
}}
Chrome浏览器上的错误消息:The error screenshot
securityConfig.java:
package com.predicine.sbproto.security;
import *;
@Configuration
@EnableGlobalMethodSecurity( securedEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
}
application.property:
#Security Configuration
spring.security.user.name=user
spring.security.user.password=user
spring.security.user.roles=USER