Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package webproject.configuration;
- import org.apache.tomcat.jdbc.pool.DataSource;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.jdbc.datasource.DriverManagerDataSource;
- import org.springframework.security.authentication.encoding.Md5PasswordEncoder;
- import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
- import org.springframework.security.config.annotation.web.builders.HttpSecurity;
- import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
- import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
- @Configuration
- @EnableWebSecurity
- public class SecurityConfig extends WebSecurityConfigurerAdapter {
- @Override
- protected void configure(AuthenticationManagerBuilder auth) throws Exception {
- auth.jdbcAuthentication().dataSource(dataSource())
- .usersByUsernameQuery(
- "select username, password, enabled from user_account " +
- "where username=?"
- )
- .authoritiesByUsernameQuery(
- "select username, role from user_account " +
- "join user_role on user_account.id=user_role.user_account_id " +
- "where username=?"
- ).passwordEncoder(new Md5PasswordEncoder());
- }
- @Override
- protected void configure(HttpSecurity http) throws Exception {
- http.authorizeRequests()
- .anyRequest().authenticated()
- .and()
- .formLogin().and().httpBasic();
- }
- private DriverManagerDataSource dataSource() {
- DriverManagerDataSource driverManagerDataSource = new DriverManagerDataSource();
- driverManagerDataSource.setDriverClassName("org.postgresql.Driver");
- driverManagerDataSource.setUrl("jdbc:postgresql://localhost:5432/webprojectdb");
- driverManagerDataSource.setUsername("postgres");
- driverManagerDataSource.setPassword("postgres");
- return driverManagerDataSource;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement