List to Map (streams)

@Getter @Setter class Directory { Long id; String name; }   List<Directory> l = repo.allDirectories();   Map<Long, String> m = l.stream() .collect(Collectors.toMap(d -> d.getId(), d -> d.getName()));@Getter @Setter class Directory { Long id; String name; } List<Directory> l = repo.allDirectories(); Map<Long, String> m = l.stream() .collect(Collectors.toMap(d -> d.getId(), d -> d.getName()));

@PreAuthorize and SpEL params

If @PreAuthorize is used on interface, parameter name (id) in SpEL must match parameter name in implementation (not required on interface – fileId). @Transactional public interface FileService {   @PreAuthorize("@securityService.canAccessFile(#id)") File getFile(Long fileId); }   /** * GOOD */ @Service public class FileServiceImpl implements FileService {   @Override public File getFile(Long id) { /* … Read more about @PreAuthorize and SpEL params[…]

SpEL in @Query (Spring Data)

@Query("select t from Test t where name = :#{#data.name} and type = :#{#data.type}") List<User> findByData(@Param("data") Data data);   public class Data { private String name; private DataType type; }@Query("select t from Test t where name = :#{#data.name} and type = :#{#data.type}") List<User> findByData(@Param("data") Data data); public class Data { private String name; private DataType type; Read more about SpEL in @Query (Spring Data)[…]

Map<String,String> in JPA Entity

@Entity @Table(name="product") public class Product extends AbstractEntity {   @Id @GeneratedValue(strategy=GenerationType.AUTO) private Long id;   @ElementCollection(fetch = FetchType.EAGER) @JoinTable(name="product_attribute", joinColumns=@JoinColumn(name="product_id")) @MapKeyColumn(name="key") @Column(name="value") private Map<String, String> attributes = new HashMap<String, String>();   // …   }@Entity @Table(name="product") public class Product extends AbstractEntity { @Id @GeneratedValue(strategy=GenerationType.AUTO) private Long id; @ElementCollection(fetch = FetchType.EAGER) @JoinTable(name="product_attribute", joinColumns=@JoinColumn(name="product_id")) @MapKeyColumn(name="key") @Column(name="value") private Read more about Map<String,String> in JPA Entity[…]

Active Directory/LDAP authentication with Spring Boot

package net.marioosh.ldapdemo;   import java.util.Arrays; import java.util.Collection;   import org.springframework.context.annotation.Configuration; import org.springframework.ldap.core.DirContextOperations; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.authority.SimpleGrantedAuthority; import org.springframework.security.ldap.userdetails.LdapAuthoritiesPopulator;   @Configuration public class SecurityConfig extends WebSecurityConfigurerAdapter {   @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .anyRequest() .fullyAuthenticated() .and().formLogin(); }   @Override public void configure(AuthenticationManagerBuilder auth) throws Exception { Read more about Active Directory/LDAP authentication with Spring Boot[…]

XML – check is well formed

public boolean isValid(String xmlFilePath) throws FileNotFoundException { FileInputStream in = new FileInputStream(xmlFilePath); try { XMLInputFactory factory = XMLInputFactory.newInstance(); XMLStreamReader staxXmlReader = factory.createXMLStreamReader(in); while(staxXmlReader.next() != XMLStreamConstants.END_DOCUMENT) {} return true; } catch (XMLStreamException e) { e.printStackTrace(); return false; } }public boolean isValid(String xmlFilePath) throws FileNotFoundException { FileInputStream in = new FileInputStream(xmlFilePath); try { XMLInputFactory factory = XMLInputFactory.newInstance(); Read more about XML – check is well formed[…]

Simple AES-128 encryption in Java

import java.io.ByteArrayOutputStream;   import javax.crypto.Cipher; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec;   /** * AES-128 */ public class EncryptionUtils {   /** * initialisation vector */ private final static byte[] IV = "thai8bai9voo1haV".getBytes();   /** * algorithm/mode/padding */ private final static String TRANSFORMATION = "AES/CBC/PKCS5Padding";   /** * * @param text encrypted text * @param password need Read more about Simple AES-128 encryption in Java[…]

Ignore SSL Certificate socket factory

import java.security.SecureRandom; import java.security.cert.X509Certificate;   import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.SSLContext; import javax.net.ssl.SSLSocketFactory; import javax.net.ssl.TrustManager; import javax.net.ssl.X509TrustManager;   …   SSLContext sc = SSLContext.getInstance("TLS"); sc.init(null, new TrustManager[] { new X509TrustManager() { public X509Certificate[] getAcceptedIssuers() { return null; } public void checkClientTrusted(X509Certificate[] certs, String authType) {} public void checkServerTrusted(X509Certificate[] certs, String authType) {} } }, new SecureRandom()); SSLSocketFactory Read more about Ignore SSL Certificate socket factory[…]