Configured proxy

This commit is contained in:
Nikolaos Laskaris 2017-09-29 15:13:21 +03:00
parent 266c5c0767
commit 839952a5cb
6 changed files with 90 additions and 9 deletions

View File

@ -254,7 +254,6 @@
</dependency> </dependency>
<!-- Various libs --> <!-- Various libs -->
<dependency> <dependency>
<groupId>org.apache.commons</groupId> <groupId>org.apache.commons</groupId>

View File

@ -0,0 +1,72 @@
package rest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLEncoder;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
@CrossOrigin
public class Proxy {
private String allowedHost;
public Proxy(String allowedHost) throws MalformedURLException {
this.allowedHost = new URL(allowedHost).getHost();
}
@RequestMapping(method = RequestMethod.GET, value = { "/proxy" }, produces="application/json")
public @ResponseBody ResponseEntity<Object> proxy(@RequestParam("url") String remoteUrl) {
StringBuffer response = new StringBuffer();
URL url;
try {
URL tempUrl = new URL(remoteUrl);
// URI uri = new URI(scheme, userInfo, host, port, path, query, fragment);
URI uri = new URI(tempUrl.getProtocol(), null, tempUrl.getHost(), tempUrl.getPort(), tempUrl.getPath(), (tempUrl.getQuery()!=null)?URLEncoder.encode(tempUrl.getQuery()):null, tempUrl.getRef());
url = uri.toURL();
if(!url.getHost().equals(allowedHost))
return ResponseEntity.status(HttpStatus.FORBIDDEN).body("{'reason': 'You are not allowed to proxy -> "+url.getHost()+"'}");
//if allowed, proceed
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
con.setRequestProperty("Accept", "application/vnd.api+json; charset=utf-8");
int responseCode = con.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) { // success
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
response.append(inputLine);
in.close();
} else {
return ResponseEntity.status(HttpStatus.FORBIDDEN).body("{'reason': 'Remote server responded with: "+responseCode+"'}");
}
return ResponseEntity.status(HttpStatus.OK).body(response.toString());
} catch (IOException | URISyntaxException e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{'reason': 'Could not proxy to given host'}");
}
}
}

View File

@ -25,7 +25,7 @@ public class CustomAuthenticationProvider implements AuthenticationProvider {
@Override @Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException { public Authentication authenticate(Authentication authentication) throws AuthenticationException {
/*
if (authentication != null) { if (authentication != null) {
// check whether the token is valid // check whether the token is valid
String token = (String)authentication.getCredentials(); String token = (String)authentication.getCredentials();
@ -47,7 +47,9 @@ public class CustomAuthenticationProvider implements AuthenticationProvider {
} }
else else
throw new AuthenticationServiceException("Authentication failed"); throw new AuthenticationServiceException("Authentication failed");
*/
return new UsernamePasswordAuthenticationToken("", "", new ArrayList<>());
} }

View File

@ -11,10 +11,8 @@
<context:property-placeholder location="classpath*:**/dmp.properties" /> <context:property-placeholder location="classpath*:**/dmp.properties" />
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/> <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" /> <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
<bean id="jpaDialect" class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" /> <bean id="jpaDialect" class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
@ -26,14 +24,15 @@
<property name="jpaDialect" ref="jpaDialect" /> <property name="jpaDialect" ref="jpaDialect" />
</bean> </bean>
<bean id="proxy" class="rest.Proxy">
<constructor-arg type = "String" value = "${proxy.allowed.host}"/>
</bean>
<bean id="springApplicationContext" class="dao.SpringJpaDaoFactory" /> <bean id="springApplicationContext" class="dao.SpringJpaDaoFactory" />
<bean id="databaseColumnType" class="typedefinition.PostgreSQLDatabaseColumnType" /> <bean id="databaseColumnType" class="typedefinition.PostgreSQLDatabaseColumnType" />
<!-- <bean id="securityConfig" class="security.SecurityConfig" /> -->
<!-- <bean id="securityWebApplicationInitializer" class="security.SecurityWebApplicationInitializer" /> -->
<bean id="tokenAuthenticationFilter" class="security.TokenAuthenticationFilter" /> <bean id="tokenAuthenticationFilter" class="security.TokenAuthenticationFilter" />
<bean id="customAuthenticationProvider" class="security.CustomAuthenticationProvider" /> <bean id="customAuthenticationProvider" class="security.CustomAuthenticationProvider" />

View File

@ -10,10 +10,16 @@
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd"> http://www.springframework.org/schema/beans/spring-beans-4.1.xsd">
<context:property-placeholder location="classpath*:**/dmp.properties" />
<mvc:resources mapping="resources/**" location="/resources/" /> <mvc:resources mapping="resources/**" location="/resources/" />
<mvc:annotation-driven /> <mvc:annotation-driven />
<context:component-scan base-package="rest" /> <context:component-scan base-package="rest" />
<bean id="proxy" class="rest.Proxy">
<constructor-arg type = "String" value = "${proxy.allowed.host}"/>
</bean>
<bean <bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver"> class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/views/" /> <property name="prefix" value="/views/" />

View File

@ -10,6 +10,9 @@ persistence.dbusername = dmptool
persistence.dbpassword = dmpt00lu$r persistence.dbpassword = dmpt00lu$r
##########################/Persistence########################################## ##########################/Persistence##########################################
###################Allowed Proxy Service Host ############################
proxy.allowed.host = https://eestore.paas2.uninett.no
#######################################################
########################Persistence/Hibernate Generic############################# ########################Persistence/Hibernate Generic#############################
persistence.hibernate.show_sql = false persistence.hibernate.show_sql = false