1 | package com.example.autopartz.config;
|
---|
2 |
|
---|
3 | import org.springframework.context.annotation.Configuration;
|
---|
4 | import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
|
---|
5 | import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
|
---|
6 | import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
---|
7 | import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
---|
8 | import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
---|
9 | import org.springframework.security.crypto.password.PasswordEncoder;
|
---|
10 |
|
---|
11 | @Configuration
|
---|
12 | @EnableWebSecurity
|
---|
13 | @EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true)
|
---|
14 | public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
|
---|
15 |
|
---|
16 | private final PasswordEncoder passwordEncoder;
|
---|
17 | private final CustomUsernamePasswordAuthenticationProvider authenticationProvider;
|
---|
18 |
|
---|
19 | public WebSecurityConfig(PasswordEncoder passwordEncoder,
|
---|
20 | CustomUsernamePasswordAuthenticationProvider authenticationProvider) {
|
---|
21 | this.passwordEncoder = passwordEncoder;
|
---|
22 | this.authenticationProvider = authenticationProvider;
|
---|
23 | }
|
---|
24 |
|
---|
25 | @Override
|
---|
26 | protected void configure(HttpSecurity http) throws Exception {
|
---|
27 |
|
---|
28 | http.csrf().disable()
|
---|
29 | .authorizeRequests()
|
---|
30 | .antMatchers("/", "/products", "/services", "/filtered", "/login", "/register","/registerWarehouseman","/finishRegister","/test/*","/access_denied","/carCategoryReport","/partManufacturersReport","/mostPurchasedPart","/part/*").permitAll()
|
---|
31 | .antMatchers("/orders","/repairs","/reviews","/currentOrder","/addCarSampleForUser","/repairs/addReview/*").hasRole("CLIENT")
|
---|
32 | .antMatchers("/viewUsers","/approve/*","/addPart","/addCarManufacturer","/addPartManufacturer","/addCategory","/addCar","/addRepairShop","/addWarehouse").hasRole("ADMIN")
|
---|
33 | .antMatchers("/myWarehouseReport","myWarehouse").hasRole("WAREHOUSEMAN")
|
---|
34 | .anyRequest()
|
---|
35 | .authenticated()
|
---|
36 | .and()
|
---|
37 | .formLogin()
|
---|
38 | .loginPage("/login").permitAll()
|
---|
39 | .failureUrl("/login?error=BadCredentials")
|
---|
40 | .defaultSuccessUrl("/", true)
|
---|
41 | .and()
|
---|
42 | .logout()
|
---|
43 | .logoutUrl("/logout")
|
---|
44 | .clearAuthentication(true)
|
---|
45 | .invalidateHttpSession(true)
|
---|
46 | .deleteCookies("JSESSIONID")
|
---|
47 | .logoutSuccessUrl("/")
|
---|
48 | .and()
|
---|
49 | .exceptionHandling().accessDeniedPage("/access_denied");
|
---|
50 |
|
---|
51 | }
|
---|
52 |
|
---|
53 | @Override
|
---|
54 | protected void configure(AuthenticationManagerBuilder auth) {
|
---|
55 | auth.authenticationProvider(authenticationProvider);
|
---|
56 | }
|
---|
57 |
|
---|
58 |
|
---|
59 |
|
---|
60 | }
|
---|
61 |
|
---|
62 |
|
---|