1
|
package vldc.aswi.configuration;
|
2
|
|
3
|
import nz.net.ultraq.thymeleaf.LayoutDialect;
|
4
|
import org.springframework.beans.factory.annotation.Autowired;
|
5
|
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
6
|
import org.springframework.context.annotation.Bean;
|
7
|
import org.springframework.context.annotation.ComponentScan;
|
8
|
import org.springframework.context.annotation.Configuration;
|
9
|
import org.springframework.http.HttpMethod;
|
10
|
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
|
11
|
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
12
|
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
13
|
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
14
|
import org.springframework.security.core.userdetails.UserDetailsService;
|
15
|
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
16
|
import org.springframework.security.crypto.password.DelegatingPasswordEncoder;
|
17
|
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
|
18
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
19
|
import org.springframework.security.web.util.matcher.RegexRequestMatcher;
|
20
|
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
21
|
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
|
22
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
23
|
import org.thymeleaf.extras.springsecurity5.dialect.SpringSecurityDialect;
|
24
|
import org.thymeleaf.spring5.SpringTemplateEngine;
|
25
|
import org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver;
|
26
|
import org.thymeleaf.spring5.view.ThymeleafViewResolver;
|
27
|
import org.thymeleaf.templatemode.TemplateMode;
|
28
|
|
29
|
/**
|
30
|
* Application configuration.
|
31
|
*/
|
32
|
@Configuration
|
33
|
@ComponentScan
|
34
|
@EnableWebMvc
|
35
|
@EnableWebSecurity
|
36
|
@EnableAutoConfiguration
|
37
|
public class AppConfig extends WebSecurityConfigurerAdapter implements WebMvcConfigurer {
|
38
|
|
39
|
private UserDetailsService userDetailsService;
|
40
|
|
41
|
@Autowired
|
42
|
public void setUserDetailsService(UserDetailsService userDetailsService) {
|
43
|
this.userDetailsService = userDetailsService;
|
44
|
}
|
45
|
|
46
|
@Bean
|
47
|
public SpringResourceTemplateResolver templateResolver(){
|
48
|
SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
|
49
|
templateResolver.setApplicationContext(this.getApplicationContext());
|
50
|
templateResolver.setPrefix("/WEB-INF/templates/");
|
51
|
templateResolver.setSuffix(".html");
|
52
|
templateResolver.setTemplateMode(TemplateMode.HTML);
|
53
|
// Template cache is true by default. Set to false if you want
|
54
|
// templates to be automatically updated when modified.
|
55
|
templateResolver.setCacheable(false);
|
56
|
return templateResolver;
|
57
|
}
|
58
|
|
59
|
@Bean
|
60
|
public SpringTemplateEngine templateEngine(){
|
61
|
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
|
62
|
templateEngine.setTemplateResolver(templateResolver());
|
63
|
templateEngine.setEnableSpringELCompiler(true);
|
64
|
templateEngine.addDialect("sec", new SpringSecurityDialect());
|
65
|
templateEngine.addDialect(new LayoutDialect());
|
66
|
return templateEngine;
|
67
|
}
|
68
|
|
69
|
@Bean
|
70
|
public ThymeleafViewResolver viewResolver(){
|
71
|
ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
|
72
|
viewResolver.setTemplateEngine(templateEngine());
|
73
|
return viewResolver;
|
74
|
}
|
75
|
|
76
|
|
77
|
@Bean
|
78
|
public PasswordEncoder passwordEncoder() {
|
79
|
return new BCryptPasswordEncoder();
|
80
|
}
|
81
|
|
82
|
@Override
|
83
|
protected void configure(HttpSecurity http) throws Exception {
|
84
|
http
|
85
|
.authorizeRequests()
|
86
|
.mvcMatchers("/login").permitAll()
|
87
|
.regexMatchers(HttpMethod.GET, "^/css/.*", "^/webfonts/.*").permitAll()
|
88
|
.anyRequest().authenticated()
|
89
|
.and()
|
90
|
.formLogin()
|
91
|
.loginPage("/login")
|
92
|
.permitAll()
|
93
|
.defaultSuccessUrl("/", true)
|
94
|
.and()
|
95
|
.logout()
|
96
|
.logoutRequestMatcher(new RegexRequestMatcher("/logout", "GET"))
|
97
|
.invalidateHttpSession(true)
|
98
|
.deleteCookies("JSESSIONID")
|
99
|
.permitAll();
|
100
|
}
|
101
|
|
102
|
@Override
|
103
|
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
|
104
|
auth
|
105
|
.userDetailsService(this.userDetailsService)
|
106
|
.passwordEncoder(passwordEncoder());
|
107
|
}
|
108
|
|
109
|
@Override
|
110
|
public void addResourceHandlers(ResourceHandlerRegistry registry) {
|
111
|
registry
|
112
|
.addResourceHandler("/css/**")
|
113
|
.addResourceLocations("/css/");
|
114
|
registry
|
115
|
.addResourceHandler("/js/**")
|
116
|
.addResourceLocations("/js/");
|
117
|
registry
|
118
|
.addResourceHandler("/webfonts/**")
|
119
|
.addResourceLocations("/webfonts/");
|
120
|
}
|
121
|
|
122
|
}
|