configure(HttpSecurity)   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 29
rs 9.184
c 0
b 0
f 0
1
package easytests.config;
2
3
import easytests.auth.handlers.AuthenticationFailureHandler;
4
import easytests.auth.services.AuthUsersService;
5
import org.springframework.beans.factory.annotation.Autowired;
6
import org.springframework.context.annotation.Configuration;
7
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
8
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
9
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
10
11
12
/**
13
 * @author malinink
14
 */
15
@Configuration
16
@EnableWebSecurity
17
public class SecurityConfig extends WebSecurityConfigurerAdapter {
18
    @Autowired
19
    private AuthUsersService authUsersService;
20
21
    @Override
22
    protected void configure(HttpSecurity http) throws Exception {
23
        final String signInUrl = "/auth/sign-in";
24
        final String usernameParameter = "login";
25
        final String userRole = "hasRole('USER')";
26
        http
27
            .authorizeRequests()
28
                .antMatchers("/").permitAll()
29
                .antMatchers("/admin/**").access("hasRole('ADMIN')")
30
                .antMatchers("/users/**").access(userRole)
31
                .antMatchers("/personal/**").access(userRole)
32
                .and()
33
            .userDetailsService(this.authUsersService)
34
            .formLogin()
35
                .loginPage(signInUrl)
36
                .loginProcessingUrl(signInUrl)
37
                .usernameParameter(usernameParameter)
38
                .passwordParameter("password")
39
                .defaultSuccessUrl("/users")
40
                .failureHandler(new AuthenticationFailureHandler("/auth/sign-in?error", usernameParameter))
41
                .and()
42
            .logout()
43
                .logoutUrl("/auth/sign-out")
44
                .logoutSuccessUrl(signInUrl)
45
                .clearAuthentication(true)
46
                .and()
47
            .csrf()
48
                .and()
49
            .rememberMe();
50
    }
51
}
52