#63 renamed the base package of the api module

This commit is contained in:
Fabio Formosa
2022-10-06 23:54:04 +02:00
parent 6972915a5c
commit 304a1e7f71
105 changed files with 463 additions and 481 deletions

View File

@@ -1,11 +1,11 @@
package it.fabioformosa.quartzmanager.security;
package it.fabioformosa.quartzmanager.api.security;
import com.fasterxml.jackson.databind.ObjectMapper;
import it.fabioformosa.quartzmanager.common.config.QuartzManagerPaths;
import it.fabioformosa.quartzmanager.security.helpers.LoginConfigurer;
import it.fabioformosa.quartzmanager.security.helpers.impl.*;
import it.fabioformosa.quartzmanager.security.properties.InMemoryAccountProperties;
import it.fabioformosa.quartzmanager.security.properties.JwtSecurityProperties;
import it.fabioformosa.quartzmanager.api.common.config.QuartzManagerPaths;
import it.fabioformosa.quartzmanager.api.security.helpers.LoginConfigurer;
import it.fabioformosa.quartzmanager.api.security.helpers.impl.*;
import it.fabioformosa.quartzmanager.api.security.properties.InMemoryAccountProperties;
import it.fabioformosa.quartzmanager.api.security.properties.JwtSecurityProperties;
import org.apache.commons.lang3.BooleanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
@@ -32,8 +32,8 @@ import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.CorsConfigurationSource;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import static it.fabioformosa.quartzmanager.common.config.QuartzManagerPaths.QUARTZ_MANAGER_LOGIN_PATH;
import static it.fabioformosa.quartzmanager.common.config.QuartzManagerPaths.QUARTZ_MANAGER_LOGOUT_PATH;
import static it.fabioformosa.quartzmanager.api.common.config.QuartzManagerPaths.QUARTZ_MANAGER_LOGIN_PATH;
import static it.fabioformosa.quartzmanager.api.common.config.QuartzManagerPaths.QUARTZ_MANAGER_LOGOUT_PATH;
/**
* @author Fabio.Formosa

View File

@@ -1,8 +1,8 @@
package it.fabioformosa.quartzmanager.security.controllers;
package it.fabioformosa.quartzmanager.api.security.controllers;
import io.swagger.v3.oas.annotations.Hidden;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import it.fabioformosa.quartzmanager.common.config.OpenAPIConfigConsts;
import it.fabioformosa.quartzmanager.api.common.config.OpenAPIConfigConsts;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
@@ -12,7 +12,7 @@ import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import static it.fabioformosa.quartzmanager.common.config.QuartzManagerPaths.QUARTZ_MANAGER_AUTH_PATH;
import static it.fabioformosa.quartzmanager.api.common.config.QuartzManagerPaths.QUARTZ_MANAGER_AUTH_PATH;
@RestController
@Hidden

View File

@@ -1,21 +1,21 @@
package it.fabioformosa.quartzmanager.security.helpers;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
/**
* It configures filters to authenticate credentials sent by client or to set authenticationSuccessHandler
*
* Implement this interface for a login strategy
*
*/
public interface LoginConfigurer {
/**
* If the authentication is based on cookie, it returns the name of cookie to be erased at the logout
*/
String cookieMustBeDeletedAtLogout();
HttpSecurity login(String loginPath, HttpSecurity http, AuthenticationManager authenticationManager) throws Exception;
}
package it.fabioformosa.quartzmanager.api.security.helpers;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
/**
* It configures filters to authenticate credentials sent by client or to set authenticationSuccessHandler
*
* Implement this interface for a login strategy
*
*/
public interface LoginConfigurer {
/**
* If the authentication is based on cookie, it returns the name of cookie to be erased at the logout
*/
String cookieMustBeDeletedAtLogout();
HttpSecurity login(String loginPath, HttpSecurity http, AuthenticationManager authenticationManager) throws Exception;
}

View File

