Java tutorial
/* * Copyright 2017 Shinya Mochida * * Licensed under the Apache License,Version2.0(the"License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing,software * Distributed under the License is distributed on an"AS IS"BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.example; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.PropertyNamingStrategy; import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; import org.springframework.hateoas.MediaTypes; import org.springframework.hateoas.hal.Jackson2HalModule; import org.springframework.http.MediaType; import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder; import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.builders.WebSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer; import org.springframework.web.client.RestTemplate; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.Arrays; import java.util.Collections; @SpringBootApplication @EnableAuthorizationServer public class AuthzApp extends WebSecurityConfigurerAdapter { public static void main(String[] args) { SpringApplication.run(AuthzApp.class, args); } @Bean ObjectMapper objectMapper() { final JavaTimeModule javaTimeModule = new JavaTimeModule(); javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ISO_LOCAL_DATE_TIME)); return new Jackson2ObjectMapperBuilder() .featuresToDisable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES).modules(javaTimeModule) .serializationInclusion(JsonInclude.Include.NON_NULL) .propertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE).build(); } private Jackson2ObjectMapperBuilder halJsonObjectMapperBuilder() { return new Jackson2ObjectMapperBuilder() .featuresToDisable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES) .propertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE) .modules(new Jackson2HalModule(), new JavaTimeModule()); } private MappingJackson2HttpMessageConverter halJsonMappingJackson2HttpMessageConverter() { final MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter( halJsonObjectMapperBuilder().build()); converter.setSupportedMediaTypes( Arrays.asList(MediaTypes.HAL_JSON, MediaType.APPLICATION_JSON_UTF8, MediaType.APPLICATION_JSON)); return converter; } @Bean @Db RestTemplate halJsonRestTemplate() { return new RestTemplate(Collections.singletonList(halJsonMappingJackson2HttpMessageConverter())); } @Bean RestTemplate restTemplate() { final MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter( new ObjectMapper().disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES) .setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE) .registerModule(new JavaTimeModule())); return new RestTemplate(Collections.singletonList(converter)); } @Override public void configure(final WebSecurity web) { web.ignoring().antMatchers("/webjars/**", "/css/**"); } @Override protected void configure(final HttpSecurity http) throws Exception { http.authorizeRequests().antMatchers("/", "/home").permitAll().anyRequest().authenticated().and() .formLogin().loginPage("/login").loginProcessingUrl("/login").failureUrl("/login?error") .defaultSuccessUrl("/home").usernameParameter("username").passwordParameter("password").permitAll() .and().logout().logoutUrl("/logout").logoutSuccessUrl("/home"); } }