diff --git a/.gitignore b/.gitignore
index 88b968b..4603eb3 100644
--- a/.gitignore
+++ b/.gitignore
@@ -54,9 +54,9 @@ atlassian-ide-plugin.xml
# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
-crashlytics.properties
-crashlytics-build.properties
-fabric.properties
+crashlytics.securityProperties
+crashlytics-build.securityProperties
+fabric.securityProperties
# Editor-based Rest Client
.idea/httpRequests
diff --git a/README.md b/README.md
index dfcb22a..8b1df7b 100644
--- a/README.md
+++ b/README.md
@@ -26,7 +26,7 @@ user's session, but with a cost of an extra http request per request.
import eu.dnetlib.uoaauthorizationlibrary.authorization.SecurityConfiguration;
- @PropertySources({@PropertySource("classpath:authorization.properties")})
+ @PropertySources({@PropertySource("classpath:authorization.securityProperties")})
@Import(SecurityConfiguration.class)
public class Application {
public static void main(String[] args) {
@@ -62,7 +62,7 @@ where session is stored.
import eu.dnetlib.uoaauthorizationlibrary.authorization.SecurityConfiguration;
- @PropertySources({@PropertySource("classpath:authorization.properties")})
+ @PropertySources({@PropertySource("classpath:authorization.securityProperties")})
@Import(SecurityConfiguration.class)
public class Application {
public static void main(String[] args) {
@@ -152,10 +152,25 @@ e.g
public Entity getEntity(@PathVariable("type") String type, @PathVariable("id") String id) {}
+## Exception Handling
+
+This library provides exception handling if an error is occurred.
+
+### Know Http Exceptions
+
+- UnauthorizedException (401)
+- ForbiddenException (403)
+- NotFoundException (404)
+- ConflictException (409)
+- UnprocessableException (422)
+
+Create your own Exception with HttpStatus by extending **HttpException** class.
+By default, any other Exception produces Http Status 400 (BAD REQUEST).
+
## Swagger configuration
This library by default includes swagger configuration, which is accessible only by PORTAL ADMIN users.
-Optional set API info properties in your project configuration file:
+Optional set API info securityProperties in your project configuration file:
api.title =
api.description =
diff --git a/src/main/java/eu/dnetlib/uoaauthorizationlibrary/AuthorizationConfiguration.java b/src/main/java/eu/dnetlib/uoaauthorizationlibrary/AuthorizationConfiguration.java
index 3c7a10c..13a8a8b 100644
--- a/src/main/java/eu/dnetlib/uoaauthorizationlibrary/AuthorizationConfiguration.java
+++ b/src/main/java/eu/dnetlib/uoaauthorizationlibrary/AuthorizationConfiguration.java
@@ -1,14 +1,77 @@
package eu.dnetlib.uoaauthorizationlibrary;
-import eu.dnetlib.uoaauthorizationlibrary.authorization.configuration.API;
-import eu.dnetlib.uoaauthorizationlibrary.authorization.configuration.GlobalVars;
-import eu.dnetlib.uoaauthorizationlibrary.authorization.configuration.Properties;
+import eu.dnetlib.uoaauthorizationlibrary.authorization.configuration.*;
+import jakarta.annotation.PostConstruct;
+import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
+import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
+import org.springframework.web.client.RestTemplate;
+
+import java.util.HashMap;
+import java.util.Map;
@Configuration
-@EnableConfigurationProperties({Properties.class, GlobalVars.class, API.class})
+@EnableConfigurationProperties({SecurityProperties.class, GlobalVars.class, API.class})
@ComponentScan(basePackages = {"eu.dnetlib.uoaauthorizationlibrary.authorization"})
public class AuthorizationConfiguration {
+
+ private final SecurityProperties properties;
+ private final GlobalVars globalVars;
+
+ @Autowired
+ public AuthorizationConfiguration(SecurityProperties properties, GlobalVars globalVars) {
+ this.properties = properties;
+ this.globalVars = globalVars;
+ }
+
+ @Bean
+ public RestTemplate restTemplate() {
+ return new RestTemplate();
+ }
+
+ @PostConstruct
+ public void checkProperties() {
+ if(properties.getDomain() == null || properties.getDomain().isEmpty()) {
+ throw new RuntimeException("authorization.security.domain is missing!");
+ } else if(properties.getSession() == null || properties.getSession().isEmpty()) {
+ throw new RuntimeException("authorization.security.session is missing!");
+ } else if(SecurityProperties.MODE == Mode.STATELESS) {
+ if(properties.getUserInfoUrl() == null || properties.getUserInfoUrl().isEmpty()) {
+ throw new RuntimeException("authorization.security.userInfoUrl is missing!");
+ }
+ } else if(SecurityProperties.MODE == Mode.REDIS) {
+ Redis redis = properties.getRedis();
+ if (redis.getHost() == null || redis.getHost().isEmpty()) {
+ throw new RuntimeException("authorization.security.redis.host is missing!");
+ } else if (redis.getPort() == null || redis.getPort().isEmpty()) {
+ throw new RuntimeException("authorization.security.redis.port is missing!");
+ }
+ }
+ }
+
+ public Map getProperties() {
+ Map map = new HashMap<>();
+ map.put("authorization.security.domain", properties.getDomain());
+ map.put("authorization.security.session", properties.getSession());
+ if(SecurityProperties.MODE == Mode.STATELESS) {
+ map.put("authorization.security.userInfoUrl", properties.getUserInfoUrl());
+ }
+ if(SecurityProperties.MODE == Mode.REDIS) {
+ map.put("authorization.security.redis.host", properties.getRedis().getHost());
+ map.put("authorization.security.redis.port", properties.getRedis().getPort());
+ map.put("authorization.security.redis.password", properties.getRedis().getPassword());
+ }
+ if (GlobalVars.date != null) {
+ map.put("Date of deploy", GlobalVars.date.toString());
+ }
+ if (globalVars.getBuildDate() != null) {
+ map.put("Date of build", globalVars.getBuildDate());
+ }
+ if (globalVars.getVersion() != null) {
+ map.put("Version", globalVars.getVersion());
+ }
+ return map;
+ }
}
diff --git a/src/main/java/eu/dnetlib/uoaauthorizationlibrary/SecurityConfiguration.java b/src/main/java/eu/dnetlib/uoaauthorizationlibrary/SecurityConfiguration.java
index 6bd815d..ba13b89 100644
--- a/src/main/java/eu/dnetlib/uoaauthorizationlibrary/SecurityConfiguration.java
+++ b/src/main/java/eu/dnetlib/uoaauthorizationlibrary/SecurityConfiguration.java
@@ -1,13 +1,10 @@
package eu.dnetlib.uoaauthorizationlibrary;
-import eu.dnetlib.uoaauthorizationlibrary.authorization.configuration.API;
-import eu.dnetlib.uoaauthorizationlibrary.authorization.configuration.GlobalVars;
-import eu.dnetlib.uoaauthorizationlibrary.authorization.configuration.Properties;
-import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
+import org.springframework.context.annotation.Import;
@Configuration
-@EnableConfigurationProperties({Properties.class, GlobalVars.class, API.class})
-@ComponentScan(basePackages = {"eu.dnetlib.uoaauthorizationlibrary.authorization"}, basePackageClasses = {WebSecurityConfig.class})
+@Import(AuthorizationConfiguration.class)
+@ComponentScan(basePackageClasses = {WebSecurityConfig.class})
public class SecurityConfiguration { }
diff --git a/src/main/java/eu/dnetlib/uoaauthorizationlibrary/authorization/configuration/Mode.java b/src/main/java/eu/dnetlib/uoaauthorizationlibrary/authorization/configuration/Mode.java
new file mode 100644
index 0000000..08132a8
--- /dev/null
+++ b/src/main/java/eu/dnetlib/uoaauthorizationlibrary/authorization/configuration/Mode.java
@@ -0,0 +1,5 @@
+package eu.dnetlib.uoaauthorizationlibrary.authorization.configuration;
+
+public enum Mode {
+ STATELESS, REDIS
+}
diff --git a/src/main/java/eu/dnetlib/uoaauthorizationlibrary/authorization/configuration/Properties.java b/src/main/java/eu/dnetlib/uoaauthorizationlibrary/authorization/configuration/SecurityProperties.java
similarity index 91%
rename from src/main/java/eu/dnetlib/uoaauthorizationlibrary/authorization/configuration/Properties.java
rename to src/main/java/eu/dnetlib/uoaauthorizationlibrary/authorization/configuration/SecurityProperties.java
index e80ae37..ccb5541 100644
--- a/src/main/java/eu/dnetlib/uoaauthorizationlibrary/authorization/configuration/Properties.java
+++ b/src/main/java/eu/dnetlib/uoaauthorizationlibrary/authorization/configuration/SecurityProperties.java
@@ -2,8 +2,11 @@ package eu.dnetlib.uoaauthorizationlibrary.authorization.configuration;
import org.springframework.boot.context.properties.ConfigurationProperties;
+
@ConfigurationProperties("authorization.security")
-public class Properties {
+public class SecurityProperties {
+
+ public static Mode MODE = Mode.STATELESS;
private Redis redis = new Redis();
private String userInfoUrl;
diff --git a/src/main/java/eu/dnetlib/uoaauthorizationlibrary/authorization/controllers/AuthorizationLibraryCheckDeployController.java b/src/main/java/eu/dnetlib/uoaauthorizationlibrary/authorization/controllers/AuthorizationLibraryCheckDeployController.java
index 6e37823..9310a1b 100644
--- a/src/main/java/eu/dnetlib/uoaauthorizationlibrary/authorization/controllers/AuthorizationLibraryCheckDeployController.java
+++ b/src/main/java/eu/dnetlib/uoaauthorizationlibrary/authorization/controllers/AuthorizationLibraryCheckDeployController.java
@@ -1,16 +1,15 @@
package eu.dnetlib.uoaauthorizationlibrary.authorization.controllers;
-import eu.dnetlib.uoaauthorizationlibrary.authorization.configuration.GlobalVars;
-import eu.dnetlib.uoaauthorizationlibrary.authorization.configuration.Properties;
+import eu.dnetlib.uoaauthorizationlibrary.AuthorizationConfiguration;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
-import java.util.HashMap;
import java.util.Map;
@RestController
@@ -18,37 +17,22 @@ import java.util.Map;
public class AuthorizationLibraryCheckDeployController {
private final Logger log = LogManager.getLogger(this.getClass());
- @Autowired
- private Properties securityConfig;
+ private final AuthorizationConfiguration configuration;
@Autowired
- private GlobalVars globalVars;
+ public AuthorizationLibraryCheckDeployController(AuthorizationConfiguration configuration) {
+ this.configuration = configuration;
+ }
@RequestMapping(value = {"","/", "/health_check"}, method = RequestMethod.GET)
- public String hello() {
+ public ResponseEntity hello() {
log.debug("Hello from uoa-authorization-library!");
- return "Hello from uoa-authorization-library!";
+ return ResponseEntity.ok("Hello from uoa-authorization-library!");
}
@PreAuthorize("hasAnyAuthority(@AuthorizationService.PORTAL_ADMIN)")
@RequestMapping(value = "/health_check/advanced", method = RequestMethod.GET)
- public Map checkEverything() {
- Map response = new HashMap<>();
- response.put("authorization.security.redis.host", securityConfig.getRedis().getHost());
- response.put("authorization.security.redis.port", securityConfig.getRedis().getPort());
- response.put("authorization.security.redis.password", securityConfig.getRedis().getPassword());
- response.put("authorization.security.userInfoUrl", securityConfig.getUserInfoUrl());
- response.put("authorization.security.session", securityConfig.getSession());
- response.put("authorization.security.domain", securityConfig.getDomain());
- if(GlobalVars.date != null) {
- response.put("Date of deploy", GlobalVars.date.toString());
- }
- if(globalVars.getBuildDate() != null) {
- response.put("Date of build", globalVars.getBuildDate());
- }
- if (globalVars.getVersion() != null) {
- response.put("Version", globalVars.getVersion());
- }
- return response;
+ public ResponseEntity