Skip to content

Commit 6ec417e

Browse files
committed
work
1 parent e147575 commit 6ec417e

File tree

4 files changed

+332
-19
lines changed

4 files changed

+332
-19
lines changed
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
package org.woehlke.greenshop.conf;
2+
3+
import lombok.EqualsAndHashCode;
4+
import lombok.Getter;
5+
import lombok.Setter;
6+
import lombok.ToString;
7+
import lombok.extern.slf4j.Slf4j;
8+
import org.springframework.boot.context.properties.ConfigurationProperties;
9+
import org.springframework.stereotype.Component;
10+
import org.springframework.validation.annotation.Validated;
11+
12+
import javax.validation.Valid;
13+
import javax.validation.constraints.Email;
14+
import javax.validation.constraints.NotBlank;
15+
import javax.validation.constraints.NotNull;
16+
import java.io.Serializable;
17+
18+
19+
@Slf4j
20+
@Getter
21+
@Setter
22+
@ToString
23+
@EqualsAndHashCode
24+
@Validated
25+
@Component
26+
@ConfigurationProperties(prefix="org.woehlke.greenshop")
27+
public class GreenshopProperties implements Serializable {
28+
29+
private static final long serialVersionUID = 4480323170764476017L;
30+
31+
@NotNull
32+
private Boolean devTesting;
33+
34+
@NotNull
35+
private Integer testDataHowManyTestData;
36+
37+
@Valid
38+
@NotNull
39+
public UserConfig userConfig;
40+
41+
@Valid
42+
@NotNull
43+
public WebConfig webConfig;
44+
45+
@Valid
46+
@NotNull
47+
public WebSecurity webSecurity;
48+
49+
@ToString
50+
@Getter
51+
@Setter
52+
@Validated
53+
public static class UserConfig {
54+
55+
@Email
56+
@NotBlank
57+
private String userEmail;
58+
59+
@NotBlank
60+
private String userPassword;
61+
62+
@NotBlank
63+
private String userFullname;
64+
}
65+
66+
@ToString
67+
@Getter
68+
@Setter
69+
@Validated
70+
public static class WebConfig {
71+
72+
@NotBlank
73+
private String exportFilename;
74+
75+
@NotBlank
76+
private String exportFilenameSeparator;
77+
78+
@NotNull
79+
private String[] webAddResourceHandlers;
80+
81+
@NotNull
82+
private String[] webAddResourceHandlersStatic;
83+
}
84+
85+
86+
@ToString
87+
@Getter
88+
@Setter
89+
@Validated
90+
public static class WebSecurity {
91+
92+
@NotNull
93+
private Boolean invalidateHttpSession;
94+
95+
@NotBlank
96+
private String loginProcessingUrl;
97+
98+
@NotBlank
99+
private String failureForwardUrl;
100+
101+
@NotBlank
102+
private String defaultSuccessUrl;
103+
104+
@NotBlank
105+
private String logoutUrl;
106+
107+
@NotBlank
108+
private String loginPage;
109+
110+
@NotBlank
111+
private String deleteCookies;
112+
113+
@NotBlank
114+
private String antMatchersFullyAuthenticated;
115+
116+
@NotNull
117+
private String[] antMatchersPermitAll;
118+
119+
@NotBlank
120+
private String usernameParameter;
121+
122+
@NotBlank
123+
private String passwordParameter;
124+
125+
@NotBlank
126+
private String secret;
127+
128+
@NotNull
129+
private Integer iterations;
130+
131+
@NotNull
132+
private Integer hashWidth;
133+
}
134+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package org.woehlke.greenshop.conf;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
5+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
6+
import org.springframework.context.annotation.Bean;
7+
import org.springframework.context.annotation.Configuration;
8+
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
9+
import org.springframework.data.web.config.EnableSpringDataWebSupport;
10+
import org.springframework.scheduling.annotation.EnableAsync;
11+
import org.springframework.validation.beanvalidation.MethodValidationPostProcessor;
12+
import org.springframework.web.servlet.LocaleResolver;
13+
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
14+
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
15+
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
16+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
17+
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
18+
import org.springframework.web.servlet.i18n.SessionLocaleResolver;
19+
20+
import java.util.Locale;
21+
22+
@Configuration
23+
@EnableAsync
24+
@EnableJpaRepositories({
25+
"org.woehlke.greenshop"
26+
})
27+
@EnableConfigurationProperties({
28+
GreenshopProperties.class
29+
})
30+
@EnableWebMvc
31+
@EnableSpringDataWebSupport
32+
@EnableAutoConfiguration
33+
public class GreenshopWebMvcConfig implements WebMvcConfigurer {
34+
35+
private final GreenshopProperties greenshopProperties;
36+
37+
@Autowired
38+
public GreenshopWebMvcConfig(GreenshopProperties greenshopProperties) {
39+
this.greenshopProperties = greenshopProperties;
40+
}
41+
42+
@Bean
43+
public LocaleResolver localeResolver() {
44+
SessionLocaleResolver slr = new SessionLocaleResolver();
45+
slr.setDefaultLocale(Locale.GERMAN);
46+
return slr;
47+
}
48+
49+
@Bean
50+
public LocaleChangeInterceptor localeChangeInterceptor() {
51+
LocaleChangeInterceptor lci = new LocaleChangeInterceptor();
52+
lci.setParamName("lang");
53+
return lci;
54+
}
55+
56+
@Bean
57+
public MethodValidationPostProcessor methodValidationPostProcessor() {
58+
return new MethodValidationPostProcessor();
59+
}
60+
61+
@Override
62+
public void addInterceptors(InterceptorRegistry registry) {
63+
registry.addInterceptor(localeChangeInterceptor());
64+
}
65+
66+
@Override
67+
public void addResourceHandlers(ResourceHandlerRegistry registry) {
68+
for(String key: greenshopProperties.getWebConfig().getWebAddResourceHandlers()){
69+
registry.addResourceHandler("/"+key+"*").addResourceLocations("/"+key);
70+
registry.addResourceHandler("/"+key+"**").addResourceLocations("/"+key);
71+
}
72+
for(String key: greenshopProperties.getWebConfig().getWebAddResourceHandlersStatic()){
73+
registry.addResourceHandler("/"+key+"*").addResourceLocations("classpath:/static/"+key);
74+
registry.addResourceHandler("/"+key+"**").addResourceLocations("classpath:/static/"+key);
75+
}
76+
}
77+
}
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
package org.woehlke.greenshop.conf;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
5+
import org.springframework.context.annotation.Bean;
6+
import org.springframework.context.annotation.Configuration;
7+
import org.springframework.context.annotation.Import;
8+
import org.springframework.data.web.config.EnableSpringDataWebSupport;
9+
import org.springframework.security.authentication.AuthenticationManager;
10+
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
11+
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
12+
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
13+
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
14+
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
15+
import org.springframework.security.core.userdetails.UserDetailsService;
16+
import org.springframework.security.crypto.password.PasswordEncoder;
17+
import org.springframework.security.crypto.password.Pbkdf2PasswordEncoder;
18+
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
19+
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
20+
import org.woehlke.greenshop.oodm.admin.service.AdministratorService;
21+
22+
23+
@SuppressWarnings("deprecation")
24+
@Configuration
25+
@EnableWebSecurity
26+
@EnableSpringDataWebSupport
27+
@Import({
28+
GreenshopWebMvcConfig.class
29+
})
30+
@EnableWebMvc
31+
@EnableAutoConfiguration
32+
public class GreenshopWebSecurityConfig extends WebSecurityConfigurerAdapter {
33+
34+
@Autowired
35+
public GreenshopWebSecurityConfig(
36+
AuthenticationManagerBuilder auth,
37+
AdministratorService administratorService,
38+
GreenshopProperties greenshopProperties
39+
) {
40+
this.authenticationManagerBuilder = auth;
41+
this.administratorService = administratorService;
42+
this.greenshopProperties = greenshopProperties;
43+
}
44+
45+
private final AuthenticationManagerBuilder authenticationManagerBuilder;
46+
private final UserDetailsService administratorService;
47+
private final GreenshopProperties greenshopProperties;
48+
49+
50+
@Override
51+
protected void configure(HttpSecurity http) throws Exception {
52+
http
53+
.headers()
54+
.disable()
55+
.authorizeRequests()
56+
.antMatchers(
57+
this.greenshopProperties.getWebSecurity().getAntMatchersPermitAll()
58+
)
59+
.permitAll()
60+
.antMatchers(
61+
this.greenshopProperties.getWebSecurity().getAntMatchersFullyAuthenticated()
62+
)
63+
.fullyAuthenticated().anyRequest().authenticated()
64+
.and()
65+
.formLogin()
66+
.loginPage(
67+
this.greenshopProperties.getWebSecurity().getLoginPage()
68+
)
69+
.usernameParameter(this.greenshopProperties.getWebSecurity().getUsernameParameter())
70+
.passwordParameter(this.greenshopProperties.getWebSecurity().getPasswordParameter())
71+
.defaultSuccessUrl(this.greenshopProperties.getWebSecurity().getDefaultSuccessUrl())
72+
.failureForwardUrl(this.greenshopProperties.getWebSecurity().getFailureForwardUrl())
73+
.loginProcessingUrl(this.greenshopProperties.getWebSecurity().getLoginProcessingUrl())
74+
//.successHandler(this.authenticationSuccessHandler)
75+
.permitAll()
76+
.and()
77+
.logout()
78+
.logoutUrl(this.greenshopProperties.getWebSecurity().getLogoutUrl())
79+
.deleteCookies(this.greenshopProperties.getWebSecurity().getDeleteCookies())
80+
.invalidateHttpSession(this.greenshopProperties.getWebSecurity().getInvalidateHttpSession())
81+
.permitAll();
82+
}
83+
84+
/**
85+
* @see <a href="https://asecuritysite.com/encryption/PBKDF2">Encrypt with PBKDF2</a>
86+
* @return PasswordEncoder encoder
87+
*/
88+
@Bean
89+
public PasswordEncoder encoder(){
90+
CharSequence secret=this.greenshopProperties.getWebSecurity().getSecret();
91+
int iterations=this.greenshopProperties.getWebSecurity().getIterations();
92+
int hashWidth=this.greenshopProperties.getWebSecurity().getHashWidth();
93+
Pbkdf2PasswordEncoder encoder = (new Pbkdf2PasswordEncoder(secret,iterations,hashWidth));
94+
encoder.setEncodeHashAsBase64(true);
95+
return encoder;
96+
}
97+
98+
@Bean
99+
public DaoAuthenticationProvider authProvider() {
100+
DaoAuthenticationProvider authProvider = new DaoAuthenticationProvider();
101+
authProvider.setUserDetailsService( this.administratorService);
102+
authProvider.setPasswordEncoder(encoder());
103+
return authProvider;
104+
}
105+
106+
@Bean
107+
public AuthenticationManager authenticationManager() throws Exception {
108+
return this.authenticationManagerBuilder.userDetailsService(administratorService).passwordEncoder(encoder()).and().build();
109+
}
110+
111+
@Bean
112+
public UsernamePasswordAuthenticationFilter authenticationFilter() throws Exception {
113+
UsernamePasswordAuthenticationFilter filter = new UsernamePasswordAuthenticationFilter();
114+
filter.setAuthenticationManager(authenticationManager());
115+
filter.setFilterProcessesUrl(this.greenshopProperties.getWebSecurity().getLoginProcessingUrl());
116+
return filter;
117+
}
118+
119+
}

src/main/resources/root-context.xml

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
xmlns:context="http://www.springframework.org/schema/context"
66
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
77
xmlns:int="http://www.springframework.org/schema/integration"
8-
xmlns:int-mongodb="http://www.springframework.org/schema/integration/mongodb"
98
xsi:schemaLocation="http://www.springframework.org/schema/beans
109
http://www.springframework.org/schema/beans/spring-beans.xsd
1110
http://www.springframework.org/schema/tx
@@ -14,26 +13,9 @@
1413
http://www.springframework.org/schema/context/spring-context.xsd
1514
http://www.springframework.org/schema/data/jpa
1615
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
17-
http://www.springframework.org/schema/integration/mongodb
18-
http://www.springframework.org/schema/integration/mongodb/spring-integration-mongodb.xsd
1916
http://www.springframework.org/schema/integration
2017
http://www.springframework.org/schema/integration/spring-integration.xsd">
2118

22-
<!--
23-
<bean id="mongoDbFactory"
24-
class="org.springframework.data.mongodb.core.SimpleMongoDbFactory">
25-
<constructor-arg>
26-
<bean class="com.mongodb.Mongo" />
27-
</constructor-arg>
28-
<constructor-arg value="test" />
29-
</bean>
30-
31-
<bean id="mongoDbMessageStore"
32-
class="org.springframework.integration.mongodb.store.MongoDbMessageStore">
33-
<constructor-arg ref="mongoDbFactory" />
34-
</bean>
35-
-->
36-
3719
<jpa:repositories base-package="org.woehlke.greenshop" />
3820

3921
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
@@ -84,7 +66,8 @@
8466
<!--
8567
<int:service-activator input-channel="emailChannel"
8668
output-channel="nullChannel" id="sendMail"
87-
ref="registrationService" method="sendMail" />-->
69+
ref="registrationService" method="sendMail" />
70+
-->
8871

8972
<!-- Mail service -->
9073
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">

0 commit comments

Comments
 (0)