1 | package com.example.moviezone.config;
|
---|
2 |
|
---|
3 | import org.springframework.beans.factory.annotation.Autowired;
|
---|
4 | import org.springframework.context.annotation.Configuration;
|
---|
5 | import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
|
---|
6 | import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
|
---|
7 | import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
---|
8 | import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
---|
9 | import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
---|
10 | import org.springframework.security.crypto.password.PasswordEncoder;
|
---|
11 |
|
---|
12 |
|
---|
13 | @Configuration
|
---|
14 | @EnableWebSecurity
|
---|
15 | @EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true)
|
---|
16 | public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
|
---|
17 |
|
---|
18 | private final PasswordEncoder passwordEncoder;
|
---|
19 | private final CustomUsernamePasswordAuthenticationProvider authenticationProvider;
|
---|
20 |
|
---|
21 | public WebSecurityConfig(PasswordEncoder passwordEncoder,
|
---|
22 | CustomUsernamePasswordAuthenticationProvider authenticationProvider) {
|
---|
23 | this.passwordEncoder = passwordEncoder;
|
---|
24 | this.authenticationProvider = authenticationProvider;
|
---|
25 | }
|
---|
26 |
|
---|
27 | @Override
|
---|
28 | protected void configure(HttpSecurity http) throws Exception {
|
---|
29 |
|
---|
30 | http.csrf().disable()
|
---|
31 | .authorizeRequests()
|
---|
32 | .antMatchers("/", "/home", "/assets/**", "/register", "/api/**").permitAll()
|
---|
33 | .antMatchers("/admin/**").hasRole("ADMIN")
|
---|
34 | .anyRequest()
|
---|
35 | .authenticated()
|
---|
36 | .and()
|
---|
37 | .formLogin()
|
---|
38 | .loginPage("/login").permitAll()
|
---|
39 | .failureUrl("/login?error=BadCredentials")
|
---|
40 | .defaultSuccessUrl("/products", true)
|
---|
41 | .and()
|
---|
42 | .logout()
|
---|
43 | .logoutUrl("/logout")
|
---|
44 | .invalidateHttpSession(true)
|
---|
45 | .deleteCookies("JSESSIONID")
|
---|
46 | .logoutSuccessUrl("/login")
|
---|
47 | .and()
|
---|
48 | .exceptionHandling().accessDeniedPage("/access_denied");
|
---|
49 |
|
---|
50 | }
|
---|
51 |
|
---|
52 | @Override
|
---|
53 | protected void configure(AuthenticationManagerBuilder auth) {
|
---|
54 | //
|
---|
55 | auth.authenticationProvider(authenticationProvider);
|
---|
56 | }
|
---|
57 |
|
---|
58 |
|
---|
59 |
|
---|
60 | }
|
---|