Advertisement
Guest User

sd

a guest
Aug 2nd, 2017
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.80 KB | None | 0 0
  1. package com.movie.database.config;
  2.  
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.beans.factory.annotation.Value;
  5. import org.springframework.context.annotation.Bean;
  6. import org.springframework.context.annotation.Configuration;
  7. import org.springframework.context.annotation.Import;
  8. import org.springframework.context.annotation.PropertySource;
  9. import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
  10. import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
  11. import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  12. import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
  13. import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  14. import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
  15. import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
  16.  
  17. import javax.sql.DataSource;
  18.  
  19. @Configuration
  20. @EnableWebSecurity
  21. @EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true)
  22. @Import({WebDatasourceConfig.class})
  23. @PropertySource("classpath:security.properties")
  24. public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
  25.  
  26.     @Value("${pattern}") String pattern;
  27.  
  28.     // Form Login
  29.     @Value("${login.page}") String loginPage;
  30.     @Value("${default.success.url}") String defaultSuccessUrl;
  31.     @Value("${failure.url}") String failureUrl;
  32.     @Value("${username.parameter}") String usernameParameter;
  33.     @Value("${password.parameter}") String passwordParameter;
  34.  
  35.     // Logout
  36.     @Value("${delete.cookies}") String deleteCookies;
  37.     @Value("${invalidate.http.session}") boolean invalidateHttpSession;
  38.     @Value("${clear.authentication}") boolean clearAuthentication;
  39.     @Value("${logout.request.matcher}") String logoutRequestMatcher;
  40.     @Value("${logout.success.url}") String logoutSuccessUrl;
  41.  
  42.     // Authentication Manager
  43.     @Value("${users.by.username.query}") String usersByUsernameQuery;
  44.     @Value("${authorities.by.username.query}") String authoritiesByUsernameQuery;
  45.  
  46.     // Password Encoder
  47.     @Value("${strength.password.encoder}") int strengthPasswordEncoder;
  48.  
  49.     @Autowired
  50.     private DataSource dataSource;
  51.  
  52.     @Override
  53.     protected void configure(HttpSecurity http) throws Exception {
  54.         http.authorizeRequests()
  55.                 .antMatchers(pattern).permitAll()
  56.                 .and()
  57.                     .formLogin()
  58.                         .loginPage(loginPage)
  59.                         .defaultSuccessUrl(defaultSuccessUrl)
  60.                         .failureUrl(failureUrl)
  61.                         .usernameParameter(usernameParameter)
  62.                         .passwordParameter(passwordParameter)
  63.                 .and()
  64.                     .logout()
  65.                         .deleteCookies(deleteCookies)
  66.                         .invalidateHttpSession(invalidateHttpSession)
  67.                         .clearAuthentication(clearAuthentication)
  68.                         .logoutRequestMatcher(new AntPathRequestMatcher(logoutRequestMatcher))
  69.                         .logoutSuccessUrl(logoutSuccessUrl)
  70.                 .and()
  71.                     .csrf().disable();
  72.     }
  73.  
  74.     @Autowired
  75.     public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
  76.         auth.jdbcAuthentication()
  77.                 .passwordEncoder(passwordEncoder())
  78.                 .dataSource(dataSource)
  79.                 .usersByUsernameQuery(usersByUsernameQuery)
  80.                 .authoritiesByUsernameQuery(authoritiesByUsernameQuery);
  81.     }
  82.  
  83.     @Bean
  84.     public BCryptPasswordEncoder passwordEncoder() {
  85.         return new BCryptPasswordEncoder(strengthPasswordEncoder);
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement