Blame view

PFE06/src/main/java/com/PFE/ServerManager/SecurityConfig.java 2.55 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
21
  
  import javax.sql.DataSource;
  
  @Configuration
  @EnableAutoConfiguration
  public class SecurityConfig extends WebSecurityConfigurerAdapter {
  
      @Autowired
      DataSource dataSource;
  
      @Autowired
6029d52b   Antoine Duquenoy   Spring Security o...
22
23
24
      private BCryptPasswordEncoder passwordEncoder;
  
      @Autowired
d0a03db7   sfeutrie   restructuration d...
25
26
27
      public void configAuthentication(AuthenticationManagerBuilder auth) throws Exception {
          auth.jdbcAuthentication()
                  .dataSource(dataSource)
6029d52b   Antoine Duquenoy   Spring Security o...
28
29
30
                  .passwordEncoder(passwordEncoder)
                  .usersByUsernameQuery("select pseudo, password , active from customer where pseudo=?")
                  .authoritiesByUsernameQuery("select c.pseudo, 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.pseudo=?");
d0a03db7   sfeutrie   restructuration d...
31
32
33
34
35
36
      }
  
      @Override
      protected void configure(HttpSecurity http) throws Exception {
          http
                  .authorizeRequests()
16791f79   sfeutrie   routage du site
37
38
                      .antMatchers("/home").hasAnyAuthority("USER","ADMIN")
                      .antMatchers("/registration").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")
6029d52b   Antoine Duquenoy   Spring Security o...
47
48
                      .usernameParameter("pseudo")
                      .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
  }