marioosh.net

mini blog / notes

AES-128 in js/Node.js

const iv = Buffer.from(’aeghei1Di8tieNg0’);   exports.encrypt = function(text, password){ try { var cipher = crypto.createCipheriv(’aes-128-cbc’,password,iv); var crypted = cipher.update(text,’utf8′,’hex’); crypted += cipher.final(’hex’); return crypted; } catch (err) { console.error(’encrypt error’,err); return null; } };   exports.decrypt = function(text, password){ try { var decipher = crypto.createDecipheriv(’aes-128-cbc’,password,iv); var dec = decipher.update(text,’hex’,’utf8’); dec += decipher.final(’utf8’); return dec; } Read more about AES-128 in js/Node.js[…]

@PreAuthorize with use Spring Expression Language (SpEL) and service method call

public interface EventRepository extends JpaRepository<Event, Long> {   @PreAuthorize("@securityService.canSave(#event,principal)") Event save(@Param("event") Event event);   @PreAuthorize("@securityService.canSave2(#event)") Event save2(@Param("event") Event event); }   @Service("securityService") public class SecurityService {   public boolean canSave(Event event, UserDetails principal) { // some logic here }   public boolean canSave2(Event event) {   // get principal from Security Context Object principal = Read more about @PreAuthorize with use Spring Expression Language (SpEL) and service method call[…]

AsyncPipe example

@Component({ … }) export class TestingComponent implements OnInit { units: any; constructor(private restService:RestService) { } ngOnInit() { this.units = this.restService.getUnits(); } }@Component({ … }) export class TestingComponent implements OnInit { units: any; constructor(private restService:RestService) { } ngOnInit() { this.units = this.restService.getUnits(); } } Template <ng-template #loading>Loading…</ng-template> <div *ngIf="units|async as units; else loading"> <span *ngFor="let unit Read more about AsyncPipe example[…]

JSON -> List

import org.springframework.core.io.Resource; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper;   …   @Value("classpath:/strings.json") private Resource jsonFile;   @Autowired private ObjectMapper mapper;   …   List<String> listOfStrings = Arrays.asList( mapper.readValue(jsonFile.getInputStream(), String[].class) );import org.springframework.core.io.Resource; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; … @Value("classpath:/strings.json") private Resource jsonFile; @Autowired private ObjectMapper mapper; … List<String> listOfStrings = Arrays.asList( mapper.readValue(jsonFile.getInputStream(), String[].class) );

Spring Boot: run WAR/JAR with specific profiles and/or port

Running from .war/.jar: java -Dspring.profiles.active=dev,other -Dserver.port=3000 -jar spring-app.warjava -Dspring.profiles.active=dev,other -Dserver.port=3000 -jar spring-app.war or java -Dspring-boot.run.profiles=dev,other -Dserver.port=3000 -jar spring-app.warjava -Dspring-boot.run.profiles=dev,other -Dserver.port=3000 -jar spring-app.war Running from Maven project (Spring Boot 2.x): mvn spring-boot:run -Dspring-boot.run.profiles=dev,other -Dspring-boot.run.arguments=–server.port=3000mvn spring-boot:run -Dspring-boot.run.profiles=dev,other -Dspring-boot.run.arguments=–server.port=3000 Spring Profiles

Angular: Reference variable in templates

Inside templates: <input type="text" #name> <input type="text" #surname> <button (click)="someMethod(name,surname)">Test</button><input type="text" #name> <input type="text" #surname> <button (click)="someMethod(name,surname)">Test</button> Inside component: import {ViewChild, ElementRef} from ‘@angular/core’; // … @ViewChild(’name’) nameRef: ElementRef;import {ViewChild, ElementRef} from ‘@angular/core’; // … @ViewChild(‘name’) nameRef: ElementRef;