Skip to content

Instantly share code, notes, and snippets.

@MrMjauh
Last active June 27, 2019 22:02
Show Gist options
  • Save MrMjauh/ad1212a79291f6265d7f9e36f64e5393 to your computer and use it in GitHub Desktop.
Save MrMjauh/ad1212a79291f6265d7f9e36f64e5393 to your computer and use it in GitHub Desktop.
Deny all
package com.codecamos.timetracking.config;
import javax.servlet.http.HttpServletResponse;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
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.WebSecurityConfigurerAdapter;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(jsr250Enabled = true)
public class Config extends WebSecurityConfigurerAdapter implements WebMvcConfigurer {
@Autowired
Environment env;
@Override
protected void configure(final HttpSecurity http) throws Exception {
if (Resource.isInDevOrStage(env.getActiveProfiles())) {
http
.csrf().disable()
.logout().disable()
.formLogin().disable()
.authorizeRequests()
.antMatchers("*").permitAll();
} else {
http
.csrf().disable()
.logout().disable()
.formLogin().disable()
.authorizeRequests()
.antMatchers(HttpMethod.GET, "/hello").permitAll()
.antMatchers(HttpMethod.GET, "/runtime").permitAll()
.anyRequest().fullyAuthenticated();
}
}
@Bean
public ObjectMapper jsonMapper()
{
ObjectMapper mapper = new ObjectMapper();
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
return mapper;
}
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
configurer.defaultContentType(MediaType.APPLICATION_JSON);
}
@Bean
public CharacterEncodingFilter characterEncodingFilter() {
final CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
characterEncodingFilter.setEncoding("UTF-8");
characterEncodingFilter.setForceEncoding(true);
return characterEncodingFilter;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment