REST API access without login [message #1868979] |
Sat, 27 July 2024 05:26  |
Eclipse User |
|
|
|
I am testing access to Scout application via REST API.
Following the example in the Technical guide I created ExampleResource and ExampleDo.
Calling localhost:8082/api/example/1 first displays the login dialog (the application uses FormBasedAccessController). After logging in, when I call the url again, I get the correct response (JSON with the content of the specific object).
I would need access via the REST API to not require a login, but normal access to the application does.
Can anyone advise me how to configure this?
Thank you very much.
Scout version: 24.1.15
Edit:
When I call the url from another REST API client, I get this response:
{"error":{"code":10,"message":"The session has expired, please reload the page."}}
[Updated on: Sat, 27 July 2024 05:37] by Moderator
|
|
|
|
|
|
|
|
|
Re: REST API access without login [message #1870740 is a reply to message #1870437] |
Fri, 06 September 2024 05:31  |
Eclipse User |
|
|
|
Some hints on how to have the REST resources in the backend server with a proxy on UI server to forward the requests, based on Technical guide 24 - REST with the necessary modifications.
The example is based on a Scout Classic application with version 24.2.2.
- Add the dependencies as stated in Dependency Management to the pom.xml in .server.app
- Add the ApiServletContributor to ServerServletContributors as described in REST Resource Registration
- Add the REST resource to the module .server module as described in REST Resource Provider. No run context will be available in this example, make sure to run the method body within an own run context if required.
- Exclude "/api/*" in UiAuthFilterContributor (#getFilterExcludes). Beware: no authentication will be applied, make sure to add your own one if required.
- Add a class RestProxyRequestHandler to .ui.html with the following code:
@Order(4600)
public class RestProxyRequestHandler extends AbstractRestProxyRequestHandler {
@Override
protected String getRemoteBaseUrl() {
return BEANS.get(ServiceTunnelTargetUrlProperty.class).getValue().replace("/process", "");
}
@Override
protected String getLocalContextPathPrefix() {
return "/api/";
}
@Override
@PostConstruct
protected void initialize() {
super.initialize();
// Remove the cookie headers. The cookie is managed by the server running this proxy, not by the remote server.
getProxy().withRequestHeaderFilter(new HttpHeaderNameFilter("Cookie"));
getProxy().withResponseHeaderFilter(new HttpHeaderNameFilter("Set-Cookie"));
}
@Override
protected HttpProxyRequestOptions createHttpProxyRequestOptions(HttpServletRequest req, HttpServletResponse resp) {
return addSignatureHeader(new HttpProxyRequestOptions());
}
protected HttpProxyRequestOptions addSignatureHeader(HttpProxyRequestOptions options) {
DefaultAuthToken token = BEANS.get(DefaultAuthTokenSigner.class).createDefaultSignedToken(DefaultAuthToken.class);
if (token != null) {
options.withCustomRequestHeader(HttpServiceTunnel.TOKEN_AUTH_HTTP_HEADER, token.toString());
}
return options;
}
}
Regards
Stephan
|
|
|
Powered by
FUDForum. Page generated in 0.11046 seconds