1 | package com.example.db.config;
|
---|
2 |
|
---|
3 | import org.springframework.context.annotation.Configuration;
|
---|
4 | import org.thymeleaf.TemplateEngine;
|
---|
5 |
|
---|
6 | /*
|
---|
7 | @Configuration
|
---|
8 | @EnableWebSecurity
|
---|
9 | @EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true)
|
---|
10 | public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
|
---|
11 |
|
---|
12 |
|
---|
13 |
|
---|
14 | private final PasswordEncoder passwordEncoder;
|
---|
15 | private final CustomUsernamePasswordAuthenticationProvider authenticationProvider;
|
---|
16 |
|
---|
17 | public WebSecurityConfig(
|
---|
18 | PasswordEncoder passwordEncoder, CustomUsernamePasswordAuthenticationProvider authenticationProvider) {
|
---|
19 | this.passwordEncoder = passwordEncoder;
|
---|
20 | this.authenticationProvider = authenticationProvider;
|
---|
21 | }
|
---|
22 |
|
---|
23 | @Override
|
---|
24 | protected void configure(HttpSecurity http) throws Exception {
|
---|
25 |
|
---|
26 | http.csrf().disable()
|
---|
27 | .authorizeRequests()
|
---|
28 | // .antMatchers("/").permitAll()
|
---|
29 | .antMatchers("/login", "/home",
|
---|
30 | "/home/**" ,"/register","/logout").permitAll()
|
---|
31 | .antMatchers("/admin/**").hasRole("EMPLOYEE")
|
---|
32 | .anyRequest().authenticated()
|
---|
33 | .and()
|
---|
34 | .formLogin()
|
---|
35 | //.loginPage("/login").permitAll()
|
---|
36 | .failureUrl("/login?error=BadCredentials")
|
---|
37 | .defaultSuccessUrl("/home", true)
|
---|
38 | .and()
|
---|
39 | .logout()
|
---|
40 | .logoutUrl("/logout")
|
---|
41 | .clearAuthentication(true)
|
---|
42 | .invalidateHttpSession(true)
|
---|
43 | .deleteCookies("JSESSIONID")
|
---|
44 | .logoutSuccessUrl("/login")
|
---|
45 | .and()
|
---|
46 | .exceptionHandling().accessDeniedPage("/access_denied");
|
---|
47 |
|
---|
48 | }
|
---|
49 |
|
---|
50 | @Override
|
---|
51 | protected void configure(AuthenticationManagerBuilder auth) throws Exception {
|
---|
52 | // auth.inMemoryAuthentication()
|
---|
53 | // .withUser("kostadin.mishev")
|
---|
54 | // .password(passwordEncoder.encode("km"))
|
---|
55 | // .authorities("ROLE_USER")
|
---|
56 | // .and()
|
---|
57 | // .withUser("admin")
|
---|
58 | // .password(passwordEncoder.encode("admin"))
|
---|
59 | // .authorities("ROLE_ADMIN");
|
---|
60 | auth.authenticationProvider(authenticationProvider);
|
---|
61 | }
|
---|
62 | // @Override
|
---|
63 | // protected void configure(AuthenticationManagerBuilder auth) throws Exception {
|
---|
64 | // auth.inMemoryAuthentication()
|
---|
65 | // .withUser("admin")
|
---|
66 | // .password(passwordEncoder.encode("admin")).authorities("ROLE_EMPLOYEE");
|
---|
67 | // }
|
---|
68 |
|
---|
69 | }
|
---|
70 | */
|
---|