Projekt

Obecné

Profil

Stáhnout (4.29 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.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
10
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
11
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
12
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
13
import org.springframework.security.core.userdetails.UserDetailsService;
14
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
15
import org.springframework.security.crypto.password.PasswordEncoder;
16
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
17
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
18
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
19
import org.thymeleaf.extras.springsecurity5.dialect.SpringSecurityDialect;
20
import org.thymeleaf.spring5.SpringTemplateEngine;
21
import org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver;
22
import org.thymeleaf.spring5.view.ThymeleafViewResolver;
23
import org.thymeleaf.templatemode.TemplateMode;
24

    
25
/**
26
 * Application configuration.
27
 */
28
@Configuration
29
@ComponentScan
30
@EnableWebMvc
31
@EnableWebSecurity
32
@EnableAutoConfiguration
33
public class AppConfig extends WebSecurityConfigurerAdapter implements WebMvcConfigurer {
34

    
35
	private UserDetailsService userDetailsService;
36

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

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

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

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

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

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

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

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

    
112
        registry
113
                .addResourceHandler("/js/**")
114
                .addResourceLocations("/js/");
115
		registry
116
		.addResourceHandler("/webfonts/**")
117
		.addResourceLocations("/webfonts/");
118
	}
119

    
120
}
(1-1/2)