package com.allfree.allfreespringbackend.auth.config; import com.allfree.allfreespringbackend.auth.security.AuthEntryPointJwt; import com.allfree.allfreespringbackend.auth.security.JwtTokenFilter; import com.allfree.allfreespringbackend.auth.security.UserDetailsServiceImpl; import com.allfree.allfreespringbackend.auth.security.UsernameAndPasswordAuthenticationProvider; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.security.authentication.AuthenticationManager; 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.config.http.SessionCreationPolicy; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; @Configuration @EnableWebSecurity //@EnableConfigurationProperties public class SecurityConfig extends WebSecurityConfigurerAdapter { @Autowired private UserDetailsServiceImpl userDetailsService; @Autowired private AuthEntryPointJwt unauthorizedHandler; @Autowired private UsernameAndPasswordAuthenticationProvider usernameAndPasswordAuthenticationProvider; //constructor test // public SecurityConfig(UserDetailsServiceImpl userDetailsService, // AuthEntryPointJwt unauthorizedHandler, // UsernameAndPasswordAuthenticationProvider usernameAndPasswordAuthenticationProvider){ // this.userDetailsService = userDetailsService; // this.unauthorizedHandler= unauthorizedHandler; // this.usernameAndPasswordAuthenticationProvider =usernameAndPasswordAuthenticationProvider; // } @Bean public JwtTokenFilter authenticationJwtTokenFilter() { return new JwtTokenFilter(); } @Override public void configure(AuthenticationManagerBuilder authenticationManagerBuilder) throws Exception { authenticationManagerBuilder .authenticationProvider(usernameAndPasswordAuthenticationProvider) .userDetailsService(userDetailsService) .passwordEncoder(passwordEncoder()); } @Bean @Override public AuthenticationManager authenticationManagerBean() throws Exception { return super.authenticationManagerBean(); } @Bean public PasswordEncoder passwordEncoder() { return new BCryptPasswordEncoder(); } @Override protected void configure(HttpSecurity http) throws Exception { http.cors().and().csrf().disable() .exceptionHandling().authenticationEntryPoint(unauthorizedHandler).and() .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and() .authorizeRequests().antMatchers("/api/auth/**").permitAll() .antMatchers("/api/v1/**").permitAll() // .antMatchers("/api/v1/users/**").hasRole("_ADMIN") // .antMatchers("/api/test/**").permitAll() .anyRequest().authenticated(); http.addFilterBefore(authenticationJwtTokenFilter(), UsernamePasswordAuthenticationFilter.class); } }