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[…]

@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[…]

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

LOGBack: Short console log pattern

# to console without date logging.pattern.console=%-5level [%30.30logger{30}:%4.4line] %msg%n # to file with date logging.pattern.file=%-5level %d{yyyy-MM-dd HH:mm:ss.SSS} [%30.30logger{30}:%4.4line] %msg%n# to console without date logging.pattern.console=%-5level [%30.30logger{30}:%4.4line] %msg%n # to file with date logging.pattern.file=%-5level %d{yyyy-MM-dd HH:mm:ss.SSS} [%30.30logger{30}:%4.4line] %msg%n

Spring Annotation Based Single Class Web Application with JSP View Resolver

Works on Servlet 3.0+ containers. Better not mix Configuration classes with Controller classes. It is only for simplicity ­čśë import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.ServletRegistration;   import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.WebApplicationInitializer; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; import org.springframework.web.servlet.DispatcherServlet; import org.springframework.web.servlet.ViewResolver; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.view.InternalResourceViewResolver; import org.springframework.web.servlet.view.JstlView;   @Configuration @EnableWebMvc @Controller public Read more about Spring Annotation Based Single Class Web Application with JSP View Resolver[…]

Spring Annotation Based Single Class Web Application

Works on Servlet 3.0+ containers. Better not mix Configuration classes with Controller classes. It is only for simplicity ­čśë import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.ServletRegistration;   import org.springframework.context.annotation.Configuration; import org.springframework.stereotype.Controller; import org.springframework.web.WebApplicationInitializer; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; import org.springframework.web.servlet.DispatcherServlet; import org.springframework.web.servlet.config.annotation.EnableWebMvc;   @Configuration @EnableWebMvc @Controller public class SpringWebApp implements WebApplicationInitializer {   @RequestMapping("/") @ResponseBody Read more about Spring Annotation Based Single Class Web Application[…]

GWT 2.4 + Spring MVC 3.0

GWT 2.4 + Spring MVC 3.0 (+ Hibernate) – Poni┼╝ej kilka podej┼Ť─ç i ich przyk┼éadowe implementacje gotowe do u┼╝ycia. 1. Metoda najmniej inwazyjna – wg mnie najlepsza https://github.com/marioosh-net/gwt-spring-non-invasive 2. Metoda z wykorzystaniem klasy kt├│ra jednocze┼Ťnie funkcjonuje jako RemoteServiceServlet i Spring MVC Controller https://github.com/marioosh-net/gwt-spring 3. Przy u┼╝yciu: GWT Server Library https://github.com/marioosh-net/gwt-spring-sl

Spring: Spring Security + reCaptcha

Mo┼╝e niezbyt eleganckie rozwi─ůzanie, ale dzia┼éa. Przedstawione rozwi─ůzanie jest oparte na Spring Web Flow + JSF 2.0. Na wejsciu do strony logowania generowany jest kod html reCaptcha. Tutaj fragment konfiguracji flow’a: 1 2 3 4 5 6 7 <view-state id="login"> <on-render> <evaluate expression="reCaptcha.createRecaptchaHtml(null,null)" result="flowScope.captcha"/> </on-render> <transition on="login"/> <transition on="resetpass" to="resetpassword"/> </view-state><view-state id="login"> <on-render> <evaluate expression="reCaptcha.createRecaptchaHtml(null,null)" Read more about Spring: Spring Security + reCaptcha[…]

Spring: Upload pliku w Spring Web Flow

xhtml: 1 2 3 4 <h:form enctype="multipart/form-data"> <input type="file" name="file" /> <h:commandButton action="upload" value="Ok" /> </h:form><h:form enctype="multipart/form-data"> <input type="file" name="file" /> <h:commandButton action="upload" value="Ok" /> </h:form> beans.xml: 1 2 3 <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize" value="100000"/> </bean><bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize" value="100000"/> </bean> flow.xml: 1 2 3 4 5 6 7 8 9 10 11 Read more about Spring: Upload pliku w Spring Web Flow[…]