Skip to content

File tree

6 files changed

+93
-68
lines changed

6 files changed

+93
-68
lines changed

pom.xml

-1
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,6 @@
175175
<dependency>
176176
<groupId>org.springframework.security</groupId>
177177
<artifactId>spring-security-config</artifactId>
178-
<scope>runtime</scope>
179178
</dependency>
180179

181180
<dependency>

src/main/config/checkstyle-suppressions.xml

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77

88
<suppress checks="LineLength" files="AbstractPageWithForm.java" lines="27,28,33" />
99
<suppress checks="LineLength" files="Form.java" lines="31,32,37" />
10-
<suppress checks="LineLength" files="Url.java" lines="73" />
10+
<suppress checks="LineLength" files="Url.java" lines="72" />
1111
<suppress checks="LineLength" files="ErrorController.java" lines="73" />
12+
<suppress checks="LineLength" files="SecurityConfig.java" lines="35,36,40" />
1213

1314
<!-- false positives due to Lombok usage -->
1415
<suppress checks="HideUtilityClassConstructor" files="ru.mystamps.web.model" />

src/main/java/ru/mystamps/web/Url.java

-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ public final class Url {
4040

4141
public static final String REGISTRATION_PAGE = "/account/register";
4242

43-
// defined at src/main/resources/spring/security.xml
4443
public static final String AUTHENTICATION_PAGE = "/account/auth";
4544
public static final String LOGIN_PAGE = "/account/login";
4645
public static final String LOGOUT_PAGE = "/account/logout";

src/main/java/ru/mystamps/web/support/spring/security/CustomUserDetails.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public CustomUserDetails(User user, Collection<? extends GrantedAuthority> autho
3131
this.user = user;
3232
}
3333

34-
// used during authentication by password-encoder with salt-source
34+
// used during authentication (see SecurityConfig.getSaltSource())
3535
public String getSalt() {
3636
return user.getSalt();
3737
}

src/main/java/ru/mystamps/web/support/spring/security/SecurityConfig.java

+90-14
Original file line numberDiff line numberDiff line change
@@ -21,44 +21,120 @@
2121

2222
import org.springframework.context.annotation.Bean;
2323
import org.springframework.context.annotation.Configuration;
24-
import org.springframework.context.annotation.ImportResource;
2524
import org.springframework.context.ApplicationListener;
25+
import org.springframework.http.HttpMethod;
2626

27+
import org.springframework.context.MessageSource;
28+
import org.springframework.security.authentication.AuthenticationProvider;
29+
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
30+
import org.springframework.security.authentication.dao.ReflectionSaltSource;
31+
import org.springframework.security.authentication.dao.SaltSource;
2732
import org.springframework.security.authentication.encoding.PasswordEncoder;
2833
import org.springframework.security.authentication.encoding.ShaPasswordEncoder;
2934
import org.springframework.security.authentication.event.AuthenticationFailureBadCredentialsEvent;
35+
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
36+
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
37+
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
38+
import org.springframework.security.config.annotation.web.builders.WebSecurity;
39+
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
40+
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
3041
import org.springframework.security.core.userdetails.UserDetailsService;
31-
import org.springframework.security.web.AuthenticationEntryPoint;
3242

3343
import ru.mystamps.web.config.ServicesConfig;
44+
import ru.mystamps.web.Url;
3445

3546
@Configuration
36-
@ImportResource("classpath:spring/security.xml")
37-
public class SecurityConfig {
47+
@EnableWebSecurity
48+
@EnableGlobalMethodSecurity(prePostEnabled = true)
49+
public class SecurityConfig extends WebSecurityConfigurerAdapter {
50+
51+
@Inject
52+
private MessageSource messageSource;
3853

3954
@Inject
4055
private ServicesConfig servicesConfig;
4156

42-
@Bean
43-
public ApplicationListener<AuthenticationFailureBadCredentialsEvent> getApplicationListener() {
44-
return new AuthenticationFailureListener();
57+
@Override
58+
@SuppressWarnings("PMD.SignatureDeclareThrowsException")
59+
public void configure(WebSecurity web) throws Exception {
60+
web.ignoring().antMatchers("/static/**");
4561
}
4662

47-
// Explicitly specified bean names due to its usage in XML config
63+
@Override
64+
@SuppressWarnings("PMD.SignatureDeclareThrowsException")
65+
protected void configure(HttpSecurity http) throws Exception {
66+
http
67+
.authorizeRequests()
68+
.antMatchers(Url.ADD_CATEGORY_PAGE).hasAuthority("CREATE_CATEGORY")
69+
.antMatchers(Url.ADD_COUNTRY_PAGE).hasAuthority("CREATE_COUNTRY")
70+
.antMatchers(Url.ADD_SERIES_PAGE).hasAuthority("CREATE_SERIES")
71+
.regexMatchers(HttpMethod.POST, "/series/[0-9]+").hasAuthority("UPDATE_COLLECTION")
72+
.anyRequest().permitAll()
73+
.and()
74+
.formLogin()
75+
.loginPage(Url.AUTHENTICATION_PAGE)
76+
.usernameParameter("login")
77+
.passwordParameter("password")
78+
.loginProcessingUrl(Url.LOGIN_PAGE)
79+
.failureUrl(Url.AUTHENTICATION_PAGE + "?failed")
80+
.defaultSuccessUrl(Url.INDEX_PAGE, true)
81+
.permitAll()
82+
.and()
83+
.logout()
84+
.logoutUrl(Url.LOGOUT_PAGE)
85+
.logoutSuccessUrl(Url.INDEX_PAGE)
86+
.invalidateHttpSession(true)
87+
.permitAll()
88+
.and()
89+
.exceptionHandling()
90+
.accessDeniedPage(Url.UNAUTHORIZED_PAGE)
91+
// This entry point handles when you request a protected page and you are
92+
// not yet authenticated (defaults to Http403ForbiddenEntryPoint)
93+
.authenticationEntryPoint(new Http401UnauthorizedEntryPoint())
94+
.and()
95+
.rememberMe()
96+
// TODO: GH #27
97+
.disable()
98+
.csrf()
99+
// TODO: GH #25
100+
.disable()
101+
.headers()
102+
// TODO
103+
.disable();
104+
}
48105

49-
@Bean(name = "passwordEncoder")
106+
@Inject
107+
protected void configure(AuthenticationManagerBuilder auth) {
108+
auth.authenticationProvider(getAuthenticationProvider());
109+
}
110+
111+
// Used in ServicesConfig.getUserService()
50112
public PasswordEncoder getPasswordEncoder() {
51113
return new ShaPasswordEncoder();
52114
}
115+
116+
@Bean
117+
public ApplicationListener<AuthenticationFailureBadCredentialsEvent> getApplicationListener() {
118+
return new AuthenticationFailureListener();
119+
}
53120

54-
@Bean(name = "customUserDetailsService")
55-
public UserDetailsService getUserDetailsService() {
121+
private UserDetailsService getUserDetailsService() {
56122
return new CustomUserDetailsService(servicesConfig.getUserService());
57123
}
58124

59-
@Bean(name = "http401UnauthorizedEntryPoint")
60-
public AuthenticationEntryPoint getHttp401UnauthorizedEntryPoint() {
61-
return new Http401UnauthorizedEntryPoint();
125+
private SaltSource getSaltSource() {
126+
ReflectionSaltSource saltSource = new ReflectionSaltSource();
127+
saltSource.setUserPropertyToUse("salt");
128+
return saltSource;
129+
}
130+
131+
private AuthenticationProvider getAuthenticationProvider() {
132+
DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
133+
provider.setPasswordEncoder(getPasswordEncoder());
134+
provider.setSaltSource(getSaltSource());
135+
provider.setUserDetailsService(getUserDetailsService());
136+
provider.setMessageSource(messageSource);
137+
return provider;
62138
}
63139

64140
}

src/main/resources/spring/security.xml

-50
This file was deleted.

0 commit comments

Comments
 (0)