Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.code.config;
- import java.util.Arrays;
- 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.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.AuthenticationEntryPoint;
- import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
- import org.springframework.web.cors.CorsConfiguration;
- import org.springframework.web.cors.CorsConfigurationSource;
- import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
- import com.altercode.gerencg.service.UserService;
- @Configuration
- @EnableWebSecurity
- public class SecurityConfig extends WebSecurityConfigurerAdapter {
- @Autowired
- private Environment env;
- @Autowired
- private UserService userService;
- @Autowired
- private JWTTokenHelper jwtTokenHelper;
- @Autowired
- private AuthenticationEntryPoint authenticationEntryPoint;
- @Bean
- @Override
- public AuthenticationManager authenticationManagerBean() throws Exception {
- return super.authenticationManagerBean();
- }
- @Override
- protected void configure(HttpSecurity http) throws Exception {
- // h2-console
- if (Arrays.asList(env.getActiveProfiles()).contains("test")) {
- http.headers().frameOptions().disable();
- }
- http.cors().and().csrf().disable();
- http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and().exceptionHandling()
- .authenticationEntryPoint(authenticationEntryPoint).and()
- .authorizeRequests((request) -> request.antMatchers("/h2-console/**", "/api/v1/auth/login").permitAll()
- .antMatchers(HttpMethod.OPTIONS, "/**").permitAll())
- .addFilterBefore(new JWTAuthenticationFilter(userService, jwtTokenHelper),
- UsernamePasswordAuthenticationFilter.class);
- http.authorizeRequests().anyRequest().permitAll();
- }
- @Override
- protected void configure(AuthenticationManagerBuilder auth) throws Exception {
- auth.inMemoryAuthentication().withUser("LoginApp").password(passwordEncoder().encode("passwordtest"))
- .authorities("USER", "ADMIN");
- auth.userDetailsService(userService).passwordEncoder(passwordEncoder());
- }
- @Bean
- public PasswordEncoder passwordEncoder() {
- return new BCryptPasswordEncoder();
- }
- @Bean
- CorsConfigurationSource corsConfigurationSource() {
- CorsConfiguration configuration = new CorsConfiguration().applyPermitDefaultValues();
- configuration.setAllowedMethods(Arrays.asList("POST", "GET", "PUT", "DELETE", "OPTIONS"));
- final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
- source.registerCorsConfiguration("/**", configuration);
- return source;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement