Projekt

Obecné

Profil

Stáhnout (4.26 KB) Statistiky
| Větev: | Revize:
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.PasswordEncoder;
17
import org.springframework.security.web.util.matcher.RegexRequestMatcher;
18
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
19
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
20
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
21
import org.thymeleaf.extras.springsecurity5.dialect.SpringSecurityDialect;
22
import org.thymeleaf.spring5.SpringTemplateEngine;
23
import org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver;
24
import org.thymeleaf.spring5.view.ThymeleafViewResolver;
25
import org.thymeleaf.templatemode.TemplateMode;
26

    
27
@Configuration
28
@ComponentScan
29
@EnableWebMvc
30
@EnableWebSecurity
31
@EnableAutoConfiguration
32
public class AppConfig extends WebSecurityConfigurerAdapter implements WebMvcConfigurer {
33

    
34
	private UserDetailsService userDetailsService;
35

    
36
	@Autowired
37
	public void setUserDetailsService(UserDetailsService userDetailsService) {
38
		this.userDetailsService = userDetailsService;
39
	}
40

    
41
    @Bean
42
    public SpringResourceTemplateResolver templateResolver(){
43
        SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
44
        templateResolver.setApplicationContext(this.getApplicationContext());
45
        templateResolver.setPrefix("/WEB-INF/templates/");
46
        templateResolver.setSuffix(".html");
47
        templateResolver.setTemplateMode(TemplateMode.HTML);
48
        // Template cache is true by default. Set to false if you want
49
        // templates to be automatically updated when modified.
50
        templateResolver.setCacheable(false);
51
        return templateResolver;
52
    }
53

    
54
    @Bean
55
    public SpringTemplateEngine templateEngine(){
56
        SpringTemplateEngine templateEngine = new SpringTemplateEngine();
57
        templateEngine.setTemplateResolver(templateResolver());
58
        templateEngine.setEnableSpringELCompiler(true);
59
        templateEngine.addDialect("sec", new SpringSecurityDialect());
60
        templateEngine.addDialect(new LayoutDialect());
61
        return templateEngine;
62
    }
63

    
64
    @Bean
65
    public ThymeleafViewResolver viewResolver(){
66
        ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
67
        viewResolver.setTemplateEngine(templateEngine());
68
        return viewResolver;
69
    }
70

    
71
    @Bean
72
    public PasswordEncoder passwordEncoder() {
73
    	return new BCryptPasswordEncoder();
74
    }
75

    
76
	@Override
77
	protected void configure(HttpSecurity http) throws Exception {
78
		/*
79
		http
80
		.authorizeRequests()
81
			.mvcMatchers("/login").permitAll()
82
			.regexMatchers(HttpMethod.GET, "^/css/.*", "^/webfonts/.*").permitAll()
83
			.anyRequest().authenticated()
84
			.and()
85
		.formLogin()
86
			.loginPage("/login")
87
			.permitAll()
88
			.defaultSuccessUrl("/")
89
			.and()
90
		.logout()
91
			.logoutRequestMatcher(new RegexRequestMatcher("/logout", "GET"))
92
			.invalidateHttpSession(true)
93
			.deleteCookies("JSESSIONID")
94
			.permitAll();
95
		 */
96
	}
97

    
98
	@Override
99
	protected void configure(AuthenticationManagerBuilder auth) throws Exception {
100
		auth
101
		.userDetailsService(this.userDetailsService)
102
		.passwordEncoder(passwordEncoder());
103
	}
104

    
105
	@Override
106
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
107
		registry
108
		.addResourceHandler("/css/**")
109
		.addResourceLocations("/css/");
110
		registry
111
		.addResourceHandler("/webfonts/**")
112
		.addResourceLocations("/webfonts/");
113
	}
114

    
115
}
(1-1/2)