How to manually log out a user with spring security?

It’s hard for me to say for sure if your code is enough. However standard Spring-security’s implementation of logging out is different. If you took a look at SecurityContextLogoutHandler you would see they do: SecurityContextHolder.clearContext(); Moreover they optionally invalidate the HttpSession: if (invalidateHttpSession) { HttpSession session = request.getSession(false); if (session != null) { session.invalidate(); } … Read more

Spring 5 WebClient using ssl

See example of use insecure TrustManagerFactory that trusts all X.509 certificates (including self-signed) without any verification. The important note from documentation: Never use this TrustManagerFactory in production. It is purely for testing purposes, and thus it is very insecure. @Bean public WebClient createWebClient() throws SSLException { SslContext sslContext = SslContextBuilder .forClient() .trustManager(InsecureTrustManagerFactory.INSTANCE) .build(); ClientHttpConnector httpConnector … Read more

@Value not resolved when using @PropertySource annotation. How to configure PropertySourcesPlaceholderConfigurer?

as @cwash said; @Configuration @PropertySource(“classpath:/test-config.properties”) public class TestConfig { @Value(“${name}”) public String name; //You need this @Bean public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() { return new PropertySourcesPlaceholderConfigurer(); } }

Where to put static files such as CSS in a spring-boot project?

Anywhere beneath src/main/resources/static is an appropriate place for static content such as CSS, JavaScript, and images. The static directory is served from /. For example, src/main/resources/static/signin.css will be served from /signin.css whereas src/main/resources/static/css/signin.css will be served from /css/signin.css. The src/main/resources/templates folder is intended for view templates that will be turned into HTML by a templating … Read more

How to get access to HTTP header information in Spring MVC REST controller?

When you annotate a parameter with @RequestHeader, the parameter retrieves the header information. So you can just do something like this: @RequestHeader(“Accept”) to get the Accept header. So from the documentation: @RequestMapping(“/displayHeaderInfo.do”) public void displayHeaderInfo(@RequestHeader(“Accept-Encoding”) String encoding, @RequestHeader(“Keep-Alive”) long keepAlive) { } The Accept-Encoding and Keep-Alive header values are provided in the encoding and keepAlive … Read more

Spring injects dependencies in constructor without @Autowired annotation

Starting with Spring 4.3, if a class, which is configured as a Spring bean, has only one constructor, the @Autowired annotation can be omitted and Spring will use that constructor and inject all necessary dependencies. Regarding the default constructor: You either need the default constructor, a constructor with the @Autowired annotation when you have multiple … Read more

Spring Boot: Is it possible to use external application.properties files in arbitrary directories with a fat jar?

If you have not changed the defaults of Spring Boot (meaning you are using @EnableAutoConfiguration or @SpringBootApplication and have not changed any Property Source handling), then it will look for properties with the following order (highest overrides lowest): A /config subdir of the current directory The current directory A classpath /config package The classpath root … Read more

Access Https Rest Service using Spring RestTemplate

KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType()); keyStore.load(new FileInputStream(new File(keyStoreFile)), keyStorePassword.toCharArray()); SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory( new SSLContextBuilder() .loadTrustMaterial(null, new TrustSelfSignedStrategy()) .loadKeyMaterial(keyStore, keyStorePassword.toCharArray()) .build(), NoopHostnameVerifier.INSTANCE); HttpClient httpClient = HttpClients.custom().setSSLSocketFactory( socketFactory).build(); ClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory( httpClient); RestTemplate restTemplate = new RestTemplate(requestFactory); MyRecord record = restTemplate.getForObject(uri, MyRecord.class); LOG.debug(record.toString());

Spring Data: “delete by” is supported?

Deprecated answer (Spring Data JPA <=1.6.x): @Modifying annotation to the rescue. You will need to provide your custom SQL behaviour though. public interface UserRepository extends JpaRepository<User, Long> { @Modifying @Query(“delete from User u where u.firstName = ?1”) void deleteUsersByFirstName(String firstName); } Update: In modern versions of Spring Data JPA (>=1.7.x) query derivation for delete, remove … Read more