Project

General

Profile

1
package eu.dnetlib.organizations;
2

    
3
import javax.sql.DataSource;
4

    
5
import org.springframework.beans.factory.annotation.Autowired;
6
import org.springframework.context.annotation.Bean;
7
import org.springframework.context.annotation.Configuration;
8
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
9
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
10
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
11
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
12
import org.springframework.security.crypto.factory.PasswordEncoderFactories;
13
import org.springframework.security.crypto.password.PasswordEncoder;
14

    
15
@Configuration
16
@EnableWebSecurity
17
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
18

    
19
	@Autowired
20
	private DataSource dataSource;
21

    
22
	@Override
23
	protected void configure(final HttpSecurity http) throws Exception {
24

    
25
		http.authorizeRequests()
26
				.antMatchers("/", "/swagger-ui.html", "/resources/**", "/messages/**").permitAll()
27
				.antMatchers("/api/**").fullyAuthenticated()
28
				.and()
29
				.httpBasic()
30
				.and()
31
				.logout()
32
				.logoutSuccessUrl("/messages/logout.html").permitAll()
33
				.deleteCookies("auth_code", "JSESSIONID")
34
				.clearAuthentication(true)
35
				.invalidateHttpSession(true)
36
				.and()
37
				.csrf().disable();
38

    
39
	}
40

    
41
	@Autowired
42
	public void configureGlobal(final AuthenticationManagerBuilder auth) throws Exception {
43

    
44
		auth.jdbcAuthentication()
45
				.dataSource(dataSource)
46
				.usersByUsernameQuery("select email, password, valid from users where email=? and valid=true")
47
				.authoritiesByUsernameQuery("select email, 'ROLE_'||role from users where email=? and valid=true");
48
	}
49

    
50
	@Bean
51
	public PasswordEncoder passwordEncoder() {
52
		return PasswordEncoderFactories.createDelegatingPasswordEncoder();
53
	}
54

    
55
}
(2-2/2)