Projekt

Obecné

Profil

Stáhnout (4.7 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.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
			.antMatchers("/assembly_new").hasRole("Administrátor")
88
			.antMatchers("/assembly_edit").hasRole("Administrátor")
89
			.antMatchers("/assembly_delete").hasRole("Administrátor")
90
			.regexMatchers(HttpMethod.GET, "^/css/.*", "^/webfonts/.*").permitAll()
91
			.anyRequest().authenticated()
92
			.and()
93
		.formLogin()
94
			.loginPage("/login")
95
			.permitAll()
96
			.defaultSuccessUrl("/", true)
97
			.and()
98
		.logout()
99
			.logoutRequestMatcher(new RegexRequestMatcher("/logout", "POST"))
100
			.invalidateHttpSession(true)
101
			.deleteCookies("JSESSIONID")
102
			.permitAll();
103
	}
104

    
105
	@Override
106
	protected void configure(AuthenticationManagerBuilder auth) throws Exception {
107
		auth
108
		.userDetailsService(this.userDetailsService)
109
		.passwordEncoder(passwordEncoder());
110
	}
111

    
112
	@Override
113
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
114
		registry
115
		.addResourceHandler("/css/**")
116
		.addResourceLocations("/css/");
117
        registry
118
		.addResourceHandler("/js/**")
119
		.addResourceLocations("/js/");
120
		registry
121
		.addResourceHandler("/webfonts/**")
122
		.addResourceLocations("/webfonts/");
123
	}
124

    
125
}
(1-1/2)