All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.iintelligas.config.SecurityConfig Maven / Gradle / Ivy

The newest version!
package com.github.iintelligas.config;


import com.github.iintelligas.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.core.annotation.Order;
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.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
@Profile({"ma", "production"})
@ComponentScan({"com.maosishu.hms","com.github.iintelligas"})
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    private static final Logger logger = LoggerFactory.getLogger(SecurityConfig.class);

    private static final String USER_ROLE           = "USER_ROLE";
    private static final String SUPER_USER_ROLE     = "SUPER_USER_ROLE";
    private static final String ADMIN_ROLE          = "admin";
    private static final String ACTUATOR            = "ACTUATOR";

    private final UserService               userDetailsService;
    private final CustomAuthFailureHandler customAuthFailureHandler;

    @Autowired
    public SecurityConfig(UserService userDetailsService, CustomAuthFailureHandler customAuthFailureHandler) {
        this.userDetailsService = userDetailsService;
        this.customAuthFailureHandler = customAuthFailureHandler;
    }

    @Configuration
    @Order(1)
    public class RestWebSecurityConfigurationAdapter extends WebSecurityConfigurerAdapter {
        protected void configure(HttpSecurity http) throws Exception {
            http
                    .csrf().disable()
                    .antMatcher("/admin/**")
                    .authorizeRequests()
                    .antMatchers("/admin/**").hasAnyAuthority(ADMIN_ROLE, SUPER_USER_ROLE,ACTUATOR)
                    .and()
                    .httpBasic()
                    .and().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);//We don't need sessions to be created.

        }
    }

    @Configuration
    @Order(2)
    public class FormLoginWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {

        @Override
        protected void configure(HttpSecurity http) throws Exception {
               http
                    .authorizeRequests()
                       .antMatchers("/register", "/register.html").permitAll()
                       .antMatchers("/login").permitAll()
                       .antMatchers("/resources/**").permitAll()
                       .anyRequest().authenticated()
                       .and()
                    .formLogin()
                       .loginPage("/login.html")
                       .usernameParameter("username")
                       .passwordParameter("password")
                       .loginProcessingUrl("/login")
                       .failureHandler(customAuthFailureHandler)
                       .defaultSuccessUrl("/")
                       .permitAll()
                       .and()
                    .logout()
                       .invalidateHttpSession(true)
                       .logoutSuccessUrl("/login")
                       .permitAll()
                       .and()
                    .headers()
                       .defaultsDisabled()
                       .frameOptions()
                       .sameOrigin()
                       .cacheControl();
            http
                    .csrf().disable();
        }
    }


    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }


    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy