package demo; import java.security.Principal; import java.util.HashMap; import java.util.Map; import java.util.UUID; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.security.SecurityProperties; import org.springframework.context.annotation.Configuration; import org.springframework.core.annotation.Order; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.web.csrf.CookieCsrfTokenRepository; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @SpringBootApplication @Controller public class UiApplication { // Match everything without a suffix (so not a static resource) @RequestMapping(value = "/{path:[^\\.]*}") public String redirect() { // Forward to home page so that route is preserved. return "forward:/"; } @RequestMapping("/user") @ResponseBody public Principal user(Principal user) { return user; } @RequestMapping("/resource") @ResponseBody public Map<String, Object> home() { Map<String, Object> model = new HashMap<String, Object>(); model.put("id", UUID.randomUUID().toString()); model.put("content", "Hello World"); return model; } public static void main(String[] args) { SpringApplication.run(UiApplication.class, args); } @Configuration @Order(SecurityProperties.ACCESS_OVERRIDE_ORDER) protected static class SecurityConfiguration extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { // @formatter:off http .httpBasic().and() .authorizeRequests() .antMatchers("/index.html", "/", "/login", "/message", "/home").permitAll() .anyRequest().authenticated() .and() .csrf() .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse()); // @formatter:on } } }