Advertisement
Guest User

iamdayn

a guest
Mar 26th, 2017
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.87 KB | None | 0 0
  1. package com.example.configuration;
  2.  
  3. import javax.sql.DataSource;
  4.  
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.beans.factory.annotation.Value;
  7. import org.springframework.context.annotation.Bean;
  8. import org.springframework.context.annotation.ComponentScan;
  9. import org.springframework.context.annotation.Configuration;
  10. import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
  11. import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  12. import org.springframework.security.config.annotation.web.builders.WebSecurity;
  13. import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
  14. import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  15. import org.springframework.security.core.userdetails.UserDetailsService;
  16. import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
  17. import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
  18.  
  19. @Configuration
  20. @EnableWebSecurity
  21. public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
  22.  
  23.  
  24.     @Autowired
  25.     private BCryptPasswordEncoder bCryptPasswordEncoder;
  26.  
  27.     @Autowired
  28.     private DataSource dataSource;
  29.  
  30.     @Value("${spring.queries.users-query}")
  31.     private String usersQuery;
  32.  
  33.     @Value("${spring.queries.roles-query}")
  34.     private String rolesQuery;
  35.  
  36.     @Override
  37.     protected void configure(AuthenticationManagerBuilder auth)
  38.             throws Exception {
  39.         auth.
  40.                 jdbcAuthentication()
  41.                 .usersByUsernameQuery(usersQuery)
  42.                 .authoritiesByUsernameQuery(rolesQuery)
  43.                 .dataSource(dataSource)
  44.                 .passwordEncoder(bCryptPasswordEncoder);
  45.     }
  46.  
  47.  
  48.     @Override
  49.     protected void configure(HttpSecurity http) throws Exception {
  50.  
  51.         http.authorizeRequests()
  52.                 .antMatchers("/").permitAll()
  53.                 .antMatchers("/login").permitAll()
  54.                 .antMatchers("/registration").permitAll()
  55.                 .antMatchers("/admin/**")
  56.                 .hasAuthority("ADMIN").anyRequest().authenticated()
  57.                 .and().csrf().disable().formLogin()
  58.                 .loginPage("/login").failureUrl("/login?error=true")
  59.                 .defaultSuccessUrl("/admin/home").loginPage("/")
  60.                 .usernameParameter("email")
  61.                 .passwordParameter("password")
  62.                 .failureUrl("/").and().logout()
  63.                 .logoutSuccessUrl("/").and()
  64.                 .authorizeRequests()
  65.  
  66.                 .antMatchers("/").permitAll()
  67.                 .antMatchers("/login").permitAll()
  68.                 .antMatchers("/registration").permitAll()
  69.                 .antMatchers("/worker/**")
  70.                 .hasAuthority("WORKER").anyRequest().authenticated()
  71.                 .and().csrf().disable().formLogin()
  72.                 .loginPage("/login").failureUrl("/login?error=true")
  73.                 .defaultSuccessUrl("/worker/home").loginPage("/")
  74.                 .usernameParameter("email")
  75.                 .passwordParameter("password")
  76.                 .failureUrl("/").and().logout()
  77.                 .logoutSuccessUrl("/")
  78.  
  79.                 .and().logout()
  80.                 .logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
  81.                 .logoutSuccessUrl("/").and().exceptionHandling()
  82.                 .accessDeniedPage("/access-denied");
  83.     }
  84.  
  85.     @Bean
  86.     public BCryptPasswordEncoder passwordEncoder() {
  87.         BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
  88.         return bCryptPasswordEncoder;
  89.     }
  90.  
  91.     @Override
  92.     public void configure(WebSecurity web) throws Exception {
  93.         web
  94.                 .ignoring()
  95.                 .antMatchers("/resources/**", "/static/**", "/css/**", "/js/**", "/images/**");
  96.     }
  97.  
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement