Last active
October 10, 2016 20:11
-
-
Save jimador/4df88c2a0bca1c8240a898a60c049f05 to your computer and use it in GitHub Desktop.
Spring Boot Security config
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import com.asi.hrportal.security.*; | |
| import com.asi.hrportal.web.filter.CsrfCookieGeneratorFilter; | |
| import org.springframework.context.annotation.Bean; | |
| import org.springframework.context.annotation.Configuration; | |
| import org.springframework.core.env.Environment; | |
| import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; | |
| 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.builders.WebSecurity; | |
| import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; | |
| import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; | |
| import org.springframework.security.core.userdetails.UserDetailsService; | |
| import org.springframework.security.crypto.password.PasswordEncoder; | |
| import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; | |
| import org.springframework.security.data.repository.query.SecurityEvaluationContextExtension; | |
| import org.springframework.security.web.authentication.RememberMeServices; | |
| import org.springframework.security.web.csrf.CsrfFilter; | |
| import javax.inject.Inject; | |
| @Configuration | |
| @EnableWebSecurity | |
| @EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true) | |
| public class SecurityConfiguration extends WebSecurityConfigurerAdapter { | |
| @Inject | |
| private Environment env; | |
| @Inject | |
| private AjaxAuthenticationSuccessHandler ajaxAuthenticationSuccessHandler; | |
| @Inject | |
| private AjaxAuthenticationFailureHandler ajaxAuthenticationFailureHandler; | |
| @Inject | |
| private AjaxLogoutSuccessHandler ajaxLogoutSuccessHandler; | |
| @Inject | |
| private Http401UnauthorizedEntryPoint authenticationEntryPoint; | |
| @Inject | |
| private UserDetailsService userDetailsService; | |
| @Inject | |
| private RememberMeServices rememberMeServices; | |
| @Bean | |
| public PasswordEncoder passwordEncoder() { | |
| return new BCryptPasswordEncoder(); | |
| } | |
| @Inject | |
| public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { | |
| auth | |
| .userDetailsService(userDetailsService) | |
| .passwordEncoder(passwordEncoder()); | |
| } | |
| @Override | |
| public void configure(WebSecurity web) throws Exception { | |
| web.ignoring() | |
| .antMatchers("/scripts/**/*.{js,html}") | |
| .antMatchers("/bower_components/**") | |
| .antMatchers("/i18n/**") | |
| .antMatchers("/assets/**") | |
| .antMatchers("/swagger-ui/index.html") | |
| .antMatchers("/test/**"); | |
| } | |
| @Override | |
| protected void configure(HttpSecurity http) throws Exception { | |
| http | |
| .csrf() | |
| .and() | |
| .addFilterAfter(new CsrfCookieGeneratorFilter(), CsrfFilter.class) | |
| .exceptionHandling() | |
| .accessDeniedHandler(new CustomAccessDeniedHandler()) | |
| .authenticationEntryPoint(authenticationEntryPoint) | |
| .and() | |
| .rememberMe() | |
| .rememberMeServices(rememberMeServices) | |
| .rememberMeParameter("remember-me") | |
| .key(env.getProperty("jhipster.security.rememberme.key")) | |
| .and() | |
| .formLogin() | |
| .loginProcessingUrl("/api/authentication") | |
| .successHandler(ajaxAuthenticationSuccessHandler) | |
| .failureHandler(ajaxAuthenticationFailureHandler) | |
| .usernameParameter("j_username") | |
| .passwordParameter("j_password") | |
| .permitAll() | |
| .and() | |
| .logout() | |
| .logoutUrl("/api/logout") | |
| .logoutSuccessHandler(ajaxLogoutSuccessHandler) | |
| .deleteCookies("JSESSIONID", "CSRF-TOKEN") | |
| .permitAll() | |
| .and() | |
| .headers() | |
| .frameOptions() | |
| .disable() | |
| .and() | |
| .authorizeRequests() | |
| .antMatchers("/api/register").permitAll() | |
| .antMatchers("/api/activate").permitAll() | |
| .antMatchers("/api/authenticate").permitAll() | |
| .antMatchers("/api/account/reset_password/init").permitAll() | |
| .antMatchers("/api/account/reset_password/finish").permitAll() | |
| .antMatchers("/api/logs/**").hasAuthority(AuthoritiesConstants.SYS_ADMIN) | |
| .antMatchers("/api/audits/**").hasAuthority(AuthoritiesConstants.SYS_ADMIN) | |
| .antMatchers("/api/**").authenticated() | |
| .antMatchers("/metrics/**").hasAuthority(AuthoritiesConstants.SYS_ADMIN) | |
| .antMatchers("/health/**").hasAuthority(AuthoritiesConstants.SYS_ADMIN) | |
| .antMatchers("/trace/**").hasAuthority(AuthoritiesConstants.SYS_ADMIN) | |
| .antMatchers("/dump/**").hasAuthority(AuthoritiesConstants.SYS_ADMIN) | |
| .antMatchers("/shutdown/**").hasAuthority(AuthoritiesConstants.SYS_ADMIN) | |
| .antMatchers("/beans/**").hasAuthority(AuthoritiesConstants.SYS_ADMIN) | |
| .antMatchers("/configprops/**").hasAuthority(AuthoritiesConstants.SYS_ADMIN) | |
| .antMatchers("/info/**").hasAuthority(AuthoritiesConstants.SYS_ADMIN) | |
| .antMatchers("/autoconfig/**").hasAuthority(AuthoritiesConstants.SYS_ADMIN) | |
| .antMatchers("/env/**").hasAuthority(AuthoritiesConstants.SYS_ADMIN) | |
| .antMatchers("/trace/**").hasAuthority(AuthoritiesConstants.SYS_ADMIN) | |
| .antMatchers("/mappings/**").hasAuthority(AuthoritiesConstants.SYS_ADMIN) | |
| .antMatchers("/liquibase/**").hasAuthority(AuthoritiesConstants.SYS_ADMIN) | |
| .antMatchers("/v2/api-docs/**").permitAll() | |
| .antMatchers("/configuration/security").permitAll() | |
| .antMatchers("/configuration/ui").permitAll() | |
| .antMatchers("/swagger-ui/index.html").hasAuthority(AuthoritiesConstants.SYS_ADMIN) | |
| .antMatchers("/protected/**").authenticated() ; | |
| } | |
| @Bean | |
| public SecurityEvaluationContextExtension securityEvaluationContextExtension() { | |
| return new SecurityEvaluationContextExtension(); | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment