当前位置: 动力学知识库 > 问答 > 编程问答 >

java - RESTful API authentication and further requests

问题描述:

I wrote a RESTful API in java in which the client has to create an account (POST to /user) and then login (POST to /login).

Now I am creating a frontend for the web-browser using AngularJS. The problem is, when I create a user, try to login and enter some login-secured-url's, I get

401 Unauthorized

It seems like every request should be authenticated separately, am I right ?

Steps I made:

  1. POST to /user which creates a user
  2. POST to /login which logs in the user
  3. GET to /account/1/something which is login-secured path in my REST service, it returns 401

When I was writing the REST service, I was testing everything under RESTful WebService test-client in IntelliJ IDEA and it was and still is all right there.

What is the way to solve this problem ?

@Neo

Here you have RestAuthenticationSuccessHandler

@Component("successHandler")

public class RestAuthenticationSuccessHandler extends SimpleUrlAuthenticationSuccessHandler {

private RequestCache requestCache = new HttpSessionRequestCache();

@Override

public void onAuthenticationSuccess(final HttpServletRequest request, final HttpServletResponse response, final Authentication authentication)

throws ServletException, IOException {

final SavedRequest savedRequest = requestCache.getRequest(request, response);

if (savedRequest == null) {

clearAuthenticationAttributes(request);

return;

}

final String targetUrlParameter = getTargetUrlParameter();

if (isAlwaysUseDefaultTargetUrl() || (targetUrlParameter != null && StringUtils.hasText(request.getParameter(targetUrlParameter)))) {

requestCache.removeRequest(request, response);

clearAuthenticationAttributes(request);

return;

}

clearAuthenticationAttributes(request);

}

public void setRequestCache(final RequestCache requestCache) {

this.requestCache = requestCache;

}

}

I have also another classes, but the post would become too wordy if I added it all, so please let me know if you (suggesting a classname) need code of any of these below:

分享给朋友:
您可能感兴趣的文章:
随机阅读: