Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.47 KB | None | 0 0
  1. package be.pxl.s2it;
  2.  
  3. import org.apache.logging.log4j.LogManager;
  4. import org.apache.logging.log4j.Logger;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.context.annotation.Bean;
  7. import org.springframework.context.annotation.Configuration;
  8. import org.springframework.http.HttpMethod;
  9. import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
  10. import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  11. import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
  12. import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  13. import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
  14. import org.springframework.web.cors.CorsConfiguration;
  15. import org.springframework.web.cors.CorsConfigurationSource;
  16. import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
  17.  
  18. import javax.sql.DataSource;
  19. import java.util.Arrays;
  20. import java.util.Collections;
  21.  
  22. @EnableWebSecurity
  23. //@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
  24. public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
  25. private Logger logger = LogManager.getLogger(getClass());
  26.  
  27. @Autowired
  28. public void configureGlobalSecurity(AuthenticationManagerBuilder auth, DataSource ds) throws Exception {
  29. auth.jdbcAuthentication()
  30. .passwordEncoder(new BCryptPasswordEncoder())
  31. .dataSource(ds)
  32. .usersByUsernameQuery(
  33. "SELECT u.userId, u.password, u.enabled FROM s2it_user u where u.userId = ?")
  34. .authoritiesByUsernameQuery(
  35. "SELECT u.userId, a.role from s2it_user u where u.userId = ?");
  36. }
  37.  
  38. @Override
  39. protected void configure(HttpSecurity http) throws Exception {
  40. http.csrf().disable()
  41. .cors()
  42. .and()
  43. .authorizeRequests()
  44. .antMatchers("/user/exist/**").permitAll()
  45. .antMatchers(HttpMethod.GET, "/auth").hasAnyRole("MERCHANT", "USER")
  46. .antMatchers("/free/**").permitAll()
  47. .anyRequest().authenticated()
  48. .and()
  49. .httpBasic()
  50. .and()
  51. .logout().logoutSuccessUrl("/logout")
  52. .and()
  53. .exceptionHandling().accessDeniedPage("/403");
  54. }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement