[英]Problem get jwt with Spring boot to angular
I have a problem with a POST request from Angular to Spring.从 Angular 到 Spring 的 POST 请求出现问题。 Here is my error in the console.
这是我在控制台中的错误。
Back-end, I have correctly entered my parameters for the CORS后端,我已经正确输入了 CORS 的参数
protected void doFilterInternal(final HttpServletRequest request, final HttpServletResponse response, final FilterChain filterChain) throws ServletException, IOException {
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse res = (HttpServletResponse) response;
res.setHeader("Access-Control-Allow-Origin",request.getHeader("origin"));
res.setHeader("Access-Control-Allow-Credentials", "true");
res.setHeader("Access-Control-Allow-Methods", "POST, PUT, GET, OPTIONS, DELETE");
res.setHeader("Access-Control-Max-Age", "3600");
res.setHeader("Access-Control-Allow-Headers", "Content-Type, Accept, X-Requested-With, remember-me");
try {
String jwt = getJwtFromCookie(request);
if (agentProxy.authSuccess(jwt)){
filterChain.doFilter(request,response);
} else {
response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
}
}
catch (Exception ex) {
ex.printStackTrace();
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
}
and when I get to the recovery of the JWT当我恢复 JWT
String jwt = getJwtFromCookie(request);
He gets nothing back.他什么也得不到。 My forehead with my method:
我的额头与我的方法:
createNoteRessource(noteRessource: NoteRessourceModel, idRessource: number){
const headers: HttpHeaders = new HttpHeaders();
headers.append('Content-Type', 'application/json');
headers.append('Access-Control-Allow-Origin','*');
const httpOption = {
headers: new HttpHeaders({'Content-type':'application/json'}),
withCredentials:true
};
return this.http.post(
this.baseUrl+'/noteRessource/create/'+idRessource, noteRessource, httpOption).pipe(
tap(_=> this.log("Ajout d'une note ressource")),
catchError(this.handleError<NoteRessourceModel>("createNoteRessource"))
);
No matter how much I turned the problem around and nothing, I've been looking for 2 days.不管我如何解决问题而一无所获,我一直在寻找 2 天。
I don't really understand the problem.我真的不明白这个问题。 My url is correct... I have a doubt when I see the header of my POST request on the client side.
我的 url 是正确的......当我在客户端看到我的 POST 请求的 header 时,我有疑问。
Frankly I do not see... Back side I get no Cookie even though I have some坦率地说,我没有看到......即使我有一些 Cookie,我也没有得到背面
private String getJwtFromCookie(HttpServletRequest request) {
Cookie[] cookies = request.getCookies();
if(cookies==null)
return "";
for (Cookie cookie : cookies) {
if (cookie.getName().equals("AuthToken")) {
String accessToken = cookie.getValue();
if (accessToken == null) {
return null;
}
return accessToken;
}
}
return null;
}
}
If someone has an idea?如果有人有想法?
Thank you so much太感谢了
It would be enough if you just add a @CrossOrigin("http://yourAngularUrl:port")
annotation above your controller class declaration.如果您只需在 controller class 声明上方添加一个
@CrossOrigin("http://yourAngularUrl:port")
注释就足够了。 So if you are developing on your local machine, and using default angular port the annotation will look like this因此,如果您在本地机器上进行开发,并使用默认的 angular 端口,注释将如下所示
@CrossOrigin("http://localhost:4200")
@RestController
public class HelloCOntroller{}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.