Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.75 KB | None | 0 0
  1. package pl.jedralski.LibraryRecommendationSystem.config;
  2.  
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.beans.factory.annotation.Qualifier;
  5. import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
  6. import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  7. import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
  8.  
  9. import javax.sql.DataSource;
  10.  
  11.  
  12. public class SpringSecurityConfig {
  13.  
  14.     private DataSource dataSource;
  15.  
  16.     @Autowired
  17.     public SpringSecurityConfig(@Qualifier("dataSource") DataSource dataSource) {
  18.         this.dataSource = dataSource;
  19.     }
  20.  
  21.     @Autowired
  22.     public void configure(AuthenticationManagerBuilder auth) throws Exception {
  23.         auth.jdbcAuthentication()
  24.                 .usersByUsernameQuery(
  25.                         "select username, hash, 1 as active from users where username = ?")
  26.                 .authoritiesByUsernameQuery(
  27.                         "select u.username, 'admin' as role_name from users u where u.username = ?")
  28.                 .dataSource(dataSource);
  29.     }
  30.  
  31.  
  32.     protected void configure(HttpSecurity http) throws Exception {
  33.         http.authorizeRequests().antMatchers("/login").permitAll()
  34.                 .antMatchers("/").hasAnyAuthority("user", "admin")
  35.                 .and().formLogin().loginPage("/login").failureUrl("/login?error=true").defaultSuccessUrl("/")
  36.                 .usernameParameter("username").passwordParameter("password")
  37.                 .and().logout().logoutRequestMatcher(new AntPathRequestMatcher("/logout")).logoutSuccessUrl("/login")
  38.                 .and().exceptionHandling().accessDeniedPage("/access-denied");
  39.     }
  40.  
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement