Project

General

Profile

« Previous | Next » 

Revision 57212

simple authentication

View differences:

modules/dnet-orgs-database-application/trunk/src/main/java/eu/dnetlib/organizations/WebSecurityConfig.java
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
		http.csrf().disable();
25
		http.authorizeRequests()
26
				.antMatchers("/**").hasRole("USER")
27
				.and()
28
				.httpBasic();
29
	}
30

  
31
	@Autowired
32
	public void configureGlobal(final AuthenticationManagerBuilder auth) throws Exception {
33
		auth.jdbcAuthentication()
34
				.dataSource(dataSource)
35
				.usersByUsernameQuery("select email, password, valid from users where email=?")
36
				.authoritiesByUsernameQuery("select email, 'ROLE_USER' from users where email=?");
37
	}
38

  
39
	@Bean
40
	public PasswordEncoder passwordEncoder() {
41
		return PasswordEncoderFactories.createDelegatingPasswordEncoder();
42
	}
43

  
44
}
modules/dnet-orgs-database-application/trunk/src/main/resources/sql/samples.sql
6 6

  
7 7

  
8 8

  
9
INSERT INTO users(email, password, valid, superuser) VALUES ('michele.artini@isti.cnr.it', '{MD5}'||MD5('dnetPwd'), true, true);
10
INSERT INTO users(email, password, valid, superuser) VALUES ('emma.lazzeri@isti.cnr.it', '{MD5}'||MD5('dnetPwd'), true, false);
11
INSERT INTO users(email, password, valid, superuser) VALUES ('paolo.manghi@isti.cnr.it', '{MD5}'||MD5('dnetPwd'), true, false);
12

  
13
INSERT INTO user_countries VALUES ('emma.lazzeri@isti.cnr.it', 'IT');
14
INSERT INTO user_countries VALUES ('paolo.manghi@isti.cnr.it', 'IT');
15
INSERT INTO user_countries VALUES ('paolo.manghi@isti.cnr.it', 'GR');
16
INSERT INTO user_countries VALUES ('paolo.manghi@isti.cnr.it', 'GB');
modules/dnet-orgs-database-application/trunk/src/main/resources/sql/schema.sql
1 1
DROP VIEW organizations_view;
2 2
DROP VIEW organizations_simple_view;
3 3

  
4
DROP TABLE IF EXISTS user_countries;
5
DROP TABLE IF EXISTS users;
4 6
DROP TABLE IF EXISTS other_ids;
5 7
DROP TABLE IF EXISTS other_names;
6 8
DROP TABLE IF EXISTS acronyms;
......
14 16
DROP TABLE IF EXISTS languages;
15 17
DROP TABLE IF EXISTS countries;
16 18

  
19

  
17 20
CREATE TABLE org_types (val text PRIMARY KEY);
18 21
INSERT INTO org_types VALUES ('Archive'), ('Company'), ('Education'), ('Facility'), ('Government'), ('Healthcare'), ('Nonprofit'), ('Other');
19 22

  
......
39 42
			('SK'), ('SL'), ('SM'), ('SN'), ('SO'), ('SR'), ('SS'), ('ST'), ('SV'), ('SX'), ('SY'), ('SZ'), ('TC'), ('TD'), ('TG'), ('TH'), ('TJ'), ('TL'), ('TM'), ('TN'), ('TO'), ('TR'),
40 43
			('TT'), ('TV'), ('TW'), ('TZ'), ('UA'), ('UG'), ('US'), ('UY'), ('UZ'), ('VA'), ('VC'), ('VE'), ('VG'), ('VN'), ('WS'), ('XK'), ('YE'), ('ZA'), ('ZM'), ('ZW');
41 44

  
45
CREATE TABLE users (
46
	email     text PRIMARY KEY,
47
	password  text NOT NULL,
48
	valid     boolean default true,
49
	superuser boolean default false
50
);
42 51

  
52
CREATE TABLE user_countries (
53
	email     text REFERENCES users(email),
54
	country   text  REFERENCES countries(val),
55
	PRIMARY KEY(email, country)	
56
);
57

  
43 58
CREATE TABLE organizations (
44 59
    id                text PRIMARY KEY,
45 60
    name              text,
modules/dnet-orgs-database-application/trunk/pom.xml
74 74
			<artifactId>spring-boot-starter-json</artifactId>
75 75
		</dependency>
76 76
		<dependency>
77
			<groupId>org.springframework.boot</groupId>
78
			<artifactId>spring-boot-starter-security</artifactId>
79
		</dependency>
80
		<dependency>
77 81
			<groupId>org.postgresql</groupId>
78 82
			<artifactId>postgresql</artifactId>
79 83
		</dependency>

Also available in: Unified diff