Skip to content

Instantly share code, notes, and snippets.

@luismoramedina
luismoramedina / WebSecurity.java
Last active January 22, 2023 14:22
Spring security - Disable security for OPTIONS (CORS)
@Configuration
public class WebSecurity extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
.authorizeRequests()
.antMatchers(HttpMethod.OPTIONS, "**").permitAll()//allow CORS option calls
.anyRequest().authenticated();
@luismoramedina
luismoramedina / BackgroundController.java
Created May 29, 2017 12:24
Background task in controller
@RequestMapping("/job/start")
public String start() throws ExecutionException, InterruptedException {
ExecutorService executor = Executors.newSingleThreadExecutor();
executor.submit(() -> {
try {
System.out.println("BGController.before");
Thread.sleep(10000);
System.out.println("BGController.after");
} catch (InterruptedException e) {
@luismoramedina
luismoramedina / ExtractPublicKeyFromCer.java
Created January 30, 2017 10:11
Extract public key from x509 certificate
String cer = "/home/luis/Downloads/APImIntranet.cer";
FileInputStream fileInputStream = new FileInputStream(cer);
CertificateFactory cf = CertificateFactory.getInstance("X.509");
X509Certificate cert = (X509Certificate)cf.generateCertificate(fileInputStream);
PublicKey publicKey = cert.getPublicKey();
byte[] encoded = publicKey.getEncoded();
byte[] b64key = Base64.getEncoder().encode(encoded);
System.out.println("b64key = " + new String(b64key));
@luismoramedina
luismoramedina / proxy.sh
Created November 22, 2016 11:46
Squid proxy switcher
#!/bin/bash
# use: proxy.sh on|off
cp /etc/squid/squid.conf.proxy-$1 /etc/squid/squid.conf
echo "restarting squid"
service squid restart
echo 'Proxy ' $1
@luismoramedina
luismoramedina / cloud-config-client-snippet.js
Created October 3, 2016 15:50
Connection to cloud config from nodejs
"use strict";
var client = require("cloud-config-client");
const options = {
application: "devstack-sample-persons",
profiles: ["production"]
};
client.load(options, function(error, cfg) {
@luismoramedina
luismoramedina / setbackground.groovy
Created August 10, 2016 14:44
set background gopro photo of the day ubuntu
#!/usr/bin/env groovy
def proxyParams = ""
def jsonUri = "https://api.gopro.com/v2/channels/feed/playlists/photo-of-the-day.json?platform=web"
//def data = get(jsonUri)
def commandJson = "wget -q -O - " + jsonUri + proxyParams
def data = commandJson.execute().text
json = new groovy.json.JsonSlurper().parseText(data)
@luismoramedina
luismoramedina / httpget.go
Created July 22, 2016 21:58
http connection in go
package main
import "fmt"
import "net/http"
func main() {
resp, error := http.Get("https://www.google.com/")
fmt.Println(resp)
fmt.Println(error)
}
@luismoramedina
luismoramedina / args.go
Created July 22, 2016 21:58
go command line
package main
import (
"flag"
"fmt"
)
func main() {
var cmd string
var i bool
@luismoramedina
luismoramedina / cli.groovy
Last active July 22, 2016 16:28
Groovy argument parse
#!/usr/bin/env groovy
def cli = new CliBuilder(usage: 'cli', stopAtNonOption: false)
cli.n('Force numeric on property set')
cli.h(longOpt: 'help', 'Show usage information')
cli.c(longOpt: 'config-server', args:1, argName:'server', 'Config server url')
cli.p(longOpt: 'profile', args:1, argName:'profile', 'Profile')
cli.l(longOpt: 'label', args:1, argName:'label', 'Label')
cli.u(longOpt: 'credentials', args:1, argName:'user:pass', 'Basic credentials')
@luismoramedina
luismoramedina / heavy_services.js
Created June 14, 2016 11:30
Emulates a heavy nodejs delayed rest service
http = require("http");
function sleep(milliSeconds, callback) {
var startTime = new Date().getTime();
while (new Date().getTime() < startTime + milliSeconds);
}
server = http.createServer(function (request, response) {
console.log(request);
console.log('a request');