Allow caching of public resources with

http://stackoverflow.com/a/36293562/3761783
This commit is contained in:
2016-07-26 22:53:25 +02:00
parent 37c4ebf0ae
commit db7f651157
4 changed files with 33 additions and 15 deletions
@@ -1,10 +1,7 @@
package de.tilman.transactions; package de.tilman.transactions;
import org.h2.server.web.WebServlet;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.embedded.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
@SpringBootApplication @SpringBootApplication
public class Application { public class Application {
@@ -1,5 +1,8 @@
package de.tilman.transactions; package de.tilman.transactions;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod; import org.springframework.http.HttpMethod;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
@@ -7,6 +10,8 @@ import org.springframework.security.config.annotation.method.configuration.Enabl
import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.header.HeaderWriter;
import org.springframework.security.web.header.writers.CacheControlHeadersWriter;
@Configuration @Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true) @EnableGlobalMethodSecurity(prePostEnabled = true)
@@ -15,23 +20,39 @@ public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override @Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception { protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication() auth.inMemoryAuthentication().withUser("adam").password("test").roles("USER", "ADMIN").and().withUser("betty")
.withUser("adam").password("test").roles("USER", "ADMIN").and() .password("test").roles("USER");
.withUser("betty").password("test").roles("USER");
} }
@Override @Override
protected void configure(HttpSecurity http) throws Exception { protected void configure(HttpSecurity http) throws Exception {
// Basis-Schutz: Nur autorisierte Zugriffe (feingranulare Steuerung über Assertions) // Basis-Schutz: Nur autorisierte Zugriffe (feingranulare Steuerung über Assertions)
http.authorizeRequests() http.authorizeRequests().antMatchers(HttpMethod.GET, "/public/**").permitAll()
.antMatchers(HttpMethod.GET, "/public/**").permitAll()
// .antMatchers("/console/**").permitAll() // H2 console for the dev database // .antMatchers("/console/**").permitAll() // H2 console for the dev database
.anyRequest().authenticated(); .anyRequest().authenticated();
// http.httpBasic(); // XXX mit HTTP Basic Auth funktioniert der Logout nicht richtig
http.formLogin(); http.formLogin();
http.csrf().disable(); // XXX später wieder aktivieren http.csrf().disable(); // XXX später wieder aktivieren
// http.headers().frameOptions().disable(); // for H2 console // http.headers().frameOptions().disable(); // for H2 console
// configure caching
http.headers().cacheControl().disable();
http.headers().addHeaderWriter(new HeaderWriter() {
CacheControlHeadersWriter originalWriter = new CacheControlHeadersWriter();
@Override
public void writeHeaders(HttpServletRequest request, HttpServletResponse response) {
String requestUri = request.getRequestURI();
if (!requestUri.startsWith("/public")) {
originalWriter.writeHeaders(request, response);
} else {
response.setHeader("Cache-Control", "public, max-age=10000000");
} }
} }
});
}
}
+1 -1
View File
@@ -1,7 +1,7 @@
body { body {
font-family: "Open Sans", sans-serif; font-family: "Open Sans", sans-serif;
padding: 20px; padding: 20px;
background: url(/background.png) no-repeat center center fixed; background: url(/public/background.png) no-repeat center center fixed;
background-size: cover; background-size: cover;
} }

Before

Width:  |  Height:  |  Size: 96 KiB

After

Width:  |  Height:  |  Size: 96 KiB