@@ -1,45 +1,45 @@
package it.fabioformosa.quartzmanager.security.helpers.impl;
import org.springframework.security.authentication.AbstractAuthenticationToken;
public class AnonAuthentication extends AbstractAuthenticationToken {
private static final long serialVersionUID = 1L;
public AnonAuthentication() {
super( null );
}
@Override
public boolean equals( Object obj ) {
if ( this == obj )
return true;
if ( obj == null )
return false;
if ( getClass() != obj.getClass() )
return false;
return true;
}
@Override
public Object getCredentials() {
return null;
}
@Override
public Object getPrincipal() {
return null;
}
@Override
public int hashCode() {
int hash = 7;
return hash;
}
@Override
public boolean isAuthenticated() {
return true;
}
}
package it.fabioformosa.quartzmanager.api.security.helpers.impl;
import org.springframework.security.authentication.AbstractAuthenticationToken;
public class AnonAuthentication extends AbstractAuthenticationToken {
private static final long serialVersionUID = 1L;
public AnonAuthentication() {
super( null );
}
@Override
public boolean equals( Object obj ) {
if ( this == obj )
return true;
if ( obj == null )
return false;
if ( getClass() != obj.getClass() )
return false;
return true;
}
@Override
public Object getCredentials() {
return null;
}
@Override
public Object getPrincipal() {
return null;
}
@Override
public int hashCode() {
int hash = 7;
return hash;
}
@Override
public boolean isAuthenticated() {
return true;
}
}

View File

@@ -1,20 +1,20 @@
package it.fabioformosa.quartzmanager.security.helpers.impl;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler;
public class AuthenticationFailureHandler extends SimpleUrlAuthenticationFailureHandler {
@Override
public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response,
AuthenticationException exception) throws IOException, ServletException {
super.onAuthenticationFailure(request, response, exception);
}
}
package it.fabioformosa.quartzmanager.api.security.helpers.impl;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler;
public class AuthenticationFailureHandler extends SimpleUrlAuthenticationFailureHandler {
@Override
public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response,
AuthenticationException exception) throws IOException, ServletException {
super.onAuthenticationFailure(request, response, exception);
}
}

View File

@@ -1,4 +1,4 @@
package it.fabioformosa.quartzmanager.security.helpers.impl;
package it.fabioformosa.quartzmanager.api.security.helpers.impl;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler;

View File

