Blame view

PFE06/src/main/java/com/PFE/ServerManager/SecurityConfig.java 2.6 KB
d0a03db7   sfeutrie   restructuration d...
1
2
3
4
5
6
7
8
  package com.PFE.ServerManager;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
  import org.springframework.context.annotation.Configuration;
  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.WebSecurityConfigurerAdapter;
6029d52b   Antoine Duquenoy   Spring Security o...
9
10
  import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
  import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
d0a03db7   sfeutrie   restructuration d...
11
12
13
14
15
16
17
18
19
20
  import javax.sql.DataSource;
  
  @Configuration
  @EnableAutoConfiguration
  public class SecurityConfig extends WebSecurityConfigurerAdapter {
  
      @Autowired
      DataSource dataSource;
  
      @Autowired
6029d52b   Antoine Duquenoy   Spring Security o...
21
22
23
      private BCryptPasswordEncoder passwordEncoder;
  
      @Autowired
d0a03db7   sfeutrie   restructuration d...
24
25
26
      public void configAuthentication(AuthenticationManagerBuilder auth) throws Exception {
          auth.jdbcAuthentication()
                  .dataSource(dataSource)
6029d52b   Antoine Duquenoy   Spring Security o...
27
                  .passwordEncoder(passwordEncoder)
072e7fc2   Unknown   Passage de pseudo...
28
29
                  .usersByUsernameQuery("select email, password , active from customer where email=?")
                  .authoritiesByUsernameQuery("select c.email, r.role from customer c inner join customer_role cr on(c.customer_id=cr.customer_id) inner join role r on(cr.role_id=r.role_id) where c.email=?");
d0a03db7   sfeutrie   restructuration d...
30
31
32
33
34
35
      }
  
      @Override
      protected void configure(HttpSecurity http) throws Exception {
          http
                  .authorizeRequests()
16791f79   sfeutrie   routage du site
36
37
                      .antMatchers("/home").hasAnyAuthority("USER","ADMIN")
                      .antMatchers("/registration").hasAuthority("ADMIN")
115e3f68   sfeutrie   amélioration du f...
38
                      .antMatchers("/all").hasAuthority("ADMIN")
d0a03db7   sfeutrie   restructuration d...
39
                      .antMatchers("/login").permitAll()
6029d52b   Antoine Duquenoy   Spring Security o...
40
                      .antMatchers("/denied").permitAll()
66a8b43c   Antoine Duquenoy   Intégration du fr...
41
                      .antMatchers("/css/**", "/js/**").permitAll()
6029d52b   Antoine Duquenoy   Spring Security o...
42
                      .anyRequest().authenticated()
d0a03db7   sfeutrie   restructuration d...
43
44
                      .and()
                  .formLogin()
6029d52b   Antoine Duquenoy   Spring Security o...
45
                      .loginPage("/login").failureUrl("/login?error=true")
16791f79   sfeutrie   routage du site
46
                      .defaultSuccessUrl("/home")
072e7fc2   Unknown   Passage de pseudo...
47
                      .usernameParameter("email")
6029d52b   Antoine Duquenoy   Spring Security o...
48
                      .passwordParameter("password")
d0a03db7   sfeutrie   restructuration d...
49
50
                      .and()
                  .logout()
6029d52b   Antoine Duquenoy   Spring Security o...
51
                      .logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
66a8b43c   Antoine Duquenoy   Intégration du fr...
52
                      .logoutSuccessUrl("/login")
6029d52b   Antoine Duquenoy   Spring Security o...
53
54
                      .and()
                  .exceptionHandling()
303fdc72   Antoine Duquenoy   Sauvegarde des fi...
55
56
57
                      .accessDeniedPage("/denied")
                      .and()
                  .csrf().disable();
d0a03db7   sfeutrie   restructuration d...
58
      }
66a8b43c   Antoine Duquenoy   Intégration du fr...
59
  
d0a03db7   sfeutrie   restructuration d...
60
  }