Skip to content

Feature/cors #91

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ are used to configure these various services, as explained below.
The Content Service can be started locally using the following command:

~~~
~/spring-docs/spring-docs/$ mvn spring-boot:run [-Dspring.profiles.active=<database profile>, <storage profile> [, google-classification]]
~/spring-docs/spring-docs/$ mvn spring-boot:run [-Dspring-boot.run.profiles=<database profile>, <storage profile> [, google-classification]]
~~~

### Database Profiles
Expand Down Expand Up @@ -83,6 +83,10 @@ Document classification can be enabled by adding the `google-classification` to
By default the application is configured to allow any client to connect. You can specify the environment variable `SPRINGDOCS_ALLOW_HOST` to specify the host to allow, restricting
all others.

~~~
~/spring-docs/spring-docs/$ mvn spring-boot:run -Dspring-boot.run.jvmArguments="-DSPRINGDOCS_ALLOW_HOST=http://localhost:8080"
~~~

## Running the UI Service

The user interface service can be started using the following command:
Expand Down
33 changes: 28 additions & 5 deletions spring-docs/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring.content.version>1.2.0</spring.content.version>
<spring.content.version>1.2.2</spring.content.version>
</properties>

<dependencies>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
Expand Down Expand Up @@ -107,15 +107,21 @@
<artifactId>spring-content-renditions-boot-starter</artifactId>
<version>${spring.content.version}</version>
</dependency>
<dependency>
<!-- Search defaults to elasticsearch. Comment if you want to use solor. -->
<dependency>
<groupId>com.github.paulcwarren</groupId>
<artifactId>spring-content-elasticsearch</artifactId>
<version>${spring.content.version}</version>
</dependency>
</dependency>
<dependency>
<groupId>com.github.paulcwarren</groupId>
<artifactId>spring-content-solr</artifactId>
<version>${spring.content.version}</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
</dependency>
</dependency>

<dependency>
<groupId>com.github.paulcwarren</groupId>
Expand Down Expand Up @@ -175,6 +181,23 @@
<version>1.24.1</version>
</dependency>

<!-- Solr -->
<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-solrj</artifactId>
<version>7.7.3</version> <!-- Match the deployed instance -->
<exclusions>
<exclusion>
<groupId>org.codehaus.woodstox</groupId>
<artifactId>wstx-asl</artifactId>
</exclusion>
<exclusion>
<artifactId>log4j</artifactId>
<groupId>log4j</groupId>
</exclusion>
</exclusions>
</dependency>

<!-- Test -->
<dependency>
<groupId>com.github.paulcwarren</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.github.paulcwarren.springdocs.config;

import static java.lang.String.format;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
Expand All @@ -8,25 +10,24 @@
import java.util.List;
import java.util.Map;

import org.springframework.boot.system.SystemProperties;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMethod;

import com.github.paulcwarren.springdocs.repositories.JpaDocumentRepository;
import com.github.paulcwarren.springdocs.repositories.MongoDocumentRepository;
import com.github.paulcwarren.springdocs.stores.FsDocumentStore;
import com.github.paulcwarren.springdocs.stores.GridFsDocumentStore;
import com.github.paulcwarren.springdocs.stores.JpaDocumentStore;
import com.github.paulcwarren.springdocs.stores.S3DocumentStore;

import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMethod;

import static java.lang.String.format;

public class CorsOriginUpdater {

private static Method method = null;

static {

String allowedHost = System.getenv("SPRINGDOCS_ALLOW_HOST");
String allowedHost = SystemProperties.get("SPRINGDOCS_ALLOW_HOST");

if (allowedHost != null) {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.github.paulcwarren.springdocs.config;

import java.util.Arrays;
import java.util.Collections;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
Expand All @@ -10,6 +13,9 @@
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

/**
* Base security configuration provides Authentication object required by
Expand Down Expand Up @@ -37,6 +43,24 @@ public AuthenticationEntryPoint getBasicAuthEntryPoint() {
return new AuthenticationEntryPoint();
}


/**
* Enable calls from client app
* @return
*/
@Bean
public CorsFilter corsFilter() {
final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
final CorsConfiguration config = new CorsConfiguration();
config.setAllowCredentials(true);
// Don't do this in production, use a proper list of allowed origins
config.setAllowedOrigins(Collections.singletonList("http://localhost:8080"));
config.setAllowedHeaders(Arrays.asList("Origin", "Content-Type", "Accept"));
config.setAllowedMethods(Arrays.asList("GET", "POST", "PUT", "OPTIONS", "DELETE", "PATCH"));
source.registerCorsConfiguration("/**", config);
return new CorsFilter(source);
}

@Override
protected void configure(HttpSecurity http) throws Exception {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import pl.allegro.tech.embeddedelasticsearch.EmbeddedElastic;

import org.springframework.content.elasticsearch.EnableElasticsearchFulltextIndexing;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.github.paulcwarren.springdocs.config.store;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration;
import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration;
import org.springframework.content.fs.config.EnableFilesystemStores;
Expand All @@ -19,23 +19,45 @@
@Profile("fs")
public class FsConfig {

private static final Log logger = LogFactory.getLog(FsConfig.class);

@Bean
public FilesystemStoreConfigurer configurer() {
@Bean
@ConditionalOnExpression("'${spring.content.fs.directoryFormat}'=='flat'")
public FilesystemStoreConfigurer flatConfigurer() {
return new FilesystemStoreConfigurer() {

@Override
public void configureFilesystemStoreConverters(ConverterRegistry registry) {
registry.addConverter(new Converter<String, String>() {

// Single Directory
@Override
public String convert(String source) {
// Standard UID format
return String.format("%s", source.toString());
}

});
}
};
}

@Bean
@ConditionalOnExpression("'${spring.content.fs.directoryFormat}'=='hierarchy'")
public FilesystemStoreConfigurer hierarchyConfigurer() {
return new FilesystemStoreConfigurer() {

@Override
public void configureFilesystemStoreConverters(ConverterRegistry registry) {
registry.addConverter(new Converter<String, String>() {

// Directory hierarchy
@Override
public String convert(String source) {
// Standard UID format
return String.format("/%s", source.toString().replaceAll("-", "")).replaceAll("..", "$0/");
}
});
}
};
}

}
19 changes: 16 additions & 3 deletions spring-docs/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,17 +1,30 @@
server.port=9090
#debug=true

spring.jpa.hibernate.ddl-auto=update
# Solar URL
solr.url=http://localhost:8983/solr/solr

# Set active profile in property file for IDE runtime
# spring.profiles.active=postgres,fs

# Disable full text search by default
spring.content.elasticsearch.autoindex=false

# JP hibernate options create, create-drop, validate, and update
spring.jpa.hibernate.ddl-auto=create-drop

#spring.content.s3.bucket=spring-docs
#logging.level.org.springframework.web=DEBUG
#logging.level.com.emc.ecs.connector.spring=DEBUG
logging.level.internal.org.springframework.content.rest.controllers=DEBUG
#logging.level.internal.org.springframework.content.rest.controllers=DEBUG

# Control root location of file store
spring.content.fs.filesystemRoot=/tmp/spring-content-store
spring.content.rest.fullyQualifiedLinks=false

# hierarchy or flat
spring.content.fs.directoryFormat=flat

# Control file upload size
spring.servlet.multipart.max-file-size=100MB

Expand All @@ -24,4 +37,4 @@ spring.jpa.database-platform=org.hibernate.dialect.H2Dialect

# Log SQL
#logging.level.org.hibernate.SQL=DEBUG
#logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE
#logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE