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 | import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
---|
12 |
|
---|
13 |
|
---|
14 | @Configuration
|
---|
15 | @EnableWebSecurity
|
---|
16 | @EnableWebMvc
|
---|
17 | @EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true)
|
---|
18 | public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
|
---|
19 |
|
---|
20 | private final PasswordEncoder passwordEncoder;
|
---|
21 | private final CustomUsernamePasswordAuthenticationProvider authenticationProvider;
|
---|
22 |
|
---|
23 | public WebSecurityConfig(PasswordEncoder passwordEncoder,
|
---|
24 | CustomUsernamePasswordAuthenticationProvider authenticationProvider) {
|
---|
25 | this.passwordEncoder = passwordEncoder;
|
---|
26 | this.authenticationProvider = authenticationProvider;
|
---|
27 | }
|
---|
28 |
|
---|
29 | @Override
|
---|
30 | protected void configure(HttpSecurity http) throws Exception {
|
---|
31 |
|
---|
32 | http.csrf().disable()
|
---|
33 | .authorizeRequests()
|
---|
34 | .antMatchers("/", "/home", "/assets/**", "/register", "/api/**").permitAll()
|
---|
35 | .antMatchers("/admin/**").hasRole("ADMIN")
|
---|
36 | .anyRequest()
|
---|
37 | .authenticated()
|
---|
38 | .and()
|
---|
39 | .formLogin()
|
---|
40 | .loginPage("/login").permitAll()
|
---|
41 | .failureUrl("/login?error=BadCredentials")
|
---|
42 | .defaultSuccessUrl("/products", true)
|
---|
43 | .and()
|
---|
44 | .logout()
|
---|
45 | .logoutUrl("/logout")
|
---|
46 | .clearAuthentication(true)
|
---|
47 | .invalidateHttpSession(true)
|
---|
48 | .deleteCookies("JSESSIONID")
|
---|
49 | .logoutSuccessUrl("/login")
|
---|
50 | .and()
|
---|
51 | .exceptionHandling().accessDeniedPage("/access_denied");
|
---|
52 |
|
---|
53 | }
|
---|
54 |
|
---|
55 | @Override
|
---|
56 | protected void configure(AuthenticationManagerBuilder auth) {
|
---|
57 | //
|
---|
58 | auth.authenticationProvider(authenticationProvider);
|
---|
59 | }
|
---|
60 |
|
---|
61 |
|
---|
62 |
|
---|
63 | }
|
---|