@@ -1,4 +1,4 @@
package it.fabioformosa.quartzmanager.security.helpers.impl;
package it.fabioformosa.quartzmanager.api.security.helpers.impl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -6,7 +6,7 @@ import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configurers.FormLoginConfigurer;
import it.fabioformosa.quartzmanager.security.helpers.LoginConfigurer;
import it.fabioformosa.quartzmanager.api.security.helpers.LoginConfigurer;
/**
* It delegates the login to the @FormLoginConfigurer of the httpSecurity.

View File

@@ -1,4 +1,4 @@
package it.fabioformosa.quartzmanager.security.helpers.impl;
package it.fabioformosa.quartzmanager.api.security.helpers.impl;
import javax.servlet.FilterChain;
import javax.servlet.http.HttpServletRequest;

View File

@@ -1,8 +1,8 @@
package it.fabioformosa.quartzmanager.security.helpers.impl;
package it.fabioformosa.quartzmanager.api.security.helpers.impl;
import com.fasterxml.jackson.databind.ObjectMapper;
import it.fabioformosa.quartzmanager.security.properties.JwtSecurityProperties;
import it.fabioformosa.quartzmanager.security.models.UserTokenState;
import it.fabioformosa.quartzmanager.api.security.properties.JwtSecurityProperties;
import it.fabioformosa.quartzmanager.api.security.models.UserTokenState;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

View File

@@ -1,4 +1,4 @@
package it.fabioformosa.quartzmanager.security.helpers.impl;
package it.fabioformosa.quartzmanager.api.security.helpers.impl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

View File

@@ -1,42 +1,42 @@
package it.fabioformosa.quartzmanager.security.helpers.impl;
import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.security.core.userdetails.UserDetails;
public class JwtTokenBasedAuthentication extends AbstractAuthenticationToken {
private static final long serialVersionUID = 1L;
private String token;
private final UserDetails principle;
public JwtTokenBasedAuthentication(UserDetails principle) {
super(principle.getAuthorities());
this.principle = principle;
}
@Override
public Object getCredentials() {
return token;
}
@Override
public UserDetails getPrincipal() {
return principle;
}
public String getToken() {
return token;
}
@Override
public boolean isAuthenticated() {
return true;
}
public void setToken( String token ) {
this.token = token;
}
}
package it.fabioformosa.quartzmanager.api.security.helpers.impl;
import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.security.core.userdetails.UserDetails;
public class JwtTokenBasedAuthentication extends AbstractAuthenticationToken {
private static final long serialVersionUID = 1L;
private String token;
private final UserDetails principle;
public JwtTokenBasedAuthentication(UserDetails principle) {
super(principle.getAuthorities());
this.principle = principle;
}
@Override
public Object getCredentials() {
return token;
}
@Override
public UserDetails getPrincipal() {
return principle;
}
public String getToken() {
return token;
}
@Override
public boolean isAuthenticated() {
return true;
}
public void setToken( String token ) {
this.token = token;
}
}

View File

@@ -1,9 +1,9 @@
package it.fabioformosa.quartzmanager.security.helpers.impl;
package it.fabioformosa.quartzmanager.api.security.helpers.impl;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import it.fabioformosa.quartzmanager.security.properties.JwtSecurityProperties;
import it.fabioformosa.quartzmanager.api.security.properties.JwtSecurityProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

View File

@@ -1,4 +1,4 @@
package it.fabioformosa.quartzmanager.security.helpers.impl;
package it.fabioformosa.quartzmanager.api.security.helpers.impl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -9,7 +9,7 @@ import org.springframework.security.web.authentication.preauth.AbstractPreAuthen
import org.springframework.security.web.util.matcher.RegexRequestMatcher;
import org.springframework.web.filter.GenericFilterBean;
import it.fabioformosa.quartzmanager.security.helpers.LoginConfigurer;
import it.fabioformosa.quartzmanager.api.security.helpers.LoginConfigurer;
/**
* It adds a new filter @JwtAuthenticationFilter after @AbstractPreAuthenticatedProcessingFilter that match login path

View File

@@ -1,36 +1,36 @@
package it.fabioformosa.quartzmanager.security.helpers.impl;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import com.fasterxml.jackson.databind.ObjectMapper;
public class LogoutSuccess implements LogoutSuccessHandler {
private final ObjectMapper objectMapper;
public LogoutSuccess(ObjectMapper objectMapper) {
super();
this.objectMapper = objectMapper;
}
@Override
public void onLogoutSuccess(HttpServletRequest httpServletRequest, HttpServletResponse response, Authentication authentication)
throws IOException, ServletException {
Map<String, String> result = new HashMap<>();
result.put( "result", "success" );
response.setContentType("application/json");
response.getWriter().write(objectMapper.writeValueAsString(result));
response.setStatus(HttpServletResponse.SC_OK);
}
}
package it.fabioformosa.quartzmanager.api.security.helpers.impl;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import com.fasterxml.jackson.databind.ObjectMapper;
public class LogoutSuccess implements LogoutSuccessHandler {
private final ObjectMapper objectMapper;
public LogoutSuccess(ObjectMapper objectMapper) {
super();
this.objectMapper = objectMapper;
}
@Override
public void onLogoutSuccess(HttpServletRequest httpServletRequest, HttpServletResponse response, Authentication authentication)
throws IOException, ServletException {
Map<String, String> result = new HashMap<>();
result.put( "result", "success" );
response.setContentType("application/json");
response.getWriter().write(objectMapper.writeValueAsString(result));
response.setStatus(HttpServletResponse.SC_OK);
}
}

View File

@@ -1,4 +1,4 @@
package it.fabioformosa.quartzmanager.security.helpers.impl;
package it.fabioformosa.quartzmanager.api.security.helpers.impl;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.SecurityConfigurerAdapter;
@@ -7,7 +7,7 @@ import org.springframework.security.config.annotation.web.configurers.LogoutConf
import org.springframework.security.web.DefaultSecurityFilterChain;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
import it.fabioformosa.quartzmanager.security.helpers.LoginConfigurer;
import it.fabioformosa.quartzmanager.api.security.helpers.LoginConfigurer;
/**
* It wraps the httpSecurity to provide new function as login and logout

View File

@@ -1,23 +1,23 @@
package it.fabioformosa.quartzmanager.security.helpers.impl;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;
@Component
public class RestAuthenticationEntryPoint implements AuthenticationEntryPoint {
@Override
public void commence(HttpServletRequest request,
HttpServletResponse response,
AuthenticationException authException) throws IOException {
response.sendError(HttpServletResponse.SC_UNAUTHORIZED, authException.getMessage());
}
}
package it.fabioformosa.quartzmanager.api.security.helpers.impl;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;
@Component
public class RestAuthenticationEntryPoint implements AuthenticationEntryPoint {
@Override
public void commence(HttpServletRequest request,
HttpServletResponse response,
AuthenticationException authException) throws IOException {
response.sendError(HttpServletResponse.SC_UNAUTHORIZED, authException.getMessage());
}
}

View File

@@ -1,32 +1,32 @@
package it.fabioformosa.quartzmanager.security.models;
public class UserTokenState {
private String access_token;
private Long expires_in_sec;
public UserTokenState() {
this.access_token = null;
this.expires_in_sec = null;
}
public UserTokenState(String access_token, long expires_in_sec) {
this.access_token = access_token;
this.expires_in_sec = expires_in_sec;
}
public String getAccess_token() {
return access_token;
}
public Long getExpires_in_sec() {
return expires_in_sec;
}
public void setAccess_token(String access_token) {
this.access_token = access_token;
}
public void setExpires_in_sec(Long expires_in_sec) {
this.expires_in_sec = expires_in_sec;
}
}
package it.fabioformosa.quartzmanager.api.security.models;
public class UserTokenState {
private String access_token;
private Long expires_in_sec;
public UserTokenState() {
this.access_token = null;
this.expires_in_sec = null;
}
public UserTokenState(String access_token, long expires_in_sec) {
this.access_token = access_token;
this.expires_in_sec = expires_in_sec;
}
public String getAccess_token() {
return access_token;
}
public Long getExpires_in_sec() {
return expires_in_sec;
}
public void setAccess_token(String access_token) {
this.access_token = access_token;
}
public void setExpires_in_sec(Long expires_in_sec) {
this.expires_in_sec = expires_in_sec;
}
}

View File

@@ -1,2 +1,2 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
it.fabioformosa.quartzmanager.security.WebSecurityConfigJWT
it.fabioformosa.quartzmanager.api.security.WebSecurityConfigJWT

View File

@@ -1,13 +1,13 @@
package it.fabioformosa.quartzmanager.security;
package it.fabioformosa.quartzmanager.api.security;
import it.fabioformosa.quartzmanager.security.models.UserTokenState;
import it.fabioformosa.quartzmanager.api.security.models.UserTokenState;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.*;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import static it.fabioformosa.quartzmanager.common.config.QuartzManagerPaths.QUARTZ_MANAGER_LOGIN_PATH;
import static it.fabioformosa.quartzmanager.api.common.config.QuartzManagerPaths.QUARTZ_MANAGER_LOGIN_PATH;
public class AbstractSecurityLoginTest {
@Autowired

View File

@@ -1,6 +1,7 @@
package it.fabioformosa.quartzmanager.security;
package it.fabioformosa.quartzmanager.api.security;
import it.fabioformosa.quartzmanager.security.controllers.TestController;
import it.fabioformosa.quartzmanager.api.common.config.QuartzManagerPaths;
import it.fabioformosa.quartzmanager.api.security.controllers.TestController;
import org.hamcrest.core.IsNot;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
@@ -13,7 +14,6 @@ import org.springframework.test.context.TestPropertySource;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import static it.fabioformosa.quartzmanager.common.config.QuartzManagerPaths.QUARTZ_MANAGER_LOGIN_PATH;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@SpringBootTest
@@ -64,7 +64,7 @@ public class SecurityControllerTest {
@Test
void givenAnAnonymousUser_whenCalledTheLoginPath_thenShouldReturn2xx() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.post(QUARTZ_MANAGER_LOGIN_PATH)
mockMvc.perform(MockMvcRequestBuilders.post(QuartzManagerPaths.QUARTZ_MANAGER_LOGIN_PATH)
.contentType("application/x-www-form-urlencoded")
.accept("application/json")
.param("username", "foo")

View File

@@ -1,6 +1,6 @@
package it.fabioformosa.quartzmanager.security;
package it.fabioformosa.quartzmanager.api.security;
import it.fabioformosa.quartzmanager.security.models.UserTokenState;
import it.fabioformosa.quartzmanager.api.security.models.UserTokenState;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

View File

@@ -1,6 +1,6 @@
package it.fabioformosa.quartzmanager.security;
package it.fabioformosa.quartzmanager.api.security;
import it.fabioformosa.quartzmanager.security.models.UserTokenState;
import it.fabioformosa.quartzmanager.api.security.models.UserTokenState;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

View File

@@ -1,6 +1,6 @@
package it.fabioformosa.quartzmanager.security;
package it.fabioformosa.quartzmanager.api.security;
import it.fabioformosa.quartzmanager.security.models.UserTokenState;
import it.fabioformosa.quartzmanager.api.security.models.UserTokenState;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

View File

@@ -1,6 +1,6 @@
package it.fabioformosa.quartzmanager.security;
package it.fabioformosa.quartzmanager.api.security;
import it.fabioformosa.quartzmanager.security.models.UserTokenState;
import it.fabioformosa.quartzmanager.api.security.models.UserTokenState;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

View File

@@ -1,4 +1,4 @@
package it.fabioformosa.quartzmanager.security;
package it.fabioformosa.quartzmanager.api.security;
import org.springframework.boot.autoconfigure.SpringBootApplication;

View File

@@ -1,4 +1,4 @@
package it.fabioformosa.quartzmanager.security.controllers;
package it.fabioformosa.quartzmanager.api.security.controllers;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;