I have a bitbucket plugin that gives a REST API. I want to make a GET request on this API with a webapplication running on Angular4. First i got a lot of Prefligt errors. I have added the @CrossOrigin annotation in my API and now i receive 0 everytime i make a request on /project/number. If i return a simple number without accessing the Java API of bitbucket i receive the returned number, but if i access something from the Java API it return. So i am thinking that that it has something to do with the authentication.
REST API:
@Path("/project")
@CrossOrigin
public class ProjectResource {
private ProjectMonitorService service;
@Inject
ProjectResource(@ComponentImport ProjectMonitorService service) {
this.service = service;
}
@GET
@AnonymousAllowed
@Produces({MediaType.APPLICATION_JSON})
public Response getMessage() {
return Response.ok(new ProjectResourceModel("Hello World"+ service.getNumberOfProjects(), "")).build();
}
@GET
@AnonymousAllowed
@Produces({MediaType.APPLICATION_JSON})
@Path("number")
public Response getNumberOfProjects(){
service.login();
return Response.ok(new IntegerModel(service.getNumberOfProjects()))
.header("Access-Control-Allow-Headers", "*")
.build();
}
Angular Client:
It will be as you wrote, anonymous user does not have permission to view the projects so 0 is returned by the service.
Try to use basic authentication with real user when creating request to check you're getting corrent number of projects.
sorry, I sent this accidentally...
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I never developed plugin for bit bucket but I think the behavior will be similar to confluence or jira. It is as you wrote, anonymous user does not have permission to view the projects so you will receive 0. You can try to use basic authentication and remove @AnonymousAllowed annotation just to check it is caused by request sent under anonymous user.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I tried it, it doesn't work. I used basic authentication and it gives me a 401 error. It is almost always a preflight error. So i tried to change some of the server settings and wrote a filter in the tomcat/conf/web.xml file and in the cargo-bitbucket-home/conf/web.xml and now I get a Options (url) 403 (forbidden). The filter that i wrote should allow everything.
Fitler:
<filter>
<filter-name>CorsFilter</filter-name>
<filter-class>org.apache.catalina.filters.CorsFilter</filter-class>
<init-param>
<param-name>cors.allowed.origins</param-name>
<param-value>*</param-value>
</init-param>
<init-param>
<param-name>cors.allowed.methods</param-name>
<param-value>GET,POST,HEAD,OPTIONS,PUT</param-value>
</init-param>
<init-param>
<param-name>cors.allowed.headers</param-name>
<param-value>Content-Type,X-Requested-With,accept,Origin,Access-Control-Request-Method,Access-Control-Request-Headers,*</param-value>
</init-param>
<init-param>
<param-name>cors.exposed.headers</param-name>
<param-value>Access-Control-Allow-Origin,Access-Control-Allow-Credentials,*</param-value>
</init-param>
<init-param>
<param-name>cors.support.credentials</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>cors.preflight.maxage</param-name>
<param-value>10</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CorsFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
If you need anything else just tell me
Thanks
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
If you ever did a plugin with a REST api and CORS was working there could i take a look?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.