package com.cardpaymentsystem.cardplatform.config; import com.cardpaymentsystem.cardplatform.security.JwtAuthenticationFilter; import lombok.AllArgsConstructor; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.annotation.Bean; import org.springframework.http.HttpMethod; import org.springframework.security.authentication.AuthenticationManager; import org.springframework.security.config.BeanIds; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; 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.core.userdetails.UserDetailsService; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; import org.springframework.security.web.session.SessionManagementFilter; @EnableWebSecurity @AllArgsConstructor public class SecurityConfiguration extends WebSecurityConfigurerAdapter { @Qualifier("myUserDetailsService") @Autowired private final UserDetailsService userDetailsService; @Autowired private JwtAuthenticationFilter jwtAuthenticationFilter; @Bean(BeanIds.AUTHENTICATION_MANAGER) @Override public AuthenticationManager authenticationManagerBean() throws Exception{ return super.authenticationManagerBean(); } @Bean CorsFilter corsFilter() { CorsFilter filter = new CorsFilter(); return filter; } @Autowired protected void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { auth.userDetailsService(userDetailsService).passwordEncoder(getPassWordEncoder()); } @Override protected void configure(HttpSecurity http) throws Exception { http.csrf().disable()//can be safely disabled since JWT not cookies will be used for session authentication .httpBasic() .and() .authorizeRequests() .antMatchers(HttpMethod.OPTIONS,"/**").permitAll()//allow CORS option calls on any path!! .antMatchers("/resources/**").permitAll() .antMatchers("/").permitAll() .antMatchers("/api/customerauth/**").permitAll() .antMatchers("/api/businessauth/**").permitAll() .antMatchers("/api/refreshtoken/**").permitAll() .antMatchers("/api/card/cardVerification/**").permitAll() .antMatchers("/api/card/fundsVerification/**").permitAll() .antMatchers("/api/shopinfo/**").permitAll() .antMatchers("/api/fraud/**").permitAll() .antMatchers("/api/cloudinary/**").hasRole("BUSINESS") .antMatchers("/api/portal/**").hasRole("BUSINESS") .antMatchers("/api/businessproducts/**").hasRole("BUSINESS") .antMatchers("/api/businessstore/**").hasRole("BUSINESS") .antMatchers("/api/card/**").hasRole("BUSINESS") .antMatchers("/api/businessaccount/**").hasRole("BUSINESS") .antMatchers("/api/businesstransactions/**").hasRole("BUSINESS") .antMatchers("/api/customerstore/**").hasRole("CUSTOMER") .antMatchers("/api/shopping/**").hasRole("CUSTOMER") .antMatchers("/api/customeraccount/**").hasRole("CUSTOMER") .antMatchers("/api/customercards/**").hasRole("CUSTOMER") .antMatchers("/api/customertransactions/**").hasRole("CUSTOMER") .antMatchers("/api/firebase/**").hasRole("CUSTOMER") .antMatchers("/apidocs").permitAll() .antMatchers("/api/test/**").permitAll() .antMatchers("/swagger-ui/**").permitAll() .antMatchers("/swagger-resources/**").permitAll() .anyRequest().authenticated(); http.addFilterBefore(jwtAuthenticationFilter, UsernamePasswordAuthenticationFilter.class);// So that spring first checks JWT token before username password authentication scheme http.addFilterBefore(corsFilter(), SessionManagementFilter.class); // adds our custom CorsFilter } @Bean public PasswordEncoder getPassWordEncoder(){ return new BCryptPasswordEncoder(); } }