Advertisement
Guest User

Untitled

a guest
Aug 31st, 2016
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.24 KB | None | 0 0
  1. package webproject.configuration;
  2.  
  3. import org.apache.tomcat.jdbc.pool.DataSource;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.context.annotation.Bean;
  6. import org.springframework.context.annotation.Configuration;
  7. import org.springframework.jdbc.datasource.DriverManagerDataSource;
  8. import org.springframework.security.authentication.encoding.Md5PasswordEncoder;
  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.  
  14. @Configuration
  15. @EnableWebSecurity
  16. public class SecurityConfig extends WebSecurityConfigurerAdapter {
  17.     @Override
  18.     protected void configure(AuthenticationManagerBuilder auth) throws Exception {
  19.         auth.jdbcAuthentication().dataSource(dataSource())
  20.                 .usersByUsernameQuery(
  21.                         "select username, password, enabled from user_account " +
  22.                         "where username=?"
  23.                 )
  24.                 .authoritiesByUsernameQuery(
  25.                         "select username, role from user_account " +
  26.                         "join user_role on user_account.id=user_role.user_account_id " +
  27.                         "where username=?"
  28.                 ).passwordEncoder(new Md5PasswordEncoder());
  29.     }
  30.  
  31.     @Override
  32.     protected void configure(HttpSecurity http) throws Exception {
  33.         http.authorizeRequests()
  34.                 .anyRequest().authenticated()
  35.                 .and()
  36.                 .formLogin().and().httpBasic();
  37.  
  38.     }
  39.  
  40.     private DriverManagerDataSource dataSource() {
  41.         DriverManagerDataSource driverManagerDataSource = new DriverManagerDataSource();
  42.         driverManagerDataSource.setDriverClassName("org.postgresql.Driver");
  43.         driverManagerDataSource.setUrl("jdbc:postgresql://localhost:5432/webprojectdb");
  44.         driverManagerDataSource.setUsername("postgres");
  45.         driverManagerDataSource.setPassword("postgres");
  46.         return driverManagerDataSource;
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement