Skip to content

Instantly share code, notes, and snippets.

View GaetanoPiazzolla's full-sized avatar
🧐
Crafting for Fun

Gaetano Piazzolla GaetanoPiazzolla

🧐
Crafting for Fun
View GitHub Profile
@GaetanoPiazzolla
GaetanoPiazzolla / Boilerplate.java
Created March 21, 2022 18:01
Boilerplate code to start working with the Form and the Drive APIs from Google
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.drive.Drive;
import com.google.api.services.forms.v1.Forms;
import java.io.IOException;
import java.security.GeneralSecurityException;
public class Boilerplate {
@GaetanoPiazzolla
GaetanoPiazzolla / CacheConfig.java
Created January 30, 2022 20:07
RedisCacheConfiguration with TTL for the blacklist cache.
@Configuration
@AutoConfigureAfter(RedisAutoConfiguration.class)
public class CacheConfig {
public final static String BLACKLIST_CACHE_NAME = "jwt-black-list";
@Value("${jwt.duration:0}")
private int tokenDuration;
@Value("${spring.redis.host:localhost}")
private String redisHost;
@GaetanoPiazzolla
GaetanoPiazzolla / BlackListingService.java
Created January 30, 2022 19:52
A blacklisting service. Uses standard springboot annotations.
@Service
public class BlackListingService {
@CachePut(CacheConfig.BLACKLIST_CACHE_NAME)
public String blackListJwt(String jwt) {
return jwt;
}
@Cacheable(value = CacheConfig.BLACKLIST_CACHE_NAME, unless = "#result == null")
public String getJwtBlackList(String jwt) {
@GaetanoPiazzolla
GaetanoPiazzolla / LogoutController.java
Created January 30, 2022 19:51
LogoutController which writes on RedisCache
@RestController
@RequestMapping("/logout")
public class LogoutController {
@Autowired
private BlackListingService blackListingService;
@Autowired
private UserRequestScopedBean userRequestScopedBean;
@GaetanoPiazzolla
GaetanoPiazzolla / JWTInterceptor.java
Created January 30, 2022 19:49
JWT Interceptor used to authorize requests.
public class JWTInterceptor implements HandlerInterceptor {
@Value("${jwt.key}")
private String jwtKey;
@Autowired
private BlackListingService blackListingService;
@Autowired
private UserRequestScopedBean userRequestScopedBean;
public class Resurrected {
private static final Set<Resurrected> resurrected = new HashSet<>();
@Override
protected void finalize() throws Throwable {
resurrected.add(this); // Resurrect the object by creating a new reference
}
}
@Override
protected void finalize() throws Throwable {
// closing resources here....
super.finalize();
}
FileInputStream input = null;
try {
input = new FileInputStream(file1);
input.close(); input = null;
} finally {
if (input != null) input.close();
}
sessionStorage.setItem('pets', '{"pets":{"fido":{"species":"dog"}}}');
const store = createStore();
const persister = createSessionPersister(store, 'pets');
await persister.load();
console.log(store.getTables());
// -> {pets: {fido: {species: 'dog'}}}
sessionStorage.clear();
const store = createStore().setTable('pets', {
fido: {species: 'dog'},
felix: {species: 'cat'},
cujo: {species: 'dog'},
});
const indexes = createIndexes(store);
indexes.setIndexDefinition(
'bySpecies', // indexId
'pets', // tableId to index
'species', // cellId to index